PHPMailer using Gmail SMTP slow when sending emails

后端 未结 6 1580
礼貌的吻别
礼貌的吻别 2020-12-30 14:17

I found few older threads that have a similar issue but most of them didn\'t have answers or if they had, the suggestions weren\'t relevant in my case.

I had a compl

6条回答
  •  别那么骄傲
    2020-12-30 14:38

    Sending e-mails is generally a slow activity. For whatever reason (network traffic, traffic priority, mailer daemon implementation, etc) it takes a lot of time. One way to handle e-mail traffic is to not send it in line with the response you're generating. A better solution is to do it asynchronously. One suggestion above (to use a work queue and empty the work queue using a cron job) is great. The only caveat (depending on the volume of traffic) is that the time to send all the e-mails in the work queue may be greater than the time interval between cron tasks.

    Another option (and this is my preferred option) is to use a messaging layer like Rabbit MQ (probably more good books/posts on the topic) or Zero MQ (if you're a more solid programmer). In this case you create an event "send-email" and push it to a queue. There are multiple queue listeners. Once your PHP script pushes the e-mail to send to a queue, it's done and moves on. One of the queue listeners picks up the message and sends the e-mail.

    This can allow you great flexibility in building a solution. Since most of the time to send e-mails appears to be wait time, you could run 10 consumer programs to send e-mail, knowing that most of the time the individual senders will be idle. (It's been my experience in sending e-mail is long wait time but not high CPU load). By running a fixed number of consumers (your PHP script is the producer), you can throttle the rate at which you send mail/devote system resources to sending mail.

    I have implemented variants of this approach and the up side is that other things that need to happen that are slow (like resizing images) can be handled using the same pattern. As you grow you can off-load portions of the work to other machines simply by starting up consumers on those machines.

提交回复
热议问题