Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mails sent using PHP taking too much time

I have written a class in PHP which I use for sending mails making use of a Gmail account. This class in turn uses the PHPMailer library. The setup is WAMP 2.4 on Windows Vista. Using the microtime() function in PHP, I see that it takes anywhere between 5 to 6 seconds to send a single mail. Is it normal for a PHP script running on the kind of set up that I have to take as much as 5-6 seconds for a single mail going out. Here is code for the class.

<?php

require_once("phpmailer/class.phpmailer.php");
require_once("phpmailer/class.smtp.php");

class Mailer {

    // Needs to be set per object
    public $subject;
    public $message;
    public $to_name;
    public $to;

    private $mail; // This is the main mail object that'll be initialized 

    public function __construct() {

        // Need to create a PHPMailer object in the constuctor and return it for use in this class. 

        $mail = new PHPMailer();

        $from_name = "bleh";
        $from = "[email protected]";
        $username = "bleh";
        $password = "bleh";

        $mail->FromName = $from_name;
        $mail->From = $from;
        $mail->Username = $username;
        $mail->Password = $password;

        $mail->IsSMTP();
        $mail->Host = "smtp.gmail.com";
        // $mail->Port = 587; // Turns out, I dont need this one. 
        $mail->SMTPAuth = true; // gmail requires this
        $mail->SMTPSecure = 'tls'; // gmail requires this

        $this->mail = $mail;


    }


    function send() {

        $mail = $this->mail; // The mail object 

        $mail->Subject = $this->subject;
        $mail->Body = $this->message;
        $mail->AddAddress($this->to, $this->to_name);

        $result = $mail->Send();
        return $result;

    }
}
?>

Code used to test this -

$startTime = microtime(true);
require_once("mailer.php");

$mailer = new Mailer();

$mailer->subject = "Test";
$mailer->message = "Test";
$mailer->to_name = "My Name";
$mailer->to = "anemail@address";

$mailer->send();
echo "Time:  " . number_format(( microtime(true) - $startTime), 4) . " Seconds\n";
like image 272
user1720897 Avatar asked Feb 14 '23 05:02

user1720897


2 Answers

It is very common for SMTP to take a long time - it's even used as an anti-spam measure in the form of greetdelay/tarpit mechanisms. RFC2821 section 4.5.3.2 allows up to a 5 minute delay before traffic starts. SMTP is not intended for interactive use (since it can't queue in that situation), and sending via SMTP during web page submission can suffer because of that. Sendmail or SMTP via an async process would avoid the issue.

In PHPMailer you can enable SMTP debug output and it will show you what's happening so you'll be able to see what's taking the time:

$mail->SMTPDebug = 2;
like image 193
Synchro Avatar answered Feb 15 '23 17:02

Synchro


As mentioned in my comment, Gmail may be rate limiting you. There could also be some aspect of your network communication with Gmail that is causing the issue.

You can manually begin an SMTP conversation with Gmail from the command line. Watch for how long each step takes, and check for any codes / messages that may come back from Gmail indicating a problem.

For details on how to create a manual SMTP conversation see

Connecting to smtp.gmail.com via command line

Messages that come back will be Base64 encoded as indicated in that answer. You can use an online Base64 decoder to convert back to plain text.

Note: The link shows instructions for Linux. If you don't have a Linux server to test from, you can use Cygwin (for windows) or an OpenSSH for Windows package that does not require a full Cygwin install

like image 34
Eric J. Avatar answered Feb 15 '23 18:02

Eric J.