Skip to content
Advertisement

What does SwiftMailer’s SMTP timeout setting relate to?

When using SwiftMailer’s Swift_SmtpTransport there is a timeout setting.

https://symfony.com/doc/current/reference/configuration/swiftmailer.html#timeout

What does this refer to? Is it just the timeout used to login to the server or is it for the complete operation of logging in and sending the message(s)?

I came across this because I noted if you use invalid SMTP credentials it takes 30 seconds before the script continues — which is far too long for a public website. Using setTimeout(5) seems to be much more acceptable in the case of invalid credentials (e.g. they get changed) or the server being down.

If this is just to login to the SMTP server I think 5 seconds is more than adequate. If this is to send messages as well I still think this would be okay for sending a small number of emails. I have done a few test emails and seems fine so far but wanted to check this before going live with it.

Advertisement

Answer

When using a stream socket in PHP, there are two timeout:

  1. The timeout for connection, (look at the timeout parameter)
  2. The timeout when writing or reading data on socket (with fread or fwrite)

fread documentation said:

a packet becomes available or the socket timeout occurs (for network streams)

It seems that this timeout parameter is used for three tasks in Swiftmailer:

  1. Timeout to connect to the smtp server.
  2. Timeout to write mail. (Here a code example for setting the timeout for writing data)
  3. A third one, for the whole process which depends from the size of mail(as described in RFC2821)

But, if you don’t want to wait that the mail is sent before sending a response to user, you should have a look on the messenger component. Instead of having only one process, you will have two async process. Your controller won’t anymore wait that the mail is sent, it will just send a message (“Please sent a registration mail to this new user”) to the messenger component. The messenger component will launch some actions to sent this mail. A state field is generally used to know if mail was successfully sent or not. Fabien Patencier wrote a book “Symfony the Fast Track” that contains a useful and complete example to do it.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement