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 "firstname.lastname@example.org" using "subject of the email" as the subject:
echo 'some message' | mail -s 'subject of the email' email@example.com
Let's CC someone else:
echo 'some message' | mail -s 'subject of the email' -c firstname.lastname@example.org email@example.com
Now let's BCC someone else:
echo 'some message' | mail -s 'subject of the email' -c firstname.lastname@example.org -b email@example.com firstname.lastname@example.org
If you already have your message save in a text file, you can use the following syntax:
mail -s 'subject' email@example.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 firstname.lastname@example.org
The same parameters are used in mutt to CC or BCC someone.