Home > Net >  PHPMailer - Script hangs for 20 seconds after SMTP connection is opened for a single email request
PHPMailer - Script hangs for 20 seconds after SMTP connection is opened for a single email request

Time:01-13

I will start by saying that I am not having an issue with sending successful emails with PHPMailer. It is working as intended in that regard. The PHPMailer script is working and this may very well not even be a PHPMailer related issue. However, there is a 20 second "processing" delay that is happening everytime I attempt to run the very basic script.

This essentially botches any kind of front-end experience which is causing an issue for my intended use case (a contact form). Note that my testing was done solely on the provided Github recommended example code, no external code was added beyond necessary configuration.

The script is sending a basic singluar email. No batch or mass sending is required. My code and PHPMailers debug log are posted below. Please check out the debug log - after the initial connection is open, there is a 20 second delay between connection opened and the first "220" status. I am not versed enough to understand exactly what this means and I was hoping to get some clarity on that. A simple google search does provide that 220 means Domain Service is Ready. But what is really happening in these 20 seconds? What is causing the hang? 20 seconds is too long to send a single email.

I have been researching delays on SMTP and PHPMailer issues for over a week now and I cannot find any good information that provides a straight forward answer to this issue. I have however found recommendations to:

  1. Use a localhost to recieve mail. (I cannot do this, I don't have the capability to run my own mail server 24/7)

  2. Design a Mail Queue and batch send on a different thread(cron job, etc) - Note that I have done this before for a much larger project. But considering this use case is going to be for a basic contact form, I feel like that is a bit overkill. Maintaining a database to send a singular email should not be necessary. I also have no need for mass sending. Every email will always be a singular email and ideally it would be recieved immediately, not on timed intervals, ie. cron job.

  3. Your Web Hosting is garbage. (It is likely bad) I don't really have experience with who is the best or the worst. I am hosted through Hostinger and unfortunately I am essentially locked in for 4 years without taking a loss.

--

Is there anyone that understands what is actually hapenning here? Maybe another person that had a similar issue? Someone that can take the debug logs at more than face value and could provide any clarity on what is causing the 20 second delay on every single of the script. Or any potential solutions that allow me to continue using SMTP but without the delay. It is almost always exactly 20-21 seconds.

(Currently when manually disabling SMTP from the script, it eliminates the 20 second delay, but it essentially reverts the code to scrictly use the php mail() function, which I hear is bad practice. I would love to be able to retain best practice, which I hear is SMTP/PHPMailer)

My basic code I'm using essentially straight from the recommended Example script on Github

My PHPMailer provided debug logs

CodePudding user response:

This is an anti-spam technique called “greetdelay”, and will be a deliberate choice implemented by your mail service provider. The SMTP spec says that clients must wait for the server to respond before starting an SMTP transaction. Badly-behaved clients (such as spam scripts) don’t wait, and the server will drop the connection and possibly block subsequent connections from that IP if they start talking too soon.

PHPMailer is well-behaved (it will wait, as you’re seeing), however this really highlights that SMTP is not a good thing to use during interactive HTTP request processing, which is mentioned in the PHPMailer docs.

The right solution to this is to run a local mail server to act as a relay. Submission will be effectively instant as far as your page is concerned, and the mail server will handle onward delivery, including the greetdelay. It will also handle more complicated problems like greylisting, network outages, mail server throttling, etc.

Mail servers like postfix are not difficult to configure to act this way, but that’s really suited to docs, tutorials, or a question on server fault. I’m not clear why you can’t do this. If you have root access to your own VM, and you already know that outbound SMTP traffic is permitted, it should not be a problem.

  • Related