Send an Email in Linux from the Shell with Attachment

Posted on Tuesday September 06, 2011 / by Eric Potvin

The "mail" command is commonly use to send email from the console without attachment. Here's a simple example of sending an email to "someone@domain.com" using "subject of the email" as the subject:

echo 'some message' | mail -s 'subject of the email' someone@domain.com

Let's CC someone else:

echo 'some message' | mail -s 'subject of the email' -c someoneelse@domain.com someone@domain.com

Now let's BCC someone else:

echo 'some message' | mail -s 'subject of the email' -c someoneelse@domain.com -b someoneimportant@domain.com someone@domain.com

If you already have your message save in a text file, you can use the following syntax:

mail -s 'subject' someone@domain.com < /tmp/messagePlease

Sending email with attachment from the shell

If you are looking to send email with attachment from the shell prompt, you will need to use the "mutt" tool. Mutt is a powerful text based program for reading electronic mail under Linux. Mutt requires a valid email server and a working mail transfer agent (sendmail, postfix) installed in order to successfully send emails.

First, let's install mutt (if it's not installed)

sudo aptitude install mutt

Use the following syntax to send an email with an attachment:

echo "some message" | mutt -s "subject of the email" -a /home/user/my-file.bzip someone@domain.com

The same parameters are used in mutt to CC or BCC someone.