Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Sending Custom Mass Mail - Fastest Way Possible

by stevieb (Canon)
on May 29, 2015 at 15:44 UTC ( [id://1128306]=note: print w/replies, xml ) Need Help??


in reply to Sending Custom Mass Mail - Fastest Way Possible

You can use the built-in fork() to do what you want. It'll allow you to run multiple processes simultaneously, while injecting your template info into each distinct email message.

Set up the %email hash with the template information for each user, then change the print/sleep operations in start(), write some logic to populate the template and send the email.

#!/usr/bin/perl use warnings; use strict; my %email = ( ken => { name => 'Ken May', addr => 'kenm@hello.com', }, suzie => { name => 'Suzie Sheer', addr => 'ss@hello.com', }, steve => { name => 'Stevie B', addr => 'sb@hello.com', } ); my @children; for my $key (keys( %email )){ my $pid = fork(); if( $pid ){ print "PID $pid forked: $key\n"; push @children, $pid; } else { my $proc = start($key); exit($proc); } } for my $child (@children){ my $pid = waitpid($child, 0); print "PID $pid exited...\n"; } sub start { my $key = shift; print "$key Starting...\n"; print "$email{$key}->{'addr'}, $email{$key}->{'name'}\n"; sleep(5); print "$key Ending...\n"; }

-stevieb

Replies are listed 'Best First'.
Re^2: Sending Custom Mass Mail - Fastest Way Possible
by edimusrex (Monk) on May 29, 2015 at 21:15 UTC
    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; }

      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.
Re^2: Sending Custom Mass Mail - Fastest Way Possible
by edimusrex (Monk) on May 29, 2015 at 15:48 UTC
    Yea, that seems like a good option. So maybe ingest my email list from the database into a hash first on fork from there.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1128306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-18 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found