2

I have a database of over 12,000 users and I am trying to send an email to all of them, each with a specific information based on their information on the database. I have made this email to send when a cron runs on Sundays by 6am and I completed the feature last friday and it ran on sunday, i.e, Yesterday. Here is what happened.

1.) The email kept sending all day from 6am to 7pm

2.) By that time, it had sent only to 750 users

3.) After that it stopped completely for reasons I don't know

PS: I am sending the emails using PHPMailer, with a template and I use a loop to loop over all users and perform calculations for each user, fill in the template with the information then send the email.

Below is a code snippet showing what I do...

foreach($users as $user){
            // Construct the email template
            $htmlContent = file_get_contents(__DIR__ . '/../../templates/weekly_spending_template.html');

            // Replace some place holders with user's custom information.
            $htmlContent = preg_replace('/\$bars/', $bars, $htmlContent);
            $htmlContent = preg_replace('/\$labels/', $labels, $htmlContent);
            $htmlContent = preg_replace('/\$total/', $currency . ' ' . number_format($total, 0), $htmlContent);
            $htmlContent = preg_replace('/\$budget/', $currency . ' ' . number_format($budget, 0), $htmlContent);
            $htmlContent = preg_replace('/\$first_name/', ucfirst($user->first_name), $htmlContent);
            $htmlContent = preg_replace('/\$remark/', $remark, $htmlContent);
            $htmlContent = preg_replace('/\$percentage_difference/', $percentage_difference, $htmlContent);
            $htmlContent = preg_replace('/\$others/', $others, $htmlContent);


            try {
                // Setup email parameters
                $mail = new PHPMailer(true);
                $subject = "Your weekly spending breakdown";


                $mail->IsSMTP();
                $mail->SMTPDebug = 0;
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = "ssl";
                $mail->Host = "smtp.gmail.com";
                $mail->Port = 465;
                $mail->AddAddress($user->email, ucfirst($user->first_name) . ' ' . ucfirst($user->last_name));
                $mail->Username = "mymail@example.com";
                $mail->Password = "myPassW0rd";
                $mail->SetFrom('mymail@example.com', 'Name');
                $mail->AddReplyTo("mymail@example.com", "Name");
                $mail->Subject = $subject;
                $mail->Body = $htmlContent;
                $mail->isHTML(true);

                if (!$mail->send()) {
                    echo "Message was not sent.\n";
                    echo 'Mailer error: ' . $mail->ErrorInfo . "\n";
                } else {
                    echo "Message has been sent.\n";
                }
            } catch (\Exception $ex) {
                echo $ex->getMessage();
            }
}

Please, can anyone give me suggestions on how to make this process more efficient, faster or better options for achieving this goal? Thanks.

Pila
  • 5,460
  • 1
  • 19
  • 30
  • That is about 1 per minute; that sounds extremely slow. How long does it take when you send only 1 message? More likely you are running into gmail's limits. – jeroen Sep 25 '17 at 11:34
  • I Tried this on staging and it took about 20 minutes to send to all 200 users on staging. – Pila Sep 25 '17 at 11:38
  • 1
    I don't think Google allows you to send 12,000 messages in 1 day. You probably need a (paid...) e-mail provider like postmark, sendgrid, etc. – jeroen Sep 25 '17 at 11:41
  • Tell me more please because the account I am using is actually a Gmail account. – Pila Sep 25 '17 at 11:46
  • 2
    Take a look at [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps); It's much more efficient than what you're doing here. I use this approach and reach 700,000 messages/hour. That said, gmail is not the way to send high volumes. – Synchro Sep 25 '17 at 12:20
  • @Synchro that definitely is a factor, OP is making a new connection for each mail, horribly inefficient. OP, this method will give you huge increases in performance right out of the box. – ishegg Sep 25 '17 at 12:34
  • That makes so much sense @Synchro. I was making a new connection for each user and for each email sent I will update the code to effect that. Now, the issue if how not to have a limit to the number of emails I send per day. How can I manage to hit up to that 70,000 messages? Which email client will let me send unlimited number of emails perday? – Pila Sep 25 '17 at 12:57
  • You missed a `0` :) Going that fast takes lots of other factors - the key thing is that PHPMailer isn't slow. It's not up to the client how many messages you can send - you just need a better host that allows you to run a decent mail server (which isn't that easy), or use an external SMTP service, as has been mentioned already. – Synchro Sep 25 '17 at 15:26
  • Any suggestions about the alternative clients @Synchro? As for the host, I am using AWS. – Pila Sep 26 '17 at 03:57
  • As I said, it's not up to the client, it's up to the server. On AWS you could look into their [SES service](https://aws.amazon.com/ses/), which has an SMTP interface. – Synchro Sep 26 '17 at 10:17
  • Possible duplicate of [How to send 100,000 emails weekly?](https://stackoverflow.com/questions/3905734/how-to-send-100-000-emails-weekly) – Johannes Kuhn Jan 10 '18 at 18:49

2 Answers2

1

You might consider using swiftmailer (below link), as it has mostly everthing you want and is used in many products and frameworks, so you can be sure it's fairly stable.

https://swiftmailer.symfony.com/docs/sending.html#sending-emails-in-batch

And you can only send 500 mails/per day @20 mails/per hour

See: https://support.google.com/a/answer/2956491#sendinglimitsforrelay

  • Swift has a very well documented integration, which makes things easier. Also SwiftMailer has a much nicer interface than PHPMailer. – BabaBaluchi Sep 25 '17 at 11:52
  • It is upto you if you find phpMailer better than swiftMailer then you can use it too. No offends. – BabaBaluchi Sep 25 '17 at 11:54
-3

Just separate them by comma, like

$email_to = "youremailaddress@yourdomain.com, emailtwo@yourdomain.com, John Doe <emailthree@example.com>"

For more details check this link:- PHP send mail to multiple email addresses

kannu
  • 37
  • 10