PDA

View Full Version : Sendmail sending multiple emails all on its own


vids
07-13-2005, 04:48 PM
Hi,

I have a really strange issue with a Perl script that sends out a HTML email to a mailing list.

When the script is executed and the emails are sent, I have some users reporting that they are reciving upto 39 emails instead of one (this number is not consistant, I have other users reporting 10 emails, etc.)

A mailing list of names and email addresses is taken from a MySQL database.

The script iterates through the list opening sendmail outputting the HTML email to it and then closing it.

I have tested it with 200 hundread entries of my own email address, and as expected I recieve exactly 200 hundread emails.

I have spoken to my hosting supplier and they assure me that there is nothing wrong at their end.

The following code builds my mailing list from the database:

my @recipients = ();
my $usersQuery="SELECT distinct email,first_name FROM tbl where optin='y'";
my $sth = $dbh->prepare($usersQuery);
$sth->execute();
while (my $row = $sth->fetchrow_hashref) {
push @recipients, join('|',$row->{first_name},$row->{email});
}
$sth->finish();

The following code iterates through the list:

foreach my $item (@recipients) {
# print $item."<br>";
my ($name,$email) = split(/\|/,$item);
SendEmailMessage($name,$email,"Subject",$cgi);
}

The following code outputs the HTML to sendmail:

sub SendEmailMessage {

my ($to_name,$to_address,$subject,$cgi) = @_;

my $fileContents = getAttachedFile($cgi->param('pdf_filename'));

unless(open (MAIL, "|/usr/sbin/sendmail -t")) {
print "Could not open the sendmail program, $!\n<br>";
exit;
}

############### Email address ###############
print MAIL "From: marketing\@totalgreekyoghurt.com\n";
print MAIL "To: $to_address\n";
print MAIL "Subject: $subject\n";
##############################################

############## set up mime types #############
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-type: Multipart/mixed; boundary=\"Message-Boundary-19990614\"\n";
print MAIL "\n";
print MAIL "\n";
##############################################

######## Add the text part of the message #####
print MAIL "--Message-Boundary-19990614\n";
print MAIL "Content-type: text/plain; charset=US-ASCII\n";
print MAIL "Content-description: Mail message body\n";
print MAIL "Content-transfer-encoding: 7BIT\n";
print MAIL "\n";
print MAIL "Your Kalimera Newsletter from Total Greek Yoghurt\n\n";
print MAIL $cgi->param('message')."\n\n";
print MAIL "From Total Greek Yoghurt\n\n";
print MAIL "If you want to find out more about Greece and all things Greek\n";
print MAIL "Visit: http://www.totalgreekyoghurt.com/\n\n";
print MAIL "If you ever wish to unsubscribe please reply to this email with 'UNSUBSCRIBE' in the subject heading\n";
print MAIL "--Message-Boundary-19990614\n";
################################################

#### print the header and attachment ################
print MAIL "Content-Type: application/octet-stream; name=\"".$cgi->param('pdf_filename').".pdf\";\n";
print MAIL "Content-Disposition: attachment; filename=\"".$cgi->param('pdf_filename').".pdf\"\n";
print MAIL "Content-Transfer-encoding: BASE64\n\n";

$fileContents= encode_base64($fileContents); ## Use base64 for encoding the contents

print MAIL $fileContents;
print MAIL "\n";
# print MAIL "--Message-Boundary-19990614-\n";
######################################################

close(MAIL);

}

I have also ensured the following:

Each email address exists only once in the database (this was the first thing I checked)

Any help or pointers in the right direction that can be offered will be much appreciated.

Thanks

Jeff Mott
07-14-2005, 01:13 AM
I can't see the issue right away, so let's walk through some debugging steps. We're going to work with the real script and the real database, so first thing is to change the sendmail function so that the mail messages is printed to a file or to the screen so people don't get a bunch of emails while we test things.

Next, we're going to put in a bunch of print statements. Print out the data you get from the database. Print out the contents of @recipients after you have loaded it. Finally, if you've been printing the mail message to files, check those message headers against your database and @recipients data to make sure it is all consistent.