Hey there,
Today, we're going to take a look at a quick Saturday script to handle mass mailing with Perl. This is not to be confused with SPAM, although you could use it for that purpose if you're looking to trim down your social circle ;)
Actually, an older post we did on collecting mails using a CGI autoresponder is probably more suited to "Bulk Email Marketing." Although I simply delete any SPAM I get, I don't question anyone's right to fill my mailbox with junk if I sign up online to download some goofy report. I take no side in the "good vs. evil" debate, insofar as SPAM is concerned, and consider myself more of an on-looker. If it is, indeed, wrong, there is almost certainly something unpleasant waiting in the wings for the purveyors, and opting to sit on the fence can become very painful after a while ;)
In any event, I hope you enjoy (or can find some good use for) today's script. It's very simple to run, and should allow you to mass email to groups of people (who belong to your mailing list because they work at your company or requested inclusion) on any Linux or Unix variant that supports sendmail. Minor modifications should be easy enough to make if you use another mail agent.
You can execute the script with the following arguments:
host # ./listmail.pl listname messagename
with the listname being a text file consisting of one address per line, with a more descriptive name followed by the email address and separated by double-colon notation (::), like this:
Mr. Miller::abc@def.com
You can modify the split() function in the script if you prefer to delimit multiple items on single lines using another typographical character (you can also opt to change the script so it only takes an email address per line and doesn't do the split).
The messagename should be a text file which includes the body of your email message. Also, your sendmail may be in /usr/lib, rather than /usr/sbin.
Note for those who wish to do some mangling and make this script more flexible: An easy improvement to this script would be to include the From:, To:, etc lines in your message file and then pipe your print statements to "sendmail -t", rather than hardcoding it using sendmail flags, as we've done below.
Anyhow, hope you enjoy it and have a great weekend :)
This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License#!/usr/bin/perl
#
# listmail.pl - send email to folks, who want to receive email from you, more efficiently
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#
if ($#ARGV != 1) {
print "Usage: listmail listname messagename\n";
exit;
}
$maillist=$ARGV[0];
$message=$ARGV[1];
$count = 0;
open(MAILING, "<$maillist");
@mailing = <MAILING>;
close(MAILING);
foreach $mailee (@mailing) {
if ($count < 60) {
@addies = split(/::/, $mailee);
open(HEADER, ">/tmp/header.lst");
print HEADER "From: uvw\@xyz.com\n";
print HEADER "Subject: xyz.com Updates!\n";
print HEADER "Reply-To: uvw\@xyz.com\n";
print HEADER "X-cid: $addies[0]\n";
close(HEADER);
open(HEADER, "</tmp/header.lst");
@HEADER = <HEADER>;
close(HEADER);
open(SENDMAIL, "|/usr/sbin/sendmail -f uvw\@xyz.com -F \"uvw\@xyz.com\" -oi $addies[1]");
foreach $head (@HEADER) {
print SENDMAIL $head;
}
open(MESSAGE, "<$message");
@message = <MESSAGE>;
close(MESSAGE);
foreach $line (@message) {
print SENDMAIL $line;
}
close(SENDMAIL);
$count++;
} elsif ($count == 60) {
@addies = split(/::/, $mailee);
open(HEADER, ">/tmp/header.lst");
print HEADER "From: uvw\@xyz.com\n";
print HEADER "Subject: xyz.com Updates!\n";
print HEADER "Reply-To: uvw\@xyz.com\n";
print HEADER "X-cid: $addies[0]\n";
close(HEADER);
open(HEADER, "</tmp/header.lst");
@HEADER = <HEADER>;
close(HEADER);
open(SENDMAIL, "|/usr/sbin/sendmail -f uvw\@xyz.com -F \"uvw\@xyz.com\" -oi $addies[1]");
foreach $head (@HEADER) {
print SENDMAIL $head;
}
open(MESSAGE, "<$message");
@message = <MESSAGE>;
close(MESSAGE);
foreach $line (@message) {
print SENDMAIL $line;
}
close(SENDMAIL);
$count = 0;
sleep 60;
}
}
, Mike
Saturday, June 7, 2008
Using Perl To Handle Mass Mailing In Linux Or Unix
Monday, November 5, 2007
Getting Sendmail To Work In Solaris 10
Evening,
So far, it's been my experience that Solaris 10's implementation of sendmail, while adequate, doesn't seem to be suited to anything in particular out-of-the-box.
Some folks I know don't care for the whole "Submission Daemon" concept and take the short-route of copying sendmail.cf over submit.cf and just not running any daemons. This works, but you still have to make some edits and you end up with user and permissions errors without end.
In actuality, it's very simple to set sendmail up to act like it used to (assuming your shop is like most and - if the machine's not a mail server - your security department won't allow you to run it in listening daemon mode on port 25).
You can still use the submission daemon (which doesn't listen on port 25 and alert your security team) and have things set up in no time by doing the following:
1. Edit /etc/default/sendmail (note that it may not exist!). Include the following values:
MODE=Ac
QUEUEINTERVAL="15m"
2. Of course, sendmail is already running, just not sending mail anywhere no matter how many times you try, so you can shut it down with the /etc/init.d/sendmail script:
/etc/init.d/sendmail stop
Note that this basically just runs Solaris' svcadm commands for you and browsing it is a good introduction to how this method of service control works.
3. Now edit /etc/sendmail/submit.cf and change the line that reads:
D{MTAHost}[127.0.0.1]
to read:
D{MTAHost}[smtprelay.xyz.com] <--- or whatever your mail relay/server is.
4. Start sendmail back up (which enables it for future reboots by default) and you can send mail again :)
/etc/init.d/sendmail start
A quick "ps -ef|grep "[m]ail" should show you the one sendmail process running with the "-Ac" (submission daemon) and 15 minute queue-scan setting! Enjoy :)
, Mike
linux unix internet technology
Posted by
Mike Golvach
at
6:52 PM
administration, advice, scripting, sendmail, solaris, technology, tips, tricks, unix
Sunday, October 14, 2007
Using Cron To Send Out Raw Email
Hey there,
It's been a while since work hasn't gotten in the way of my blog-baby here ;) Unfortunately, I've been on-call and it's taken every moment of my time. I'll be handing off the pager here in just about an hour. Yay!!
Got a question about how to use "cron" to send out emails without calling a separate script and using a linux/unix built-in mailer. It's actually very simple to do; it just looks complicated.
For instance, if you wanted to send out an email every 30 minutes on the hour and half hour, with a Subject of "Your Regular Email" and a body with just a few lines in it, like:
"Howdy,
Hope you like this email!
, Mike"
You could just add this entry to your crontab:
0,30 * * * * (echo "Subject: Your Regular Email";echo;echo "Howdy";echo " Hope you like this email";echo;echo " , Mike")|/usr/lib/sendmail you@email.com
It's a simple concept - You create the email on the left side of the pipe (|) and use sendmail on the right side to mail that out to the address (you@email.com).
The most important thing to remember is to write out the entire contents of your email in a subshell (in parentheses). If you wrote all those echo statements outside of a subshell, only the last echo would get passed to Sendmail, and you'd get an email with no subject and a body that said:
" , Mike"
Generally, you only send mail this way for really simple stuff, but it can be as complicated as you like :)
, Mike
linux unix internet technology
Posted by
Mike Golvach
at
4:15 AM
administration, advice, cron, crontab, email, executable script, linux, raw email, scripting, sendmail, technology, tips, unix

