【问题标题】:How to improve the efficiency of PHP mail sending?如何提高PHP邮件发送的效率?
【发布时间】:2021-05-08 07:39:30
【问题描述】:

我在 CakePHP 中使用 PHP 向多个人发送邮件。

我目前在收件人数组上使用foreach,其中包含邮件地址和姓名,然后发送邮件。发送邮件需要很长时间,并且无法将收件人的邮件作为数组来完成,因为所有邮件之间存在微小差异,例如收件人姓名。

邮件使用相同的模板但参数不同,有没有我可以提高它的效率?

更新:这是电子邮件模板

<?= $userSex ?> <?= $userName ?>, <br>
Thanks for ordering!<br>
Your order's ID is "<?= $orderID ?>".<br>
You can check the shipping status on our website.<br>
Thank you and hope you have a wonderful day.

这是代码。

$mail_list = {
    [0]=>{
        'userSex'=>'Miss',
        'userName'=>'V',
        'orderID'=>'xxxxxxxx'
    },
    [1]=>{
        'userSex'=>'Mr.',
        'userName'=>'Arasaka',
        'orderID'=>'xxxxxxxx'
    }
}
foreach($mail_list as $target) {
    $email = new Email();
    $email->viewBuilder()->setTemplate('notify_template');
    $email->setEmailFormat('html')
            ->setSubject($subject)
            ->setViewVars([
                'userSex' => $target['userSex'],
                'userName' => $target['userName'],
                'orderID' => $target['orderID'],
    ]);
    $emailRes = $email
        ->setFrom(['no-reply@service.com' => 'service announence'])
        ->setTo($target['email'])
        ->setSubject($subject)
        ->send();
    if (!$emailRes) {
        $res = false;
        break;
    }
}

发送的每封邮件都略有不同。

有没有办法提高邮寄速度?

【问题讨论】:

    标签: php email cakephp-3.0


    【解决方案1】:

    您的邮寄速度将取决于您配置的传输速度(请参阅https://book.cakephp.org/3/en/core-libraries/email.html#using-transports)。

    像这样按顺序发送电子邮件总是需要时间,而且通常在某种后台进程中完成(如 cron 作业)。

    如果您确实需要比快速传输更快地发送电子邮件(对于订单和其他交易电子邮件,我对此表示怀疑),您可以研究其他选项。大多数电子邮件 API 服务都提供了某种方式来使用模板(例如 Sendgrid、Mandrill、Mailgun、Postmark 等)发送批量消息

    【讨论】:

    猜你喜欢
    • 2016-01-18
    • 2019-04-27
    • 2013-07-03
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2011-09-25
    • 2017-04-21
    • 1970-01-01
    相关资源
    最近更新 更多