I need to send an email using the mailx command. I am clear that the command would be the following by example:

echo "Body message" | mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com"

Now, I have a file and I need to send the contents of the file in the body of the email

mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com" < bodymail.txt


echo "$(cat bodymail.txt)" | mailx -s "Sending mail with Mailx" -r "abc@mail.com" "abc@mail.com"

and in both cases I always place the file as an attachment. Would they know how I can put the contents of the file in the body of the email and not as an attachment? The body message its convert into ATT00001.bin file attachment. I have removed the special characters from the file that goes in the mail bodymail.txt but I can not find a way to appear de content in the body message


Try the below command,

cat bodymail.txt | mailx -s "Sending mail with Mailx" abc@mail.com
  • Continues to give the same problem. Dont works. – alemarchan Sep 25 '18 at 14:14

I came across this problem. The hint I got for the solution was from the man page:

Mailx expects input text to be in Unix format, with lines separated by newline (^J, \n) characters only. Non-Unix text files that use carriage return (^M, \r) characters in addition will be treated as binary data; to send such files as text, strip these characters e. g. by tr -d '\015'

The solution they show with tr is only a partial solution. If there are other control characters in the file they will result on mailx treating the data as binary and will then attach it instead of using it as the body. The following will strip all special characters and place the contents of the file into the message body:

tr -cd "[:print:]\n" < SourceFile | mailx -s "Test Subject" myemail@contoso.com

See here for the tr discussion: tr remove all special chars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.