PHP Mailer taking over 30 seconds to send a welcome email

独自空忆成欢 提交于 2020-01-11 11:11:51

问题


So I am using the PHPMailer library in PHP to send a welcome email when ever my users registered, and it takes so long to do this.

It takes around 30 - 50 seconds to actually load the home page, after clicking submit on the registration. It basically puts the page in a reloading state for over 30 seconds.

The code I use is below...

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

And my mail library is here.

<?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);
        }
    }
}
?>

So I call these to start with, then the sendWelcomeEmail() when I want to actually send the email.

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

and

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

Why is it taking so long? Should it be taking this long..


回答1:


Remote SMTP is not really a good thing to use during page submissions - it's often very slow (sometimes deliberately, for greetdelay checks), as you're seeing. The way around it is to always submit to a local (fast) mail server and let it deal with the waiting around, and also handle things like deferred delivery which you can't handle from PHPMailer. You also need to deal with bounces correctly when going that route as you won't get immediate feedback.

That you can often get away with direct delivery doesn't mean it's a reliable approach.

To see what part of the SMTP conversation is taking a long time, set $mailer->SMTPDebug = 2; and watch the output (though don't do that on your live site!).




回答2:


Don't know if PHPMailer is compulsory for you or not, but if it's not I am recommanding SwiftMailer .

as per my personal Experience It's Really fast and reliable.

Thanks.

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);

    }


来源:https://stackoverflow.com/questions/36950408/php-mailer-taking-over-30-seconds-to-send-a-welcome-email

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