Icon of a thin page Icon of a thick page

Howto: SPAM^W Email in Bulk

David A. Harding

I just had to send out a bunch (20) emails for a GPG Keysigning Party I held last night at LUG/IP. I tried sending everything out using a Bcc: line but, somewhere in the works, the email got lost. I still didn't want to create a big To: line, so I broke out some shell scripting.

First, you want to correctly configure your Mail Transport Agent (MTA). Everyone (that's you!) should do this anyway so output from their cron jobs, and the like, reach them. To configure your MTA, check your distro's documentation or query your favourite mailing list with the name of your distro and a request for assistance.

All Linux Standard Base (LSB) compliant distros put a symlink to the default MTA from /usr/lib/sendmail. We'll use this command.

Finally, you should know about one switch supported by all sendmail-compliant mailers, the -t switch. sendmail -t will read a file on standard input and proccess it based on the headers: i.e. sending mail to the people listed in the To: and Cc: headers and privately sending mail to the people in the Bcc: header.

Let's try a simple test. Write the following lines to a file called, email.mbox:

From: David A. Harding <dave@dtrt.org>
To: foo@gnuisance.net
Subject: This is a test

This is a test.

Save the file and run the following command:

cat email.mbox | /usr/lib/sendmail -t

Check your email in a few moments, and you should find a copy of that email. If it didn't work, ask someone (me) for help.

Now, for the uber-geek mailmerge: create another file, addys.txt with something like this as contents:

 Dave Harding <dave@dtrt.org>
 Another Dave Harding <devnull@gnuisance.net>
 Example.com <example@example.com>
 

And rewrite your email to look like the example below:

From: David A. Harding <dave@dtrt.org>
To: @TO
Subject: This is a test

This is a test.

Now use run this command line (you may paste this as-in into bash):

for e in `cat addys.txt` 
do 
	sed -e s/@TO/$e/ email.mbox | /usr/lib/sendmail -t 
done

The result will be each participant receiving an individually addressed email. Make sure you don't use @TO for anything else in the file!

* * *

An alternative (and a good one for repeated correspondence) is to use alias functionality of sendmail-compatible MTAs. Briefly, you edit the file /etc/alias to read something like this:

## GNUisance Off-Topic
gnuisance-ot: dave@dtrt.org, devnull@gnuisance.net, example@example.com

Run the command newaliases(8) and any email sent through /usr/lib/sendmail to the address gnuisance-ot on the same machine will be sent to the above three email addresses.

The receipiants will only see that the message is addressed to gnuisance- ot—not each other's email addresses.

To do this, you need to correctly configure your MTA (another good reason to do so) and you need to make sure your Mail User Agent (MUA) like Mozilla or Evolution sends mail using /usr/lib/sendmail (a good idea anyway because sendmail compatible MTAs normally keep logs). Nearly every free software MTA for GNU/Linux can send mail using /usr/lib/sendmail.

This blog originally appeared as two responses to a query on the CHLUG mailing list.