【问题标题】:PHP Mailer taking over 30 seconds to send a welcome emailPHP Mailer 需要 30 多秒来发送欢迎电子邮件
【发布时间】:2016-04-30 15:23:21
【问题描述】:

所以我使用 PHP 中的 PHPMailer 库在我的用户注册时发送一封欢迎电子邮件,而这需要很长时间。

点击注册后,实际加载主页大约需要 30 - 50 秒。它基本上使页面处于重新加载状态超过 30 秒。

我使用的代码如下...

if ($config['user']['welcome_email_enabled'])
    $autoLoader->getLibrary('mail')->sendWelcomeEmail($email, $username);

我的邮件库就在这里。

<?php
/**
 * MangoCMS, content management system.
 *
 * @info         Handles the mail functions.
 * @author       Liam Digital <liamatzubbo@outlook.com>
 * @version      1.0 BETA
 * @package      MangoCMS_Master
 *
 */

defined("CAN_VIEW") or die('You do not have permission to view this file.');

class mangoMail {
    private $phpMailer;

    public function assignMailer($phpMailer) {
        $this->phpMailer = $phpMailer;
    }

    public function setupMail($username, $password, $eHost) {
        if ($this->phpMailer == null)
            return;

        $this->phpMailer->isSMTP();
        $this->phpMailer->Host = $eHost;
        $this->phpMailer->SMTPAuth = true;
        $this->phpMailer->Username = $username;
        $this->phpMailer->Password = $password;
        $this->phpMailer->SMTPSecure = 'tls';
        $this->phpMailer->Port = 587;
    }

    public function sendMail() {
        if ($this->phpMailer == null)
            return;

        if (!$this->phpMailer->send()) {
            echo 'Message could not be sent.';
            echo 'Mailer Error: ' . $this->phpMailer->ErrorInfo;
            exit();
        }
        else {
            echo 'Email has been sent.';
        }
    }

    public function setFrom($from, $fromTitle) {
        $this->phpMailer->setFrom($from, $fromTitle);
    }

    public function addAddress($address) {
        $this->phpMailer->addAddress($address);
    }

    public function setSubject($subject) {
        $this->phpMailer->Subject = $subject;
    }

    public function setBody($body) {
        $this->phpMailer->Body = $body;
    }

    public function setAltBody($altBody) {
        $this->phpMailer->AltBody = $altBody;
    }

    public function setHTML($html) {
        $this->phpMailer->isHTML($html);
    }

    public function addReply($email, $name = '') {
        $this->phpMailer->addReplyTo($email, $name);
    }

    public function sendWelcomeEmail($email, $username) {
        global $config;
        $mailer = $this->phpMailer;
        $mailer->setFrom($config['website']['email'], $config['website']['owner']);
        $mailer->addAddress($email, $username);
        $mailer->addReplyTo($config['website']['email'], 'Reply Here');
        $mailer->isHTML(true);
        $mailer->Subject = 'Welcome to ' . $config['website']['name'] . ' (' . $config['website']['link'] . ')';
        $mailer->Body = '<div style="background-color:#1a8cff;padding:24px;color:#fff;border-radius:3px;">
        <h2>Welcome to Zubbo ' . $username . '!</h2>Thank you for joining the Zubbo community, we offer spectacular events, opportunities, and entertainment.<br><br>When you join Zubbo you will recieve <b>250,000 credits</b>, <b>100,000 duckets</b>, and <b>5 diamonds</b>. One way to earn more is by being online and active, the more you are active the more you will earn, other ways are competitions, events, and games :)<br><br>We strive to keep the community safe and secure, so if you have any questions or concerns or have found a bug please reply to this email or contact us using in-game support.<br><br>Thank you for joining Zubbo Hotel!<br>- Zubbo Staff Team
        </div>';
        $mailer->AltBody = 'Here is a alt body...';
        if (!$mailer->send()) {
            exit('FAILED TO SEND WELCOME EMAIL!! ' . $mailer->ErrorInfo);
        }
    }
}
?>

所以我首先调用这些,然后在我想实际发送电子邮件时调用 sendWelcomeEmail()。

$mailer->assignMailer(new PHPMailer());

$mailer->setupMail(
    "********@gmail.com",
    "**************",
    "smtp.gmail.com");

为什么要花这么长时间?是不是要花这么长时间..

【问题讨论】:

  • 您应该进行基准测试以找出加载时间在哪里,phpmailer 所做的只是将消息发送到邮件服务器(此后它不会等待消息实际发送),它不太可能问题。
  • 您是否尝试过独立于大量渲染进行测试并仅触发电子邮件功能?
  • 有什么理由使用 gmail,而不是您的主机邮件服务器?那总是会变慢。
  • 这可能会有所帮助:stackoverflow.com/questions/6477396/…

标签: php email phpmailer


【解决方案1】:

在页面提交期间使用远程 SMTP 并不是一个真正的好东西 - 正如您所见,它通常非常慢(有时是故意的,用于 greetdelay 检查)。解决它的方法是始终提交到本地(快速)邮件服务器并让它处理等待,并处理您无法从 PHPMailer 处理的延迟交付之类的事情。您还需要在走这条路线时正确处理反弹,因为您不会立即得到反馈。

直接交付通常可以侥幸成功并不意味着它是一种可靠的方法。

要查看 SMTP 对话的哪一部分花费了很长时间,请设置 $mailer-&gt;SMTPDebug = 2; 并观察输出(尽管不要在您的实时站点上这样做!)。

【讨论】:

    【解决方案2】:

    不知道 PHPMailer 是否对您是强制性的,但如果不是,我推荐SwiftMailer

    根据我的个人经验,它确实快速可靠。

    谢谢。

    include_once "inc/swift_required.php";
    
    $subject = 'Hello from Jeet Patel, PHP!'; //this will Subject
    $from = array('jeet@mydomain.com' =>'mydomain.com'); //you can use variable
    
    $text = "This is TEXT PART";
    $html = "<em>This IS <strong>HTML</strong></em>";
    
    $transport = Swift_SmtpTransport::newInstance('abc.xyz.com', 465, 'ssl');
    $transport->setUsername('MYUSERNAME@MYDOMAIN.COM');
    $transport->setPassword('*********');
    $swift = Swift_Mailer::newInstance($transport);
    
    
    
        $to = array($row['email']  => $row['cname']);
        $message = new Swift_Message($subject);
        $message->setFrom($from);
        $message->setBody($html, 'text/html');
        $message->setTo($to);
        $message->addPart($text, 'text/plain');
    
        if ($swift->send($message, $failures))
        {
            echo "Send successfulllyy";
        } else {
            print_r($failures);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多