Uncategorized · July 30, 2004 0

Sending emails from command line

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” user@domain.com

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” user@domain.com < text_file

Disadvantage: the email will be sent using header From: root@host.domain.com

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.
Program: Email
Version: 3.1.3
WebSite: http://www.cleancode.org/projects/email (former: http://email.cleancode.org)

As root:
Create a temp folder to download the source:
mkdir email
cd email
wget http://www.cleancode.org/downloads/email/email-3.1.3.tar.gz
tar -zxvf email-3.1.3.tar.gz
cd email-3.1.3

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

For sendmail:
Comment out lines: (add # at the beginning of the line)
#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 = ‘info@domain.com’
REPLY_TO = ‘info@domain.com’

Exit and save
Ctrl x

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
Ctrl x

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:
cd /var/spool/MailScanner/quarantine/20040730/i6UKnWp02396

Verify the mentioned file exists:
decompressed_file_name.pdf file_name.zip

Execute the email command to send the attachment:
email -s “Attachment” -a file_name.zip user@domain.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:
Shift a

Dear customer:
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.
pico /path/to/download/folder/email-3.1.3/README

I hope it helps you.


Track Changes:
2007-12-22 Updated to use latest version as of today.
2011-06-28 Updated to use latest application version.