PhpMailer的发送延迟约30秒

时间:2019-07-23 14:16:48

标签: php header phpmailer

好的,所以我对PhpMailer没什么问题。

  1. 执行$ mail-> send大约有30秒的延迟。
  

2019-07-23 13:43:55 连接:打开   smtp.gmail.com:587,超时= 300,options = array()
2019-07-23    13:44:16 连接:已打开
2019-07-23 13:44:16服务器   等等...(一些参数列表)。

  1. 用于注册用户的代码。我实例化了模型方法内部的Mail类,然后在控制器内部通过成功登录后重定向的函数调用了该方法。我还收到标头已发送错误。
  

2019-07-23 13:44:17连接:已关闭
消息已   已发送
警告:无法修改标头信息-标头   已发送者(输出始于   C:\ xampp \ htdocs \ log \ vendor \ phpmailer \ phpmailer \ src \ SMTP.php:257)中   C:\ xampp \ htdocs \ log \ App \ Core \ Controller.php,第43行

我的代码:

邮件类别:

class Mail
{
    public function sendMail($to, $subject, $text, $html)
    {
        $mail = new PHPMailer(true);

        try {
            //Server settings
            $mail->SMTPDebug = 3;
            $mail->isSMTP();
            $mail->Host = 'smtp.gmail.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'wuwu5431@gmail.com';
            $mail->Password = 'Password';                       
            $mail->SMTPSecure = 'tls';
            $mail->Port = 587;

            //Recipients
            $mail->setFrom('wuwu5431@gmail.com', 'John Smith');
            $mail->addAddress($to, '');
            $mail->addReplyTo('wuwu5431@gmail.com', 'Information($mail->addReplyTo)');

            //Content
            $mail->isHTML(true);
            $mail->Subject = $subject;
            $mail->Body    = $text;
            $mail->AltBody = $html . ' $html . This is the body in plain text for non-HTML mail clients';

            $mail->send();

            echo 'Message has been sent';
        } catch (Exception $e) {
            echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        }

    }//end of method
}// end of class

模型(方法):

public function sendActivationEmail($email)
{
    $url = 'url';
    $text = 'text';
    $html = 'html';
    $mail = new \App\Mail;
    $mail->sendMail($email, 'Account activation', $text, $html);
}

控制器(方法):

public function register()
{
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
        foreach($_POST as $key => $value){
            $data[$key] = htmlspecialchars(strip_tags(trim($value)));
        }
        $this->userModel = new \App\Models\UserM;
        if ($this->userModel->Register($data)) {
            Flash::addMessage('Thank you for Registering with us');
            $this->userModel->sendActivationEmail($data['email']);
            $this->redirect('User/UserC/success');
        }
    }
}

环境:win 7,xampp,localhost。

1 个答案:

答案 0 :(得分:0)

这完全正常-欢迎使用SMTP。

SMTP可以在事务中的多个点上施加最多10个分钟的延迟,因此,它仍然不适合在页面提交处理中使用,尽管它仍然这样做很常见。

处理它的方法是使它异步(就您的页面而言),并且有多种实现方法,例如,将消息存储在队列中,该队列由稍后的单独进程拾取并发送(即不暂停页面提交处理),或直接将其提交到本地邮件服务器,该服务器通常会在几毫秒内接受提交,然后为您处理继续传递。

与此分开,这些行可能是错误的:

$mail->setFrom('wuwu5431@gmail.com', 'John Smith');
$mail->addAddress($to, '');
$mail->addReplyTo('wuwu5431@gmail.com', 'Information($mail->addReplyTo)');

如果您的发件人回复至地址相同,则回复至将被忽略。如果您没有与 to 地址一起使用的名称,请不要使用。单引号字符串在PHP中不进行变量插值。您可能只想要:

$mail->setFrom('wuwu5431@gmail.com', 'John Smith');
$mail->addAddress($to);
相关问题