in reply to Re: Sending Custom Mass Mail - Fastest Way Possible
in thread Sending Custom Mass Mail - Fastest Way Possible

Thank you for that suggestion! Man, that really sped up my script. I had to make some server side adjustments to exim but with forking the script now completes in less than a minute where it was taking well over 15 before. Here is what the adjusted code looks like

#!/usr/bin/perl use strict; use warnings; use MIME::Lite; use HTML::Template; use DBI; use Cwd qw( abs_path ); use File::Basename qw( dirname ); use Config::Properties; use Getopt::Long; use Term::ANSIColor; use Parallel::ForkManager; my $pm = new Parallel::ForkManager(40); if (scalar $#ARGV == -1) { &usage(); } my (%props,%emails); &properties(); my ($mail,$list,$remove,$add); GetOptions( 'mail' => \$mail, 'list' => \$list, 'remove' => \$remove, 'add' => \$add, ) or die &usage(); my %connect = ( 'database'=>$props{MySQL_Database}, 'host'=>$props{MySQL_Host}, 'port'=>$props{MySQL_Port}, 'user'=>$props{MySQL_User}, 'password'=>$props{MySQL_Password}, 'file'=>$props{HTML_File}, ); my $dsn = "DBI:mysql:database=$connect{database};host=$connect{host};p +ort=$connect{port}"; my $dbh = DBI->connect( $dsn, $connect{user}, $connect{password} ) or +die "Failed to connect to the database: " . DBI->errstr; my $sql = qq|SELECT `emailAddress`, `firstName` FROM $connect{database +}.`users` WHERE `status` = (SELECT `Id` FROM $connect{database}.`assc +_status` WHERE `Status` = 'PROVISIONAL') AND `blacklisted` = (SELECT +`Id` FROM $connect{database}.`assc_blacklist` WHERE `Blacklisted` = ' +No') AND `unsubscribed` = (SELECT `Id` FROM $connect{database}.`assc_ +unsubscribed` WHERE `Status` = 'No')|; my $sql_remove = qq|UPDATE $connect{database}.`users` SET `unsubscribe +d` = (SELECT `Id` FROM $connect{database}.`assc_unsubscribed` WHERE ` +Status` = 'Yes') WHERE `emailAddress` = ?|; my $sql_add = qq|UPDATE $connect{database}.`users` SET `unsubscribed` += (SELECT `Id` FROM $connect{database}.`assc_unsubscribed` WHERE `Sta +tus` = 'No') WHERE `emailAddress` = ?|; if($mail) { print "This option will email all subscribed users. Are you sure +you would like to continue? : (yes|no) "; chomp(my $res = <>); if ($res !~ /^yes$/) { print "Closing Script\n"; exit; } my $sth = $dbh->prepare($sql); $sth->execute or die "Failed to execute query:$!"; my $file = HTML::Template->new(filename => $connect{file}); while (my $result = $sth->fetchrow_hashref) { my $fname = $result->{firstName}; my $email_address = $result->{emailAddress}; $emails{$email_address} = $fname; } $dbh->disconnect; for my $x (keys %emails) { my $pid = $pm->start and next; $file->param(USER_NAME => $emails{$x}); my $proc = sendMail($x,$file->output); exit($proc); $pm->finish; } $pm->wait_all_children; exit; } if($list) { my $sth = $dbh->prepare($sql); $sth->execute or die "Failed to execute query:$!"; while (my $result = $sth->fetchrow_hashref) { print "$result->{emailAddress}\n"; } $dbh->disconnect; exit; } if($remove) { my $ans = 1; while($ans) { print "Enter email address you would like to remove from list +: "; chomp(my $em = <>); my $sth = $dbh->prepare($sql_remove); $sth->execute($em) or die "Failed to execute query:$!"; print "$em has been unsubscribed!\n\nWould you like to remove +another user? : (yes|no) "; chomp(my $res = <>); if (lc $res !~ /^yes$/ ) { $ans = 0; } } $dbh->disconnect; exit; } if($add) { my $ans = 1; while($ans) { print "Enter email address you would like to add to list : "; chomp(my $em = <>); my $sth = $dbh->prepare($sql_add); $sth->execute($em) or die "Failed to execute query:$!"; print "$em has been subscribed!\n\nWould you like to add anoth +er user? : (yes|no) "; chomp(my $res = <>); if (lc $res !~ /^yes$/ ) { $ans = 0; } } $dbh->disconnect; exit; } sub sendMail{ my $subject = "Good ole subject line"; my $to = shift; my $body = shift; my $msg = MIME::Lite->new( From => 'email@email.com', To => $to, Subject => $subject, Type => 'text/html', Data => $body, ) or die "Error creating multipart container: $!\n"; $msg->send or die "Failed To Send!: $!\n"; print "Message sent to - $to\n"; } sub properties { open my $fh, '<', dirname(abs_path($0))."/mailer.props" || warn "F +ailed to open :$!"; my $properties = Config::Properties->new(); $properties->load($fh); %props = $properties->properties; return; } sub usage { print color("yellow"), "\n$0 Usage :\n", color("reset"); my $message = <<EOF; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++ + + + + + --mail -:- Send weekly email to all users subscribed + + + + + + + + --list -:- List subscribed users by email + + + + + + + + --remove -:- Unsubscribe user from email, requires you to + enter the email address + + + + + + + --add -:- Add user to subscribed list + + + + + + + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++ EOF print $message; exit; }

Replies are listed 'Best First'.
Re^3: Sending Custom Mass Mail - Fastest Way Possible
by stevieb (Canon) on May 29, 2015 at 21:18 UTC

    Awesome. Glad it was of help. I develop professionally in Python now, so it's nice to keep somewhat fresh with Perl by trying to help out here.

    Cheers,

    -stevieb

      I've dabbled a little in python but still more comfortable in perl. Still have a ways to go though. Thanks again.