Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending phpmailer smtp email with gmail takes long time (1.5 seconds)

The following script takes about 1.5 seconds to send an email as measured by the timers across the $mail->send() line. If I don't use smtp, it goes MUCH faster, however, some mail servers will block the incoming email.

What is causing the delay? If there is nothing that could be done about it, what would be a good solution to prevent the user from having to wait for it?

<?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 = "[email protected]";
        $this->Password = "my_password";
    }
}

try {
    $mail = new myPHPMailer(true);
    $mail->AddReplyTo('[email protected]');
    $mail->SetFrom('[email protected]');
    $mail->AddAddress('[email protected]', '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);
}

?>
like image 281
user1032531 Avatar asked Dec 18 '14 17:12

user1032531


People also ask

Does PHPMailer use SMTP?

Local Mail Server Limitation Mail() function usually needs local mail server for sending out emails whereas PHPMailer uses SMTP. Also, you should have authentication credentials.

What is PHPMailer SMTP?

The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD, and macOS platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP client allows email sending on all platforms without needing a local mail server.

How many emails can I send with PHPMailer?

PHPMailer does not set any limits, nor does the mail() function, it's only ISPs like GoDaddy that do. However, they do so by blocking access to normal SMTP ports, and/or redirecting SMTP connections to their own servers ( *. secureserver. * ).

Can I use PHPMailer in localhost?

The PHPMailer library provides the easiest way to send an email from localhost with an SMTP server using PHP. Not only the text email, but you can also send HTML email from localhost in PHP using PHPMailer.


1 Answers

When you use the isMail() or isSendmail() transport options in PHPMailer, it does not send the message immediately, but submits it to your local mail server, which frees up your web app and sends the message at its leisure. This usually incurs no network overhead, no encryption or authentication, and if it's a low-traffic server, it's probably doing little else and can accept the message very quickly.

SMTP was never really meant to be used interactively i.e. during submission of a web page, and it can indeed take a long time. It's a complex protocol with many round-trips and points where delays are likely, particularly with the likelihood of grey listing, greet delays, immediate spam filtering and more.

If you want to use SMTP and make it fast, use a nearby mail server (even localhost) as a relay that doesn't need encryption or authentication and does not apply spam filtering on outbound messages.

like image 156
Synchro Avatar answered Sep 20 '22 23:09

Synchro