SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sending email through SMTP

    Among other things I run Funny-City.com.

    Lately I have build a members section where someone can register and for now keep favourites and vote on the content.

    My problem is the following.

    When a user signs up I am send an email to him in order to verify that he owes the email he used. Since he can also sign up to the newsletter this is something I need to do.

    After the user fills in the form a function is called that sends the email and then the user is redirected to another page where he sees instructions of what to do with the email he is about to recieve.

    At first I was sending the email through mail which was fast but I noticed a lot of the emails where not getting through.

    Then I started using an SMTP library to send the email. That is when troubles knocked my door.

    Sometimes the SMTP server takes a while to respond (between 5 to 20 seconds) which makes the page loading delay. The user thinks that something went wrong and he stops the page, or reloads or leaves.

    I have tried a cron to send the emails in que every 1 or 2 minutes but it seems silly to use such an intensive procedure in order to send some dozens emails per day.

    There must be a better and more effective way to do this, right?

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Ajax?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess you mean loading the page and making an Ajax call to a php to send the email. I would like to avoid that. What I am asking here is: Is there a way to send an instant email without making the user wait for 1,2,3,5 whatever seconds? It can't be true that the only answer is Ajax, right?

  4. #4
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't speed up the network or the SMTP server. You can fork the process of sending the mail so that it doesn't stall the page. Use exec to call a PHP script that will send the mail and redirect the output of the command to dev/null or a text file.

    That will allow the mail PHP script to continue and then exit while the script that sends the mail continues until it is done. Google for "PHP forking exec" and you will find examples of how to do it.

    Mail delivery is done when it's done. There are far too many networks, routers and servers between your server and any given recipient to have any control over how fast the message actually arrives.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip, it seems like a promising solution.

  6. #6
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    Mail delivery is done when it's done. There are far too many networks, routers and servers between your server and any given recipient to have any control over how fast the message actually arrives.
    I could be wrong but I thought your only waiting for the email to arrive in the SMTP queue rather than waiting for it to be officially sent - as an example when I send a newsletter in batches of 1000 emails at once, the exim mail queue grows while it sends them out, then gradually shrinks as each email is dealt with one way or the other.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  7. #7
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wheeler View Post
    I could be wrong but I thought your only waiting for the email to arrive in the SMTP queue rather than waiting for it to be officially sent - as an example when I send a newsletter in batches of 1000 emails at once, the exim mail queue grows while it sends them out, then gradually shrinks as each email is dealt with one way or the other.
    There could be a number of factors as to why it takes the SMTP server time to respond including network issues. I took what he said to mean that communication in general with the SMTP server was taking too long and causing the page to hang. I only sought to point out that the entire process can be slow and so if the user is lead to expect that things will be quick, they have more of a chance of thinking that something went wrong.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After dealing with the issue for some days now I have decided that forking is the best way to go. I am calling an emailer script and continue loading the page. In that script I make two tries to send the email. If connection to the SMTP is not possible I quit, leaving the email in a queue (in a mysql table with all necessary data). Once every 20 minutes I run a script using cron that tries to send all the unsent emails.

    I think I am covered. And most of all, the user doesn't wait for anything.

    Thanks for the help guys.

  9. #9
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    good to hear you got it working.

    obviously I have no idea what your situation is, but I have personally never seen or heard of so many problems sending SMTP mail... which SMTP library are you using? The other problem might be your hosting is substandard - you might want to upgrade if possible.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not exactly a problem if it takes 2-3 seconds to send an email. But when every account and every possible registered user counts you don't want to loose 10% or 20% that, after waiting for 5 seconds, thought that something didn't work and hit refresh or even worse leave.

    The site is hosted at Mediatemple's Grid and the library is Swift.

    It's all about numbers. When I have e.g. 50 users signing up in a day and 20 of them actually verify their email I have to benchmark and optimize the proccess as mush as possible. I know that out of the remaing 30 users, that didn't verify, probably 20 are robots. But what about the other 10? Is it the copyright? Did they receive their email? Did they have problems with the verification code?

    I guess it's part of the job, right?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •