mailx sends text body as an attachment

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux (RHEL) 6
  • mailx

Issue

When sending an e-mail from a command line or a script using mailx, the text intended to be a body is received as an attachment. This is often visible as a .bin attachment in Microsoft Outlook. Sometimes when the same command or script is used on RHEL 5, the e-mail is displayed as expected in the e-mail client (MUA).

Resolution

  • Remove non-US-ASCII or non-printable characters from the e-mail text, or
  • use sendmail, which will accept and forward DOS-style formatted text, or
  • use mutt, which provides more funcionality regarding how the e-mail should be sent.

Root Cause

As a matter of fact, RHEL 5 and RHEL 6 mailx commands are two different packages. While bsd-mailx is included in RHEL 5, mailx in RHEL 6 comes from the heirloom-mailx package, which is a different implementation of mailx and is not 100% compatible with bsd-mailx. mailx in RHEL 5 sometimes sends e-mails that are not compliant to applicable standards (most importantly RFC 2822). Such e-mails might be displayed correctly by coincidence though. mailx in RHEL 6 fixes many such occurrences, which might be observered as an unexpected behavior and paradoxically even leading to e-mails being incorrectly displayed by some e-mail clients (MUAs).

Due to non-US-ASCII or non-printable characters, when mailx processes the input, it correctly sets the MIME type to application/octet-stream when non-US-ASCII or non-printable characters are present. This is an expected behavior. E-mail clients then may display application/octet-stream content as an attachment rather than as an e-mail body. A non-printable character that often appears in text files and causes such issues is a DOS-style line ending \r\n, as opposed to \n used in UN*X-like operating systems.

Diagnostic Steps

As per mailx 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’ <input | mailx . . .

  or fix the tools that generate them.

Alternatively dos2unix can be used to strip the \r character. Similarly, if any other non-US-ASCII or non-printable characters are present, strip or convert them before passing the text to mailx.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

1 Comments

Rather than just stripping DOS \r characters, it might be more useful to also strip all extended characters (ASCII 128+): tr -d '[\015\200-\377]'