• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Linux / UNIX : How to send mails with attachments using mailx command

by admin

Question : How to send email with attachments from Linux machine using mailx command ?

Answer :

Basic syntax to send emails from linux machine using mailx command is show below :

# echo "this is the body of the email" | mailx -vvv -s "test mail" -r "From" -S smtp="your-smtp" someone@address

here,

-vvv =        Verbosity.
-s   =        Specifies the subject.
-r   =        Email sent from.
-S   =        Specifies the smtp server.

Method 1 : using uuencode (old method)

If the mailx version is below 12.x, you can use the uuencode command to send mails with attachments.

# uuencode [path/to/file] [name_of_attachment] | mailx -s "Subject" user@domain.com

Method 2 : -a switch in mailx command

Use the new attachment switch (-a) in mailx to send attachments with the mail. The -a options is easier to use that the uuencode command.

# mailx -a file.txt -s "Subject" user@domain.com

The above command will print a new blank line. Type the body of the message here and press [ctrl] + [d] to send. This will attach the file to the outbound email correctly with proper Content-Type and boundary headers.

To make this more “scriptable”, you can send the message without a body with the following:

# mailx -a file.txt -s "Subject" user@domain.com < /dev/null

To send mails with a message body, replace /dev/null in above command with your message body file.

In the newer version of mailx, the headers that are used in outgoing email changed from:

From: 
Date: 
To: 
Subject:

to to the below form:

From: 
Date: 
To: 
Subject: 
User-Agent: 
MIME-Version: 
Content-Type: 
Content-Transfer-Encoding:
NOTE: 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.
Linux / UNIX : Send mail with attachment using mutt
How to send mails with attachments using the solaris mailx command

Filed Under: Linux

Some more articles you might also be interested in …

  1. ctr: command not found
  2. e2image: command not found
  3. RHEL/CentOS 6,7 : How to caculate the size of hugepage used by a specific process/application
  4. tail command examples in UNIX/Linux
  5. How To Migrate Existing Iptables rules to Nftables In CentOS/RHEL 8
  6. bpftrace: command not found
  7. CentOS / RHEL 7 : How to remove rescue image using grubby
  8. CentOS / RHEL 7 : systemctl replacements of legacy commands service and chkconfig
  9. mkfs.exfat Command Examples in Linux
  10. nm : Command to list the symbols in object files.

You May Also Like

Primary Sidebar

Recent Posts

  • qm Command Examples in Linux
  • qm wait Command Examples in Linux
  • qm start Command Examples in Linux
  • qm snapshot Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright