Send an Email in Linux from the Shell with Attachment
Wednesday, September 7, 2011 EDT
by: Eric Potvin
Tags:ubuntulinux
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.
Link to this Article
To link directly to this article from your web site, use one of the following snippets below.
Send an Email in Linux from the Shell with Attachment | Book Of Zeus<a href="http://www.bookofzeus.com/articles/send-an-email-in-linux-from-the-shell-with-attachment/" title="Send an Email in Linux from the Shell with Attachment">Send an Email in Linux from the Shell with Attachment | Book Of Zeus</a>
Short URL:
Send an Email in Linux from the Shell with Attachment | Book Of Zeus<a href="http://s.bookofzeus.com/YMFf6" title="Send an Email in Linux from the Shell with Attachment Short URL">Send an Email in Linux from the Shell with Attachment | Book Of Zeus</a>