How to attach a file to an email using command line in RHEL 6?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux 6
  • 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 way uuencoded 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?

Resolution

Either use mutt -a or mailx -a

  • mutt -a

    1. yum install mutt

    2. echo "MESSAGE BODY" | mutt -s "SUBJECT" -a "FILEPATH" "ANOTHERFILE" -- USER@DOM.TLD

      • Leave off the echo completely to enter message body interactively
  • mailx -a

    1. 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 dropping echo 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 & 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 of Content-Type: text/plain with no boundary. Email that has attachments using the newer mailx -a switch will have Content-Type: multipart/mixed; boundary="= ...." headers.

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.

6 Comments

This doesn't work as the -a option isn't found.

$ mailx -a /tmp/john -s "attachment" john@abc.com
mailx: invalid option -- a
Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...
[-- sendmail-options ...]
mail [-iInNv] -f [name]
mail [-iInNv] [-u user]

$ rpm -qa | grep mailx
mailx-8.1.1-44.2.2

Am I missing something?

Hi John. This article is for RHEL 6. Your mailx version shows me you're on RHEL 5. Go here: How to attach a file to an email using command line in Red Hat Enterprise Linux 5?

Hi,
Thanks for the article.
In RHEL 5, we can rename the attachment using uuencode, how about the case on RHEL 6, is there a way to rename attachment from command line? Thanks in advance!

"is there a way to rename attachment from command line?"

No there is not.

Hi,
Getting the below error while sending an email with attachment.

$echo "Test Email" | mutt -s "Email from #host" -a /tmp/test1.txt -a /tmp/test2.txt xyz@abc.com
Can't stat xyz@abc.com: No such file or directory
xyz@abc.com: unable to attach file.
$rpm -qa |grep -i mutt
mutt-1.5.20-7.20091214hg736b6a.el6.x86_64

https://access.redhat.com/solutions/43567 says "--" has to be added after the attachment , to split attachment and mail id.
could you please check and confirm ?

Solution given in https://access.redhat.com/solutions/43567 worked for me. ( add -- between attachment and "To" address.