frank1 has asked for the wisdom of the Perl Monks concerning the following question:

I have my following code below to send notifications, newsletters to my registered customers and its working well

my Question is that, is it really efficient and effective to send out mails to over 100k customers emails

 CGI CODE
#!/usr/bin/perl -wT use strict; use warnings; use CGI qw/:standard/; print "Content-type: text/html\n\n"; print <<HTML; <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>mail sending with progressbar-jquery-ajax-</title> <link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightn +ess/jquery-ui.css" rel = "stylesheet"> <script src = "https://code.jquery.com/jquery-1.10.2.js"></scrip +t> <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js">< +/script> <style> .ajax-loader { display: inline-block; width: 20px; height: 20px; border: 8px solid #204d74; border-right-color: transparent; border-radius: 50%; animation: spin 1.3s linear infinite; } \@keyframes spin { from { transform: rotate(0deg); opacity: 0.3; } 50% { transform: rotate(180deg); opacity: 1.0; } to { transform: rotate(360deg); opacity: 0.3; } } </style> <script> \$(document).ready(function () { \$("#Submit").click(function(event) { if (\$("form input[name='sub']").val() == "") { \$('#error').text("Subject Required"); return false; } if (\$("form textarea[name='msg']").val() == "") { \$('#error').text("Message Required"); return false; } Execute(); \$('#error').hide(); \$('#progress_start').show(); }); function Execute(){ \$.ajax({ xhr: function() { var xhr = new window.XMLHttpRequest(); //Upload progress xhr.upload.addEventListener("ajax-loader", function(evt){ if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //Do something with upload progress console.log(percentComplete); } }, false); //Download progress xhr.addEventListener("ajax-loader", function(evt){ if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //Do something with download progress console.log(percentComplete); } }, false); return xhr; }, type: 'POST', url: 'mail.pl', data: { 'sub': \$("input[name='sub']").val(), 'msg': \$("textarea[name='msg']").val() }, success: function(res){ \$('#progress_start').hide(); \$('#Response').html(res.msg); }, error: function() { alert("Failed To send Message"); } }); }; }); </script> </head> <body> <br> <form method="post"> <input type="text" id="sub" name="sub" placeholder="Subject"><br><br +> <textarea id="msg" name="msg" placeholder="Message"></textarea><br>< +br> <input type="button" id="Submit" value="Submit"> </form> <div id='progress_start' style='display:none'> <p>Running the task<span class="ajax-loader"></span></p> </div> <div id="Response"></div> <div id="error"></div> </body> </html> HTML
 Mail.pl code
my $sub = $Cgi->param("sub"); my $msg = $Cgi->param("msg"); my $Response; if ($sub) { my $select_emails = $dbh->prepare("SELECT DISTINCT email FROM testuser +"); $select_emails->execute(); my $records = $select_emails->fetchall_arrayref; my @to; for my $em ( @$records ) { push @to, @$em; } for my $recipient(@to) { mail($recipient); } sub mail { my $recipient = shift; my $smtpserver = 'smtp.xxxxxx.com'; my $smtpport = 587; my $smtpuser = 'xxxxx'; my $smtppassword = 'xxxxxx'; my $transport = Email::Sender::Transport::SMTP->new({ host => $smtpserver, ssl => 'starttls', port => $smtpport, sasl_username => $smtpuser, sasl_password => $smtppassword, }); my $email = Email::Simple->create( header => [ To => $recipient, From => 'xxxxx@xxxx.com', Subject => $sub, 'Content-Type' => 'text/html', ], body => $msg, ); sendmail($email, { transport => $transport }); $Response = "Done sending"; } my $json = encode_json( { msg => $Response } ); print $Cgi->header( -type => 'application/json' ),$json; } $dbh->disconnect;

Replies are listed 'Best First'.
Re: Mail sending
by hippo (Archbishop) on Aug 03, 2023 at 12:49 UTC

    No, it isn't efficient. Creating 2 new objects for each message is not efficient. Creating new, identical messages for each recipient is not efficient. Sending bulk email without setting the relevant Precedence is going to get you in a lot of trouble also. Did you tell your hosting provider you were going to be doing this? If not, prepare to have your service suspended as many prohibit bulk mail sending in their Terms and Conditions.

    Outsourcing bulk mail is easy which is why lots of people go that route. You can do it yourself but you need to know what you're doing otherwise you can end up in big trouble.


    🦛

      am using amazon ses, so no problem with sending bulk. but my question was is it good to go, because i have tested it with 100 emails and its working well and delivering

      my Question is it good to go when i have over 100k customers

      does the progressbar operating on mail.pl script to make sure that all mails sent, before outputting success

Re: Mail sending
by Corion (Patriarch) on Aug 03, 2023 at 11:22 UTC

    If you run a legitimate business/operation with 100k real recipients, then you should have some insights as to where efficiency is needed. Are you actually encountering problems? How often do you send out new mails? Are the problems in the open rate of your mail, in the delivery/bounce rate of your mail?

    If you are looking to improve your code, I would use a templating system like Template or Mojolicious::Lite to move the HTML from your "cgi code" script into a file. Also, I would protect the mail.pl script with a username/password so that other people cannot use it to send mails without authentication.

Re: Mail sending
by afoken (Chancellor) on Aug 03, 2023 at 18:30 UTC
Re: Mail sending
by cavac (Prior) on Aug 04, 2023 at 10:30 UTC

    For one, you should have a sort of scheduler table in your database to mark to which receivers you have already send mails. That way, if your program crashed, you don't send multiple mails to some customers.

    You will also have to track which mails can't get delivered (and why), so you can disable email accounts that are no longer active.

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP

      thanks for the idea

Re: Mail sending
by karlgoethebier (Abbot) on Aug 04, 2023 at 14:17 UTC