Currently I've been using PHP and the Zend Framework to send emails with SMTP and GMail's SMTP servers. That's because I have a Google Apps domain setup to handle all my emails so it looks like my emails are coming from mailer@mycompany.com.

Now when a user registers or does something which requires my web server to send an email to them this can take 5 seconds or so on the PHP side to initiate the SMTP connection and send the email to them. The process would be something like this:

1. User completes registration form
2. User submits form
3. PHP script saves information to database and sends them an email (5-10 seconds)
4. User redirected to another page

Now when they've just submitted the form I can't have a massive delay from my web server while the user sits there for 10 seconds looking like it's loading. Basically the send email process is single threaded so the server can't do anything else until it's completed sending the email which is anywhere between 5-10 seconds, sometimes 20 seconds if there's an error with the email address so this is far too long.

How to people normally get around issues like this?

Originally I tried a few things:

1. Using another library like SwiftMailer. Still same problem. I think it's the SMTP/TLS handshake which takes extra time.
2. Storing the email data in a database and then using fsockopen to initiate an asynchronous request to another page which would then get the email data from the database and send the email. Meanwhile my PHP script could continue synchronously and the user would get redirected to the next page immediately after the form submission. Problem was there was no way to confirm whether the email was sent, or if it failed. I could set a flag on the database table I suppose but I wanted to provide instant feedback to the user that their email had been sent. That meant on the next page, firing off an Ajax request after 10 seconds to another page which would check the flag on the database table to see if it was sent or not, then show the response on the page to the user. This wouldn't work if they entered the email wrong and it couldn't send the email, as the response from GMail would sometimes take 20 seconds for an error case, then the database flag wouldn't be set.
3. Another option I came up with was to put the email data in the $\_SESSION array, so after submitting the page and the user is on the redirected page it would send an ajax request off to another page which would get the session data and send the email, then send a response back to the page the user was on and tell them the email was sent. This works pretty well, but I feel it's probably not the best way.

So I'm wondering what's the best practice way to do it? Is GMail too slow for this kind of thing as it has to do the TLS handshake? Should I just queue up all the emails into the database and run a CRON job every few minutes to send them off, then remove them from the database once sent? Problem with that is there's no instant feedback given to the user that their email was sent correctly. Or do I not need to bother with that anyway? They either get the email or not.