1

I have a php script which sends user a mail if his purchase is successful. The problem is it the page loads slow due to mail(). I know there are ways like putting the mails in a database table and then using a cron job to send them but the frequency of purchase can be high and I want the mail to be sent right away, so that doesn't look like a good option.

The purchase request gets processed by the same page from where he purchased and he can only do so once. The user doesn't control any part of mail other than the purchase details. I thought of using Ajax, the script would send the data to client side and call a ajax function which then calls another mail script but this would let user know what is being sent and can be tampered. Is there any other way I can use Ajax safely without letting user know whats being sent and where? And are there any better workarounds?

6
  • Are you running this on a windows machine?
    – hafichuk
    Nov 1, 2011 at 15:27
  • 1
    The problem is that php needs to connect to the MTA, which is probably causing the slowness. See the 'notes' section on php.net/manual/en/function.mail.php
    – hafichuk
    Nov 1, 2011 at 15:33
  • @hafichuk- That can be a reason, but I cannot change the MTA. I have no control over the server and that's the reason I'm searching for workarounds to send mail in an asynchronous way.
    – vidit
    Nov 1, 2011 at 15:50
  • Libraries like Zend_Mail and PEAR::Mail will allow you to connect to any SMTP server, so you don't necessarily need to be constrained by what your host defaults your PHP environment to. (Unless you're on a host like GoDaddy which blocks non-local outbound SMTP connections.) Nov 1, 2011 at 16:03
  • I'd look at pear.php.net/package/Mail_Queue which allows you to asynchronously send mail. I'm not sure how nicely this will play on a windows system though.
    – hafichuk
    Nov 1, 2011 at 16:20

2 Answers 2

4

mail() should return immediately upon queuing the message to your mail server, which (generally) should take no time at all, so I think your problem here is definitely the mail server and not your PHP. I'm guessing that the mail server you're submitting to is running some anti-spam checks, like reverse DNS lookups. It might also be throttling you based on usage. Can you try sending through another mail server to verify?

Also, if you have shell access, try sending a message from the shell (e.g., echo test | mail -v -s test [email protected]) to see what it's doing and how long it takes.

Edit: Just noticed your comment re: Windows. In this case, at least you can try a telnet from the PHP server to port 25 on the mail host to see how long it takes to connect and get the 220 greeting header. (I bet you'll connect immediately but won't get the 220 header for a while.)

2
  • @AlexHowansky - You are right, it connected but took a while for the headers. So the only option is to use external SMTP servers?
    – vidit
    Nov 1, 2011 at 16:08
  • Not necessarily. I'd recommend contacting the admin of the mail server to find out what checks it's doing. Your slowness might be the result of something as simple as an incorrect DNS entry for your domain. You might also be able to get them to whitelist you so that the checks aren't performed at all. Nov 1, 2011 at 16:45
0

You're on to the right idea. Here's what I would do in this scenario:

  1. The user makes a purchase (no ajax unless you want to at this point)
  2. In processing that purchase an email is inserted into the email table with an "id" & "sent" column plus all the other stuff.
  3. User is brought to a success page
  4. The success page kicks off ajax in the background to send the email with that id from the db - and doesn't care about the result
  5. The php page in charge of emailing sends the email and marks the sent column

If someone ever makes a call to your database with an id that's not sent, that email needed to get sent out anyway, so it's ok. If the id doesn't match or the sent column is marked true, you can ignore it. No worries about people trying to send spam using your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.