How can I increase the speed of sending Secure SMT

2019-07-29 14:50发布

问题:

I have a PHPMailer 5.2.16 on a server using Exim 4.87 which also has a TLS certificate for secured connections.

My PHP is thus:

class MailerSMTP extends PHPMailer {
    /***
     * Mailer for authenticated SMTP  emails using account mail system
     ***/
    public function __construct(){
        $this->AddReplyTo('...', '...');
        $this->setFrom('...', '...');
        $this->Host = "hostname.co.uk";
        $this->isSMTP();
        $this->Timeout = 20;

        $this->SMTPAuth = true;
        $this->SMTPSecure = "tls";
        $this->Port = "587";
        $this->Username = 'emailuser';
        $this->Password = 'emailpass';
    }
}

And this is obviously called on the script and populated with reciever and message, etc.

However the SMTPSecure aspect adds about 2 seconds (or sometimes a bit more) on to the time taken to send the message. Currently this delay is on a single message sending, and I would hope (I think I read somewhere) that the SMTP secure would only need to be called once to send X number of messages to X number of recipients.

  • While I accept this delay might be unavoidable to some extent, I would like some advice on to how to improve the efficiency of secured SMTP via this method?

Bonus Question:

  • Am I correct in thinking that this delay would only occur once when this class is instantiated, regardless of the number of emails sent through it?

I would imagine I can do something like this:

$sender = new MailerSMTP();
$sender->subject ="hello";
$sender->Body = "message";
foreach($receiver as $row){
    $sender->addAddress($row['email']);
    $sender->send();
    $sender->clearAddresses();
}

Would this send all emails with only a 2 second SMTPSecure delay?

回答1:

Yes, TLS adds some connection overhead. You can avoid it by submitting to a local mail server over SMTP without TLS - that's the fastest way to free up your script. It remains secure because it's not leaving the server, and you can configure your mail server to relay securely from there onwards.

You're nearly there on the multiple messages thing, but I recommend looking at the mailing list example provided with PHPMailer. The most important thing when sending multiple messages in one go is to enable keepalive, which avoids repeating the connection overhead for each message.