Historical Project Notice: This project is over 20 years old (from university years). Please visit Projects to see my current projects.
One hot afternoon, I was assigned the job to e-mail a bunch of people and asked them to do a survey. Since those are all busy people, we thought it would be nice to personalize each one instead of a generic dear sir/madam...
Personalize E-mail with Perl Script
For those who have been in cs (Computer Science not Counter Strike!) for a while, this is probably pretty easy. But to promote the use of perl (it's a nice scripting language!), I thought it would be nice to show how this task can be done in this concise scripting language. You can think of this as a brief perl tutorial or a simple perl example.
Pre-requisite
This is done on a unix/linux system, where perl is pre-installed. The script is also dependent on the standard mailing program "sendmail". Theoretically, this can also be done on Windows, with a perl interpretor and a mailing tool.
Intended Audience
This is intended for people who have programming background. I remember the days when I got frustrated when reading some books on scripting languages that cover stuff like how to write a for loop. I think I know how to write a loop if I have programming background thank you very much.
Now I will start discuss the script bit by bit. If you prefer, you can download the complete script first.
#!/usr/local/bin/perl
-w
$sendmail
= '/usr/lib/sendmail -i -t';
$sender
= 'yourname@address.com';
open(CONTACTLIST,'contactlist.txt');
$count=0;
while(<CONTACTLIST>){
chomp;
$nameList[$count++]=$_;
$_=<CONTACTLIST>;
chomp;
$nameList[$count++]=$_;
}
%nameList=@nameList;
close(CONTACTLIST);
open(MESSAGE,'message.txt');
@entireMessage=<MESSAGE>;
while(($recipient,
$address)=each(%nameList)){
#print "Key: $Key, Value: $Value\n";
$mailbody="Dear $recipient,\n\n";
foreach(@entireMessage){$mailbody.=$_;}
print $mailbody;
open(MAIL, "|$sendmail -oi -t")
;
print MAIL "To: $address\n";
print MAIL "From: $sender\n";
print MAIL "Subject: AddSubjectLine\n\n";
print MAIL "$mailbody";
close(MAIL);
}
Last words:
For those who have done some programming, you may realize what the script is doing is terribly inefficient - for example, it reads the entire file to an array, and then convert the array into a string, and then convert it back, etc. Clearly, this is a contrived example meant to show you some basics of perl. Feel free to contact me if you have any questions.