SPUG: sending emails

Jacinta Richardson jarich at perltraining.com.au
Wed Feb 21 21:11:05 PST 2007


G'day Luis,

This code seems to be showing its age a little.  It has some interesting failure
cases ( $content contains "\n.\n" anywhere; user writes something naughty for
$title, or the name of the content file... ) and doesn't always check that
things work (failure on opening sendmail never gets considered).  You also seem
to be missing "strict" and warnings.

Perhaps the following might suit you better (untested, but probably ok)


#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite;
use Fatal qw(open close);

# Get list of email addresses
my $addr_file = "tot.txt";
open(my $addr_fh, "<", $addr_file);
my @addresses = <$addr_fh>;
close $addr_fh;

# Read email content
my $cont_file = shift @ARGV or die "error: give the name of the file content";
open(my $content_fh, "<", $cont_file);
my $content = do { local $/; <$content_fh> };
close $content_fh;

# Get email subject
print "subject of the email\n";
my $subject=<STDIN>;
chomp($subject);

# Iterate over emails, sending to roughly half of them (random)
foreach my $email (@addresses) {
	chomp $email;

	if( int(rand(2)) == 1 ) {
	{
		my $msg = MIME::Lite->new(
			From     => 'posting at mailserver',
			Type     => 'text/plain',
			Encoding => 'quoted-printable',
			To       => $email,
			Subject  => $subject,
			Data     => $content,
		);
		$msg->send;
	}
	sleep(5);
}


All the best,

	Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |


More information about the spug-list mailing list