15

How to enable persistent SMTP connections in PHPMailer?

I will send many emails, so with persistent connections probably I will get performance gain.

Paulo Coghi
  • 13,724
  • 14
  • 68
  • 90

4 Answers4

23

We really don't care here if your server gets blacklisted, right? This is how to achieve what you want. Just set to true the SMTPKeepAlive property and after the bulk sending, call implicitly the SmtpClose() method.

$phpMailer = New PHPMailer();
$phpMailer->isSMTP();
$phpMailer->SMTPKeepAlive = true;

for ( ... ) {
    // Send your emails right away
    [ ... ]
}

$phpMailer->SmtpClose();
5lava
  • 1,168
  • 11
  • 12
Mauro
  • 3,946
  • 2
  • 27
  • 41
  • Thank you very much, Mauro! This is the answer I was looking for. – Paulo Coghi Apr 09 '11 at 05:11
  • 1
    Hi Mauro I used the same code $phpMailer->SMTPKeepAlive = true. But did not increase the email sending speed. This line of code does not change performance in my case. Please give me suggestions if you have any other. – mahesh kajale Aug 17 '15 at 10:47
  • 4
    @maheshkajale, `$phpMailer->SMTPKeepAlive = true` will only avoid closing the connection on the first email so it doesn't have to be opened on the next. However, the server might be closing the connection anyway and you can't change that. If you need to send email faster, I recommend you use an API based email platform like Mandrill or Mailgun, where you can fire hundreds of emails on a single request. However if you're trying to do SPAM, just forget I helped you, go to your room and thing about what you did. – Mauro Aug 17 '15 at 14:36
  • 3
    This may be helpful when using Gmail's smtp-relay servers (for enterprise/educational accounts) when sending multiple emails via script. - From Google's docs: "Google Apps SMTP relay servers have protections in place to guard against Denial of Service (DoS) attacks. To avoid conflicts with these protections, SMTP agents that send large amounts of mail through smtp-relay.google.com should reuse connections, sending multiple messages per connection. This is also known as connection caching." – Tim Dearborn Feb 29 '16 at 15:45
  • 6
    Be sure to call `ClearAddresses()` within the loop before calling `AddAddress()`. See: http://stackoverflow.com/a/18285896/482115 – degenerate Feb 15 '17 at 20:21
  • Many of us use PHPMailer to send email through transactional services like Postmark, Mailgun, SendGrid, SES, etc. so spam is not an issue at all. Those services allow you to send mail as fast as you want, so minimizing SMTP connect time is ideal. – degenerate Nov 30 '17 at 17:42
2

By optimising the sending of emails, you might open yourself up as being identified as spamming and so cause web servers to block your IP.

How many emails are you sending? It may be better to actually throttle emails sent rather than speed up.

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
1

What do you mean by persistent SMTP connection?

First if you send a Email you are connected to the Server until it finishes the job. Secondly if you wanna send many emails (Probably your server will be in the blacklist), you write a loop in your PHP code, whitch fetches all Email adresses and passes them to the phpmailer and finaly sendts them. Thats how i would send mass mails.

streetparade
  • 32,000
  • 37
  • 101
  • 123
  • But I need to enable this in some place in PHPMailer, because it can connect and disconnect on every email sent – Paulo Coghi Feb 25 '10 at 12:47
  • My server is recognized and has (almost) the necessary stuff to don't be blacklisted. We send emails regularly and have a large customer base, and this regularity also allows us to be well ranked in receive servers. – Paulo Coghi Feb 25 '10 at 12:50
  • You should probably set up a queue and use the loop to process a batch at a time so that you can throttle the amount of emails – cEz Feb 25 '10 at 14:22
0

Persistent SMTP connection is suitable when you are sending bulk and want to send faster, keeping the SMTP connection alive for specific number of email sends is the good idea, for faster sending. Frequently alternating SMTP connection within the loop can be the way for more controlled sending during IP warming sessions. https://www.mumara.com/persistent-smtp-connection-and-non-persistent-loop/