发送电子邮件时使用Gmail SMTP的PHPMailer速度很慢 [英] PHPMailer using Gmail SMTP slow when sending emails

查看:161
本文介绍了发送电子邮件时使用Gmail SMTP的PHPMailer速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很少有类似问题的旧线程,但大多数都没有答案,或者如果有答案,则该建议与我的情况无关.

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.

我使用PHP邮件功能一次完成了完整的设置,并且效果很好.我必须一次格式化硬盘驱动器,然后从头开始设置服务器.之后,PHP邮件功能变慢了.在研究解决方案时,我发现大多数人都推荐PHPMailer.我切换到那个位置,但问题仍然存在.

I had a complete setup at one point with PHP mail function and it worked great. I had to format my hard drive at one point and setup the server from scratch. Afterwards, PHP mail function became slow. While researching solutions for that, I found that most people recommended PHPMailer. I switched to that but the problem still persisted.

大多数时候,我每页至少发送两封电子邮件,但邮件正文不同,但使用的对象相同.大约有3-4秒的延迟.请在下面找到相关代码($ email1和$ email2是包含有效电子邮件地址的数组):

Most of the time, I am sending at least two emails per page with different bodies but using the same object. There is about 3-4 second delay. Please find the relevant code below ($email1 and $email2 are arrays containing valid email addresses):

function sendEmail ($email1, $subject1, $message1, $email2, $subject2, $message2) 
{
    require_once('../PHPMailer/class.phpmailer.php');

    $mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->SMTPDebug = 0; 
    $mail->SMTPAuth = true; 
    $mail->SMTPSecure = 'ssl';
    $mail->Host = "smtp.gmail.com";
    $mail->Port = 465; 
    $mail->IsHTML(true);
    $mail->Username = $gmail_username;
    $mail->Password = $gmail_password;
    $mail->SetFrom($gmail_address,$email_title);

    $mail->Subject = $subject1;
    $mail->Body = $message1;
    foreach($email1 as $k => $v) {  $mail->AddAddress($v);  }       
    if(!$mail->Send()) {    $emailreturn['cust'] = 0;   } else {    $emailreturn['cust'] = 1;   }
    $mail->ClearAddresses();

    $mail->Subject = $subject2;
    $mail->Body = $message2;
    foreach($email2 as $k => $v) {  $mail->AddAddress($v);  }
    if(!$mail->Send()) {    $emailreturn['partner'] = 0;    } else {    $emailreturn['partner'] = 1;    }
    $mail->ClearAddresses();
}

通过调试和消息我看不到任何错误,发送电子邮件所花的时间比平时更长.

I don't see any error through debug and messages, it just takes longer than usual to send the email.

我尝试过的事情:

  • 我关闭防火墙只是为了对其进行测试,它是相同的.
  • 切换到"tls",这使其变得更慢
  • 使用对象发送每封电子邮件,这会使每封电子邮件延迟3-4秒
  • 播放可选配置,注释掉或设置为false,所有结果均相同

邮件程序设置中是否还缺少其他内容,或者我应该检查一些幕后配置? 谢谢

Is there anything else missing in the mailer setup or is there some behind-the-scenes configuration that I should check? Thanks

推荐答案

缓慢(或由于超时而失败)是因为Google支持IPv6寻址,但您的网络却不支持. (例如,Digital Ocean尚不支持IPv6进行SMTP通信).因此,使用此:

The slowness (or failure due to timeouts) is because Google supports IPv6 addressing but your network does not. (e.g. Digital Ocean does not yet support IPv6 for SMTP traffic). So, use this:

$mail->Host = gethostbyname("smtp.gmail.com");

gethostbyname()将返回IPv4地址.

gethostbyname() will return the IPv4 address.

对我来说,我的PHPMailer脚本从大约2分钟的执行时间变为了不到4秒的时间

For me, my PHPMailer script went from ~2 minutes execution time to <4 seconds

这篇关于发送电子邮件时使用Gmail SMTP的PHPMailer速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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