Speed up PHPmailer with office 365 smtp

。_饼干妹妹 提交于 2019-12-13 01:34:14

问题


I am using an PHPMailer to send mail from my server through an office365 account.

However it seems to take about 10-12 seconds to send the email, and this is making customers impatient and they are clicking the send button again.

This is my code

$mail->IsSMTP();                                // enable SMTP
$mail->SMTPAuth     = true;                     // turn on SMTP authentication
$mail->SMTPSecure   = "tls";                    // Secure type = tls
$mail->Host         = "smtp.office365.com";     // Hostname
$mail->Username     = "user@domain.co.uk";      // Username
$mail->Password     = "password";            // Password
$mail->Port         = 587;                      // Office 365 Port

$mail->IsHTML(true);
$mail->setFrom('online@domain.co.uk','Website Email');
$mail->AddReplyTo("sales@domain.co.uk","Sales");
$mail->AddAddress($value);
$mail->Subject($subject);
$mail->Body($body);
if(!$mail->Send()) {
     $string = date('Y-m-d').','.date('H:i:s').','.$mail->ErrorInfo;
     $this->writetolog('mailLog',$string);
 } else {
      echo "Message sent!";
 }

The message is sending correctly but it just takes so long to send it.

Is this a common thing or there something I can do to speed the process up at all.

Before i was using the smtp and just sending from the server it was instant but customers were sometimes getting the mail into junk boxes etc and they did not look professional.

Many Thanks.


回答1:


Your code is fine. SMTP just isn't very good for interactive use - it's designed for store-and-forward - and it's often slow. Office365 also has something of a reputation for being slow and unreliable. If you run your own mail server (such as postfix) you can submit to that locally (which is more or less instant) and configure it to relay through office365.




回答2:


Some processes are slow; sometimes there's just no way around it. I can't comment specifically on SMTP in Office365, but my advice applies to many processes that can be slow.

By far the best way to deal with this kind of thing is to move the delay out of the user interface.

The way to do that is simply not to send the email when the user asks you to. Instead, just create a record in a database noting that a request has been made to send an email.

So you have a DB table with a bunch of records that effectively form a queue.

You then write a separate batch program which reads through the queue, does the jobs requested in the queue (ie sending the emails), and marks the relevant records as having been completed.

This batch program would be set up as an automated process that runs regularly (eg every five minutes) to look for new requests and action them.

The net effect of all of this is:

  • Your user interface will never sit around waiting for the emails to be sent; it'll respond immediately.
  • The emails may not be send straight away, but they should never be delayed by more than five minutes (or whatever the interval is on your batch job). And to be honest, who cares about a five minute delay on an email?

You can also use this technique for other time consuming tasks; your queue doesn't have to just contain email requests.

This is a very common technique; indeed, there are whole frameworks and libraries built around doing things this way. It's fairly easy to set up a basic system as described above, but more complex queueing systems can be quite powerful.




回答3:


Thanks for the help,

To get over the problem i decided to store the email into a database. Every 2 minutes i then run a cron job to clear the mail queue and send using the exchange smtp

If it take 10-12 seconds behind the scenes it has not ruined my customer experience.



来源:https://stackoverflow.com/questions/32116362/speed-up-phpmailer-with-office-365-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!