Web Hosting Talk







View Full Version : Creating a newsletter program


marco
06-30-2001, 07:46 AM
Hi!
I hope this is the right place to talk about this.
I am currently using a program on my website which delivers every night a message to 6500+ persons (growing)
The program is written in Perl, and it's quite simple: after having extracted emails from the DB it enters a "for" loop which calls a small procedures which:

a) opens a pipe with the sendmail program:
open(MAIL, "| /usr/sbin/sendmail -t");
b) executes these instructions:
print MAIL "To: <$recipient>\n";
print MAIL "From: \"me\" <me\@host.com>\n";
print MAIL "Subject: $subject\n";
print MAIL "\n";
# subject
print MAIL "$body\n";
c) closes the pipe:
close(MAIL);
d) exits the procedure and goes back to the "for" loop

Should it be done better? Or every package out there (such as GNU mailman, majordomo, etc.) works this way? If not, how?

Please help, I'm looking for a new host and I've been told that the execution of this script in the crontab is conditioned upon how the script is written.

Any suggestion to improve speed and reduce server load? Or is it good enough? I don't want to use standard mailing list packages.
Thanks in advance.

Best regards, :confused:

marco
06-30-2001, 10:33 AM
It's better if I post the code here, so that anybody can see...
#!/usr/bin/perl

#################
# sub send_mail #
#################

sub send_mail {
my $recipient;
my $subject;
my $body;

($recipient, $subject, $body) = @_;

$mailprog = "/usr/sbin/sendmail";

open(MAIL, "| $mailprog -t");

print MAIL "To: <$recipient>\n";
print MAIL "From: \"me\" <me\@host.com>\n";
print MAIL "Subject: $subject\n";
print MAIL "\n";

print MAIL "$body\n";

close(MAIL);
}

########
# main #
########

$FILE = "/usr/home/marco/myprogs/update/data/mail." . `date +%Y%m%d`;
chop($FILE);
$BAN = "/usr/home/marco/myprogs/update/data/ban." . `date +%Y%m%d`;
chop($BAN);

$DAILY_LOG = `uptime` . "\n";
$DAILY_LOG .= "Begin: " . `date` . "\n";
$DAILY_LOG .= "Invocation: \"$0\"\n";

if (-f "$BAN") {
$DAILY_LOG .= "Ban found, skipping\n";
$DAILY_LOG .= "End: " . `date` . "\n";

send_mail("webmaster\@host.com", "Daily log", "$DAILY_LOG");

exit(1);
}

if (-f "$FILE") {
$TITOLO = `head -n 1 "$FILE"`;
$TESTO = `tail -n +2 "$FILE"`;

@output_mysql = `echo "SELECT email FROM users" | /usr/local/bin/mysql -u db_user --password=bigsecret db`;
for ($user = 1; $user <= $#output_mysql; $user++) {
chop $output_mysql[$user];
send_mail("$output_mysql[$user]", "$TITOLO", "$TESTO");
}
$DAILY_LOG .= "Mail sent\n";
}
else {
$DAILY_LOG .= "Mail not sent, message file missing\n";
}

$DAILY_LOG .= "End: " . `date` . "\n";
$subscribers = `echo 'SELECT COUNT(*) FROM users' | /usr/local/bin/mysql -u db_user --password=bigsecret db | grep [0-9]`;
$DAILY_LOG .= "Users reached: $subscribers\n";

send_mail("webmaster\@host.com", "Daily log", "$DAILY_LOG");

exit(0);


Thanks. :)