This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Mail & Messaging

A utility for sending complex email messages from the command line

By Ben Martin on June 06, 2008 (9:00:00 AM)

Share    Print    Comments   

When the need arises to send email from the command line, many folks first think of the mail(1) command. A better choice might be the email program, which gives you the ability to send email to an SMTP server over SSL, offers MIME support including ability to attach one or more files to your emails, uses an address book to store your recipients, and lets you digitally sign and encrypt your messages.

While there are tools to allow you to send complex email messages from Perl or PHP, being able to send email directly from the command line is handy for cron jobs and other applications where you just want to quickly send a message with an attachment.

Because the project is simply called email, I will refer to it as the "email program" in this article to avoid ambiguity. Because the project name "email" is so general I had a hard time searching for packages. I could not find any for Ubuntu or Fedora, but there is a 1-Click install for openSUSE 10.3. For this article I'll build the program from source on a 64-bit Fedora 8 machine using the usual ./configure; make; sudo make install sequence.

If you do not explicitly change the prefix or sysconfdir when running ./configure, you will find the systemwide configuration file at /usr/local/etc/email/email.conf when you finish the install. If the file ~/.email.conf exists then email will use that; otherwise it will fall back to the system configuration file. You will want to change the MY_NAME and MY_EMAIL options in your personal configuration file. If you cannot send email through localhost, you'll also have to set up the SMTP variables to point to your server and allow authentication. The default signature file includes a little blurb about being created by the email program, which you might like to disable.

$ cp /usr/local/etc/email/email.conf ~/.email.conf $ vi ~/.email.conf ... SMTP_SERVER = '127.0.0.1' SMTP_AUTH='PLAIN' SMTP_AUTH_USER='foo' SMTP_AUTH_PASS='bar' USE_TLS='true' ... MY_NAME = 'Sysadmin number one' MY_EMAIL = 'sadmin@example.com' ... #SIGNATURE_FILE = '&/email.sig' #SIGNATURE_DIVIDER = '---' ...

Once you have the software set up, you can send email messages from the command line using the email command. The first email below is sent in a very similar manner to the way the mail(1) program works. I specify the subject and recipient on the command line, then read the body of the message from the user before sending the message. Instead of the message being read in directly from the console as mail(1) does, the email program will run your nominated text editor to allow you to compose your message. In the second example below I send the current time as the message body with a subject line describing what happened at that time.

$ email --subject test1 ben@localhost email: WARNING: Deprecated variable: SIGNATURE_DIVIDER email: WARNING: Environment varaible EDITOR not set: Defaulting to "vi" hello there ZZ $ date | email --subject "Bad things happen at night" ben@localhost

The email program also lets you use an address book, which can be handy if you are using email to send out notifications and your email address changes, or you wish to send a message to a group instead of just yourself. The address book format allows for single or multiple recipient addresses. It is a good idea to always start out using addresses from the address book in scripts, using the specific task that the script is performing as the address in your address book. For example, a database backup script might send to the "database-archive" address from your address book. You can then easily change who will see such messages by changing your address book and not have to look at the scripts that send the email messages at all.

Recipients in the address book start with the tag single: or group:. Single email addresses are in the form name = email-address. Multiple email addresses are specified in a comma-separated list in a group email definition. The README file for email states that every recipient in a group address must be defined as a single address; that is, you cannot directly specify email addresses as recipients in a group definition. So, as shown below, the group address "us" references to single addresses "moi" and "anata" instead of using the email addresses like root@localhost directly.

$ vi ~/.email.conf ... ADDRESS_BOOK = '~/.email.address' .. $ cp /usr/local//etc/email/email.address.template ~/.email.address $ vi ~/.email.address ... single: moi = ben@localhost single: anata = root@localhost group: us = moi, anata ...

By default the address book names that you use show up in the email messages that are sent. As you can see in the below message, because I sent to the address "moi," the To line in the resulting email contains "moi" <ben@localhost.localdomain>. If you want to keep the address book names and recipients from knowing who else is getting the messages, you can send the message to a single target address and use the --bcc option to blind carbon copy the message to a group. This will also keep the address book name that was used in the bcc target from being sent to the recipients.

$ date | email --subject "linux.com, eh?" moi $ vi /var/spool/mail/ben ... From yourlogin@example.com Tue May 27 12:33:57 2008 Return-Path: <yourlogin@example.com> Received: from v8tsrv (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.14.1/8.14.1) with ESMTP id m4R2Xv8w014763 for <ben@localhost>; Tue, 27 May 2008 12:33:57 +1000 Message-Id: <200805270233.m4R2Xv8w014763@localhost.localdomain> Subject: linux.com, eh? From: "Your Real Name" <yourlogin@example.com> To: "moi" <ben@localhost.localdomain> Date: Tue, 27 May 2008 12:33:57 +1000 Content-Type: text/plain X-Mailer: Cleancode.email v3.0.5 Tue May 27 12:33:57 EST 2008 ... $ date | email --subject "Singing and dancing monkies?" us From yourlogin@example.com Tue May 27 12:35:52 2008 Return-Path: <yourlogin@example.com> Received: from v8tsrv (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.14.1/8.14.1) with ESMTP id m4R2ZqEq014772; Tue, 27 May 2008 12:35:52 +1000 Message-Id: <200805270235.m4R2ZqEq014772@localhost.localdomain> Subject: Singing and dancing monkies? From: "Your Real Name" <yourlogin@example.com> To: <ben@localhost.localdomain>, "anata" <root@localhost.localdomain> Date: Tue, 27 May 2008 12:35:52 +1000 Content-Type: text/plain X-Mailer: Cleancode.email v3.0.5 Tue May 27 12:35:52 EST 2008 $ date | email --subject "You can't tell the group" --bcc us moi # vi /var/spool/mail/root ... From yourlogin@example.com Tue May 27 12:39:13 2008 Return-Path: <yourlogin@example.com> Received: from v8tsrv (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.14.1/8.14.1) with ESMTP id m4R2dDKY014832; Tue, 27 May 2008 12:39:13 +1000 Message-Id: <200805270239.m4R2dDKY014832@localhost.localdomain> Subject: You can't tell the group From: "Your Real Name" <yourlogin@example.com> To: "moi" <ben@localhost.localdomain> Date: Tue, 27 May 2008 12:39:13 +1000 Content-Type: text/plain X-Mailer: Cleancode.email v3.0.5 Tue May 27 12:39:13 EST 2008 ...

