What is an efficient way to send email to many users in PHP

340 Views Asked by At

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 = "[email protected]";
                $mail->Password = "myPassW0rd";
                $mail->SetFrom('[email protected]', 'Name');
                $mail->AddReplyTo("[email protected]", "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.

2

There are 2 best solutions below

2
On

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

0
On

Just separate them by comma, like

$email_to = "[email protected], [email protected], John Doe <[email protected]>"

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