Simple E-mail Merge

We all know the ‘mail merge’ function that comes with allmost every wordprocessor. You compose one letter you want to send to a group of individuals and their names and addresses are inserted in each letter automatically from a list. I needed such a mail merge for an e-mail I wanted to send. I found that none of the e-mail clients I use supported an ‘e-mail merge’. After some googling I found Tobin Maginnis’ Simple Mail List Shell Script.

This was exactly what I needed. Well, allmost. I needed a slightly more sophisticated approach to name and e-mail management. Also I wanted to be able to insert the subject from the command line instead from the source of the program. So I wrote Simple E-mail Merge (sem). It should run on any posix system. I use msmtp to send the mail via the smtp server of my internet provider.

You invoke sem like this:

sem maillist message "My subject line"


The file ‘maillist’ might look something like:


Bill,hisemailaddress@sample.comm
Steve,sample@address.comm
Larry & Sergey,anothersample@address.comm


The ‘message’ file contains the letter you want to send.

Here’s the code. Please make sure to read my software notice before using this.

#!/bin/bash
#
# Simple E-mail Merge (sem)
#
# September 2007
# v.0.4
# by Jeroen Leijen
# http://jeroen.leijen.net
#
# based on Tobin Maginnis' Simple Mail List Shell Script
# http://pix.cs.olemiss.edu/mailscript.html
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# The full text of this software license is at: http://www.gnu.org/licenses/gpl.txt
# or to receive a written copy of the GNU General Public License, write to
# the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
#
if [ ${#@} != 3 ]; then
echo "Usage: $0 MailList Letter Subject(in quotes) "
else
if [[ -e "$1" && -e "$2" ]]; then
echo "Sending $2 with subject $3 to mail list: $1 ..."
while read line ; do
name=`echo $line| cut -d ',' -f 1`
email=`echo $line| cut -d ',' -f 2`
echo "Mailing: $name"
echo "with e-mailaddress: $email"
#You can change 'Dear' in the next line to whatever you need
echo "Dear ${name}," > tempmail$$
cat $2 >> tempmail$$
mail -s "`echo $3`" `echo $email` < tempmail$$
#if you like a bcc copy use this line
#mail -s "`echo $3`" -b your_bcc_address@mail.comm `echo $email` < tempmail$$
done < $1
rm tempmail$$
echo "Finished mail list: $1"
else
echo "Cannot find mail list file: $1 and/or letter file: $2"
fi
fi

Leave a Reply