Persistent SMTP connection in PHPMailer
Asked Answered
F

4

16

How to enable persistent SMTP connections in PHPMailer?

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

Fatidic answered 25/2, 2010 at 12:30 Comment(0)
P
25

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();
Payne answered 26/3, 2011 at 22:45 Comment(6)
Thank you very much, Mauro! This is the answer I was looking for.Fatidic
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.Bonis
@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.Payne
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."Aluminous
Be sure to call ClearAddresses() within the loop before calling AddAddress(). See: https://mcmap.net/q/749144/-phpmailer-keep-1-smtp-connection-with-different-receiver-by-email-contentElodia
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.Elodia
A
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.

Abscise answered 25/2, 2010 at 12:39 Comment(2)
Currently I'm sending 2500 emails.Fatidic
Yes, I agree about throttling. Limiting to 500-1000 per hour should avoid any possibility of being seen as spammingBub
D
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.

Denazify answered 25/2, 2010 at 12:37 Comment(3)
But I need to enable this in some place in PHPMailer, because it can connect and disconnect on every email sentFatidic
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.Fatidic
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 emailsBub
I
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/

Intension answered 9/2, 2016 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.