我编写了代码,通过循环1200次向超过1200个用户发送邮件,以便为每个用户生成唯一的随机取消订阅链接,但由于此过程,页面会延迟。请给我建议完成这项任务。我的代码如下:
while ($result = mysqli_fetch_assoc($query)) {
$unsubscribe_link = 'XXXXXX';
mail(
$result['user_email'],
$subject,
message($unsubscribe_link), html_headers($from_mail)
);
}
答案 0 :(得分:1)
It is possible to just concatenate email addresses in to the to field of PHP's mail function to avoid the loop:
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';
However as you are sending individual unsubscribe links to users by the looks of things this wouldn't be feasible (as recipients would see other's email addresses). Plus, as mentioned in the comments already, this is probably a task better suited to being handled in the background.
值得注意的是,mail()函数不适合循环中的大量电子邮件。此功能为每封电子邮件打开和关闭SMTP套接字,效率不高。 要发送大量电子邮件,请参阅»PEAR :: Mail和»PEAR :: Mail_Queue包。
我会想到,对于您的交易电子邮件(例如Mandrill),使用第三方提供商可能会更安全(垃圾邮件)并且更有效。