[英]sending phpmailer smtp email with gmail takes long time (1.5 seconds)
以下腳本大約需要1.5秒來發送電子郵件,這是由$mail->send()
行中的計時器測量的。 如果我不使用smtp,它會更快,但是,一些郵件服務器將阻止傳入的電子郵件。
造成延誤的原因是什么? 如果沒有什么可以做的,那么防止用戶不得不等待它的好方法是什么?
<?php
require_once ('../../application/classes_3rd/PHPMailer/PHPMailerAutoload.php');
class myPHPMailer extends PHPMailer
{
public function __construct()
{
$this->isSMTP();
$this->SMTPDebug = 0;
$this->Host = 587;
$this->Port = "smtp.gmail.com";
$this->SMTPSecure="tls";
$this->SMTPAuth = true;
$this->Username = "example@gmail.com";
$this->Password = "my_password";
}
}
try {
$mail = new myPHPMailer(true);
$mail->AddReplyTo('me@example.com');
$mail->SetFrom('me@example.com');
$mail->AddAddress('me@example.com', 'John Doe');
$mail->Subject = "My subject";
$mail->MsgHTML("Hello! Click this link https://www.google.com/");
$time=microtime(1);
$mail->Send();
echo(microtime(1)-$time);
} catch (phpmailerException $e) {
trigger_error($e->errorMessage(), E_USER_ERROR);
}
?>
當您在PHPMailer中使用isMail()
或isSendmail()
傳輸選項時,它不會立即發送消息,而是將其提交到您的本地郵件服務器,從而釋放您的Web應用程序並在閑暇時發送消息。 這通常不會產生網絡開銷,沒有加密或身份驗證,如果它是一個低流量的服務器,它可能正在做很少的事情,並且可以非常快速地接受消息。
從來沒有真正意圖以交互方式使用SMTP,即在提交網頁期間,它確實需要很長時間。 這是一個復雜的協議,有許多往返和可能出現延遲的點,特別是灰名單的可能性,問候延遲,立即垃圾郵件過濾等等。
如果要使用SMTP並使其快速,請使用附近的郵件服務器(甚至是localhost)作為不需要加密或身份驗證的中繼,並且不對出站郵件應用垃圾郵件過濾。
而不是使用這個:
$mail->IsSMTP();
用這個:
$mail->IsMail();
我遇到同樣的問題,SMTP真的會讓你失望。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.