You can attach a file to a message by using the --attach command-line option and specifying the path of the file to attach. You can use the --attach option multiple times to include many files.

Use the --sign and --encrypt options to digitally sign and encrypt a message before sending. I found that the --sign option attempted to digitally sign the message using the private key of the first recipient rather than the private key of the message sender. For example, if sending from sadmin@localhost to ben@hawaii.example.com, the email program would try to use the private key of ben@hawaii to sign the message. This would normally fail because only the private key of sadmin@localhost is available on the machine. Of course if you always send to sadmin@localhost as the first address, then digital signing works as expected because the program looks for the correct key.

You can also override the SMTP server and authentication information, as well as the path of the configuration file, by using command-line options.

The email program is fairly quick to install and set up. The ability to send messages using an arbitrary SMTP server and the support that the email program has for connecting to an SMTP server using SSL let you send messages through whatever outgoing server you like. The support for attachments, encryption, and an address book makes sending email from the command line simple for automated scripts such as cron jobs. Having error reports from such scripts encrypted and signed also protects you against pranks and allows such reports to be sent through public email servers while remaining confidential.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   

Comments

on A utility for sending complex email messages from the command line

Note: Comments are owned by the poster. We are not responsible for their content.

A utility for sending complex email messages from the command line

Posted by: Anonymous [ip: 65.89.151.33] on June 06, 2008 03:41 PM
another similar tool is sendemail, a smtp agent written in Perl:

http://freshmeat.net/projects/sendemail/

#

A utility for sending complex email messages from the command line

Posted by: Anonymous [ip: 81.214.210.70] on June 06, 2008 05:07 PM
<a href="http://duplicity.nongnu.org/" target="_blank">http://duplicity.nongnu.org/</a>

#

A utility for sending complex email messages from the command line

Posted by: Anonymous [ip: 62.192.145.30] on June 07, 2008 04:29 PM
I can’t install on FreeBSD.

$ make
cd dlib/ && make
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dnet.c
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dhash.c
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dlist.c
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dstrbuf.c
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dutil.c
gcc -g -O2 -Wall -W -I/root/scripts/email/email-3.0.5/dlib -I/root/scripts/email/email-3.0.5/dlib/src -I/root/scripts/email/email-3.0.5/dlib/include -I../include -DHAVE_CONFIG_H -c src/dvector.c
ar rc libdlib.a src/dnet.o src/dhash.o src/dlist.o src/dstrbuf.o src/dutil.o src/dvector.o
ar: 'src/dnet.o': No such file
ar: 'src/dhash.o': No such file
ar: 'src/dlist.o': No such file
ar: 'src/dstrbuf.o': No such file
ar: 'src/dutil.o': No such file
ar: 'src/dvector.o': No such file
cd src/ && make
...
...
...
...
...
...
utils.o(.text+0x3ed):/root/scripts/email/email-3.0.5/src/utils.c:232: undefined reference to `dlDestroy'
utils.o(.text+0x43d):/root/scripts/email/email-3.0.5/src/utils.c:154: undefined reference to `dsbDestroy'
utils.o(.text+0x47e): In function `copyUpTo':
/root/scripts/email/email-3.0.5/src/utils.c:274: undefined reference to `dsbnCat'
*** Error code 1

Stop in /root/scripts/email/email-3.0.5/src.
*** Error code 1

Stop in /root/scripts/email/email-3.0.5.

#

Think using the wording "complex" is misleading

Posted by: Anonymous [ip: 124.212.215.50] on June 09, 2008 09:12 AM
This "email program" does not support multipart/alternative syntax. So no sending both text and html. And no support for charset other than English. Tried contacting the author, but his contact form seems to be inoperable. Would like to add a few things to the TODO list.

BTW my contact form does work!! So if you can Dean drop me an email.

Kind Regards,
Dave Faulkmore
http://www.usguyintokyo.com

#

Slight retraction

Posted by: Anonymous [ip: 124.212.215.50] on June 10, 2008 03:36 AM
After much head scraching, dawned on me to try the --header option. Supplied the Content-Type for multipart/alternative eventhough there is already a Content-Type header line present. So this added charset support and support for multipart emails. Haven't tried to add an attachment and change the Content-Type. Suspect would still be able to send text/html but would have to make a few adjustments.

So i'm proud to say got Japanese plain text working and Japanese plain text and html working.

Still can't email Dean. Lets start a "Where's Dean" movement. Maybe that will draw him out of the closet...

Kind Regards,
Dave Faulkmore
http://www.usguyintokyo.com

#

Similarities with Sendemail...

Posted by: Anonymous [ip: 69.156.68.30] on June 25, 2008 02:36 PM
however... sending emails with this may be a lot simpler since it supports attachments and you can export the address book easily... I am still getting into linux, takes some time to learn... I'll get there.


- KG Lew
<a href="http://www.oneyearmillionaire.net">One Year Millionaire.net</a>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya