How to attach a file to an email using command line in RHEL 6 and later?
Environment
- Red Hat Enterprise Linux (RHEL)
- 6
- 7
- mailx
Issue
- How can I attach a file to an email using the command line in RHEL 6?
- Why when upgrading to
mailx 12.x
the wayuuencoded
email attachments are displayed in an email client changes from being shown as an attachment to being displayed inline? - Why does
mailx
sends uuencoded attachments as body in the mail? - We are not able to send the email as an attachment using the uuencode
Resolution
Either use mutt -a
or mailx -a
-
mutt -a
-
yum install mutt
-
echo "MESSAGE BODY" | mutt -s "SUBJECT" -a "FILEPATH" "ANOTHERFILE" -- USER@DOM.TLD
- Leave off the
echo
completely to enter message body interactively
- Leave off the
-
-
mailx -a
-
echo "MESSAGE BODY" | mailx -s "SUBJECT" -a "FILEPATH" -a "ANOTHERFILE" USER@DOM.TLD
-
Leave off the
echo
completely to enter message body interactively (press Ctrl-d when finished) -
Skip specifying a message body by using an
echo
with no arguments (echo | mailx ...
) or by droppingecho
and reading/dev/null
via stdin (mailx ... USER@DOM.TLD </dev/null
) -
For sending attachments in RHEL 5, see:
How to attach a file to an email using command line in RHEL 5?
-
-
Root Cause
There was a package change between RHEL 5 & RHEL 6. RHEL 5 had bsd-mailx
, and RHEL 6 has Heirloom mailx
. (That is to say, this is not a bug in uuencode
but simply a change in the mailx
package.)
In the newer version of mailx
the headers that are used in outgoing email changed from:
~~~
From:
Date:
To:
Subject:
~~~
to
~~~
From:
Date:
To:
Subject:
User-Agent:
MIME-Version:
Content-Type:
Content-Transfer-Encoding:
~~~
The old bsd-mailx
(same as RHEL 5) package is available (unsupported) for RHEL 6 via EPEL; however, note that any scripts that require the old bsd-mailx
package should be updated to use the new mailx
(bsd-mailx
will not be available in RHEL 7 and later)
Diagnostic Steps
- Check the headers in the received email:
- Email that has been sent with the old
uuencode
method of attachment will have a content type ofContent-Type: text/plain
with no boundary. - Email that has attachments using the newer
mailx -a
switch will haveContent-Type: multipart/mixed; boundary="= ...."
headers.
- Email that has been sent with the old
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.
Comments