I’ll describe possible ways to send emails from the command line. Of course there are much nicer ways to do it, but you may be in the situation (such as the one that drove me to do a deeper research and come out with this solution) and will need an easy and fast way to send mails from the prompt.
Simple text emails:
You can send simple emails by using the already installed sendmail program with the following command at the prompt:
echo “Simple, small body Text goes here” | mail -s “The Subject goes here” firstname.lastname@example.org
If you have already typed a message in a text file, then import the text into the body of the email you are sending by using command:
mail -s “The Subject goes here” email@example.com < text_file
Disadvantage: the email will be sent using header From: firstname.lastname@example.org
Advanced emails with attachments:
If you need to send emails containing attachments, then I’ll recommend you to use an additional program not included on the standard Linux distributions, since there it were no way I could find a way to use the build in tools to do it.
This is a mature software, very well documented and with constant development to keep up to date and using most possible features we’ll need.
WebSite: http://www.cleancode.org/projects/email (former: http://email.cleancode.org)
Create a temp folder to download the source:
tar -zxvf email-3.1.3.tar.gz
Run the following commands to have it installed:
Create the installation file:
Run the installation:
su -c ‘make install’
At the end you’ll get the program installed with:
Binary directory: /usr/local/bin
Email Files /usr/local/etc/email
Edit (with your favorite editor: vi, pico, etc.) the configuration file:
You can select to send email by SMTP or by sendmail.
I personally use sendmail for easiness.
pico -w /usr/local/etc/email/email.conf
Comment out lines: (add # at the beginning of the line)
#SMTP_SERVER = ‘127.0.0.1’
#SMTP_PORT = ’25’
Uncomment line: (remove # from the beginning of the line)
SENDMAIL_BIN = ‘/usr/lib/sendmail -t -i’
Set sender personal info with something like:
MY_NAME = ‘System Administrator’
MY_EMAIL = ‘email@example.com’
REPLY_TO = ‘firstname.lastname@example.org’
Exit and save
You got the idea, just fill out SMTP variables with proper info.
Edit the signature file:
pico -w /usr/local/etc/email/email.sig
Set it as appropriate with relevant info.
I’d recommend removing line: (to keep system privacy)
On System: %h
Exit and save
System installed and configured.
Some usage example:
Let say you are using Mailsacanner with ClamAV, and a valid attachment was removed, then a customer contacted you with the email MailScanner sent him, including the file name and info and the path were the attachment was quarantined:
It’ll read something like:
The original e-mail attachment “file_name.zip”
Note to Help Desk: Look on the E-MailSystem MailScanner in /var/spool/MailScanner/quarantine/20040730 (message i6UKnWp02396)
With that info will do:
Verify the mentioned file exists:
Execute the email command to send the attachment:
email -s “Attachment” -a file_name.zip email@example.com
Info: -s = Subject -a = Attachment
For the email body, the system default editor will open up, usually vi
Type in whatever message you want to tell:
To start typing:
Find attached the removed file.
To finish typing press:
To quit vi type:
As soon as vi exists, the email will be sent, and will give you a nice progress bar.
Read the README file for more usage options and tweaks.
I hope it helps you.
2007-12-22 Updated to use latest version as of today.
2011-06-28 Updated to use latest application version.