Send an Email in Linux from the Shell with Attachment
The "mail" command is commonly used to send email from the console without attachment. Here's a simple example of sending an email to "email@example.com" using "subject of the email" as the subject:
echo 'some message' | mail -s 'subject of the email' firstname.lastname@example.org
Let's CC someone else:
echo 'some message' | mail -s 'subject of the email' -c email@example.com firstname.lastname@example.org
Now let's BCC someone else:
echo 'some message' | mail -s 'subject of the email' -c email@example.com -b firstname.lastname@example.org email@example.com
If you already have your message save in a text file, you can use the following syntax:
mail -s 'subject' firstname.lastname@example.org < /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 email@example.com
The same parameters are used in mutt to CC or BCC someone.