On Fri, 15 Sep 2000, Charles Galpin wrote:

> (even though the curren tversion of the script calss sendmail directly.
> 
> It would be trivial to use the script I suggested if you just dump your
> email list to a flat file (in any repectable format)
> 
> It would be a little more work to have it talk to the db.

If you use DBM's or Perl's DBI, it would be more work.  The dirty way
would be to call the mysql tool and read in its output.  Pipes rule  :)

I modified the script you linked to in another post to do just that.  It's
attached.

( A proud programmer would use Net:SMTP and Perl's DBI and proclaim these
things to be good.  They are, but I'm more interested in stuff that works
without a lot of fuss.  ;)

MSG

#!/usr/local/bin/perl

# massmail.pl
#
# This script simply mails a form letter to all the people
# in a file. It may seem a bit verbose for the task, but does
# have a few useful features:
#
# allows for customization of the message
# sets the email headers nicely (mail doesn't have to be from you)
# logs it's activity in case you need to stop and restart for some reason


# a file containing the body of the message to be sent. If the
# tag __FULLNAME__ appears anywhere in the text, the recipient's
# full name will be substituted. This same scheme can be used to
# substitute any info you get from the $listFile.

my $bodyFile;
# who the email is from
my $from = "My Name";

# and their email address
my $fromEmail = "me\@my.com";

# and the subject
my $subject = "My subject";



if( $#ARGV < 0 ) # No arguments given
{
    print "Use:  massmail.pl <email_body_filename>\n";
    exit( 1 );
}
else
{
    $bodyFile = $ARGV[0];
}

# Output will be tab separated
open( SQL, "mysql -e 'SELECT Firstname, Lastname, Email FROM subscribers' |" );
# Get rid of column names in first row.
<SQL>;

open( BODY, "<$bodyFile" ) or die "Can't open $bodyFile: $!\n";
my @body = <BODY>;
close BODY;

# initalize logging
open( LOG, ">log.out" ) or die "Can't open log.out: $!\n";
print LOG `date`;
print LOG "\n starting\n";

while( <SQL> ) {
    my( $firstname, $lastname, $recipient ) = split( /\t/ );
    my $fullname = "$firstname $lastname";
    print LOG "$to <$recipient>\n";
    $mybody = $body;
    $mybody =~ s/__FULLNAME__/$fullname/g;
    $mybody =~ s/__FIRSTNAME__/$firstname/g;
    $mybody =~ s/__LASTNAME__/$lastname/g;
    if ( &sendMail( $fullname, $recipient, $from, $fromEmail, $subject, $mybody) ) {
        #print "mailed successfully\n";
    } else {
        print "email failed $to,$toEmail\n";
        print LOG "email failed $to,$toEmail\n";
    }
}

close LOG;
close SQL;
#done

# subroutine to send email
# to, toEmail, from, fromEmail, subject, message
sub sendMail {
    my ($to, $toEmail, $from, $fromEmail, $subject, $message) = @_;

    my $sendmail = "/usr/sbin/sendmail -i -t -f ";

    if ( open( MAIL,"|$sendmail  $fromEmail" ) )
    {
        print MAIL "Reply-to: $fromEmail\n";
        print MAIL "Subject: $subject\n";
        print MAIL "From: $from <$fromEmail>\n";
        print MAIL "To: $to <$toEmail>\n";
        print MAIL "\n";
        print MAIL "$message\n";
        close (MAIL);

        return 1;
    }
    else
    {
        return 0;
    }

}

Reply via email to