【问题标题】:What is an efficient way to send email to many users in PHP用PHP向许多用户发送电子邮件的有效方法是什么
【发布时间】:2018-03-06 08:41:30
【问题描述】:

我有一个包含 12,000 多个用户的数据库,我正在尝试向所有用户发送一封电子邮件,每个人都根据他们在数据库中的信息提供特定信息。我已经发送了这封电子邮件,以便在周日早上 6 点运行 cron 时发送,并且我在上周五完成了该功能,它在周日运行,即昨天。这就是发生的事情。

1.) 这封电子邮件从早上 6 点到晚上 7 点整天都在发送

2.) 到那时,它只发送给 750 个用户

3.) 之后它完全停止了,原因我不知道

PS: I am sending the emails using PHPMailer,带有模板,我使用循环遍历所有用户并为每个用户执行计算,在模板中填写信息然后发送电子邮件。

下面是一个代码 sn-p 显示我的工作......

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();
            }
}

请问,任何人都可以就如何使这个过程更有效、更快或更好的选择来实现这个目标给我建议吗?谢谢。

【问题讨论】:

  • 大约每分钟1个;这听起来非常缓慢。只发送 1 条消息需要多长时间?您更有可能遇到 gmail 的限制。
  • 我在 staging 中尝试了这个,大约需要 20 分钟才能在 staging 中发送给所有 200 个用户。
  • 我认为 Google 不允许您在 1 天内发送 12,000 条消息。您可能需要一个(付费...)电子邮件提供商,如邮戳、sendgrid 等。
  • 请告诉我更多信息,因为我使用的帐户实际上是一个 Gmail 帐户。
  • 看看the mailing list example provided with PHPMailer;它比你在这里做的更有效率。我使用这种方法并达到 700,000 条消息/小时。也就是说,gmail 不是发送大量邮件的方式。

标签: php email cron phpmailer massmail


【解决方案1】:

您可以考虑使用 swiftmailer(下面的链接),因为它几乎可以满足您的所有需求,并且用于许多产品和框架,因此您可以确定它相当稳定。

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

而您每天只能发送 500 封邮件@20 封邮件/每小时

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

【讨论】:

  • Swift 有一个非常有据可查的集成,这让事情变得更容易。 SwiftMailer 的界面也比 PHPMailer 好得多。
  • 如果您发现 phpMailer 比 swiftMailer 更好,那么您也可以使用它。没有冒犯。
【解决方案2】:

只需用逗号分隔它们,如

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

欲了解更多详情,请查看此链接:- PHP send mail to multiple email addresses

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多