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


It's not just the \n characters that need to be removed. It might also be ASCII characters above 128 (such as accented characters like á, é, í, ñ, ó, ú). To remove them:

tr -d '[\015\200-\377]' < input_file > output_file

After cleaning up the file, you can use it to send like this:

mailx -s 'Error Log' abc@mail.com < output_file

This time it should go through as the mail body instead of a binary attachment.


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

-q file
   Start the message with the contents of the specified file.  May be given in send mode only.

In your case it would be:

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

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.