如何使用PHPmailer建立电子邮件队列? [英] How to build an email queue with PHPmailer?

查看:96
本文介绍了如何使用PHPmailer建立电子邮件队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在插入表格后,我已经使用PHPmailer构建了一个电子邮件脚本,但是,我收到了错误的网关502,因为该脚本超时。对我来说,发送300多封电子邮件以响应网络请求对我来说并不是一个好主意。所以我的问题是如何建立一个在后台发送电子邮件的队列?

I have built an emailing script with PHPmailer after inserting into a table, however, I'm getting a bad gateway 502 coz the script times out. and sending 300+ emails in response to a web request doesn't sound like a good idea to me. So my question is how can I build a queue that will send the emails in the background?

据我了解,我需要新表,可以说 email_queue_table 插入电子邮件地址,内容,然后发送或排队一个名为 status 的字段,创建一个while循环,例如 if($ status ==已排队){//然后在此处发送电子邮件} else {//不发送任何邮件。}

as far as I understand I will need new table lets say email_queue_table insert the email addresses, content and then have a field called status sent or queued create a while loop something like if($status == "queued"){ //then send the email here} else{ // nothing to be sent.}

如果您知道这样做的更有效/更好的方法,我将不胜感激。谢谢你的帮助。

If you know of a more efficient/better way of doing this I'm all ears. Thanks for any help.

推荐答案

如何工作的逻辑-带有一些代码可以帮助您-如下:

The logic of how it would work - with a bit of code to help you along the way - is something like this:

将以下字段添加到 email_queue_table 表中:

Add the following fields to your email_queue_table table:


  • 电子邮件地址,电子邮件地址(收件人:)

  • 内容,消息内容。如果要发送给每个用户的消息是相同,则最好将其存储在其他位置。如果仅是几件事需要更改内容(例如用户名),则使用PHP的 str_replace()进行 str_replace('% name%','Andy'),其中%name%在模板中,并将在每个循环中适当地替换。 理想情况下,您不会有此列,也不会在此处重复相同的数据-如果您的消息为50 Kb并且您有3000个用户,例如,您将在一个表中存储150 Mb的数据,

  • sent_status ,已发送状态(默认=未发送或 0

  • retry_attempts 重试尝试(默认= 0

  • email_address, Email address (to:)
  • content, Message content. If it's the same message that's to be sent to every user, you'd be better storing this once somewhere else. If it's just a few things that need changing in the content such as the users name then use PHP's str_replace() to do things like str_replace('%name%', 'Andy') where %name% is in your template and will be replaced as appropriate on each loop. Ideally you would not have this column or be repeating the same data here - if your message was 50 Kb and you had 3000 users, for example, you'd be storing 150 Mb of data in a table un-necessarily.
  • sent_status, Sent status (default = "not sent", or 0)
  • retry_attempts Retry attempts (default = 0)

创建一个执行以下操作的PHP脚本:

Create a PHP script which does the following:


  • 选择电子邮件地址,内容来自email_queue_table LIMIT 0,50 。每个循环可让您50人。重复直到列表的末尾-为此,您需要知道表中可以使用 COUNT()
  • $进行操作的记录总数。 b $ b
  • 在每个循环上(每个向您发送邮件的人):


    • 使用PHPMailer尝试发送内容电子邮件地址

    • 阅读返回状态从PHPMailer。如果成功发送,则将标志 sent_status 发送为已发送( 1 )。如果没有,请将其标记为未发送( 0 )。

    • SELECT email_address, content FROM email_queue_table LIMIT 0, 50. This gets you 50 people per loop. Repeat until the end of the list - to do this you will need to know the total number of records in the table which you can do with COUNT()
    • On each loop (each person you're sending mail to):
      • Use PHPMailer to attempt to send content to email_address
      • Read the return status from PHPMailer. If it's successfully sent flag sent_status as "sent" (1). If not, flag it as "not sent" (0).

      所有邮件发送完毕后,您可以选择返回表格尝试重新发送任何邮件没有发送。仍然保留 LIMIT 逻辑,因为可能有很多地方没有发送,例如

      When all the messages have been sent, you could optionally go back through the table to try and resend any that didn't send. Still keep the LIMIT logic because there may be a large number where it didn't send, e.g.

      SELECT email_address, content FROM email_queue_table WHERE sent_status = 0 AND retry_attempts < 5 LIMIT 0, 50
      

      增加 retry_attempts 领域。如果超过5次尝试,请停止。

      Increment the retry_attempts field. Stop if it goes beyond, say, 5 attempts.

      您无法通过浏览器执行上述脚本,因为它会超时。

      相反,您可以从命令行手动触发它,例如

      Instead, you can manually trigger it from a command line, e.g.

      php send_email.php
      

      或者在Cron上进行上述设置,以每晚或以任何频率运行

      Or set the above up on Cron, to run every night, or at whatever frequency are needed.

      或者您可以通过ajax调用触发它并在浏览器中更新进度。请参阅:在php中创建长时间运行的后台进程

      Or you can trigger it from an ajax call and update the progress in the browser. See: creating background processes in php for long running process

      这篇关于如何使用PHPmailer建立电子邮件队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