SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Email Response Add Delay

    Hi guys,

    I've created a PHP form for my website and have added an email response to it to be sent to the user once they've submitted their details. What I'd like to do is add in a delay of some sort so that the user doesn't immediately receive the email. I'd like to pretend that it's a real person emailing them back instead of them receiving an email 2 seconds after clicking the submit button.

    Any ideas how I'd do that?

    Many thanks

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by mike52321 View Post
    Hi guys,

    I've created a PHP form for my website and have added an email response to it to be sent to the user once they've submitted their details. What I'd like to do is add in a delay of some sort so that the user doesn't immediately receive the email. I'd like to pretend that it's a real person emailing them back instead of them receiving an email 2 seconds after clicking the submit button.

    Any ideas how I'd do that?

    Many thanks
    As long as you're storing the data you need to create the email in a db, you can create a cron job to query for rows that have not been sent yet, send your emails, mark them as sent.

  3. #3
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Easy solution
    PHP Code:
    sleep(300); 
    this will wait for 5 minutes.
    What I lack in acuracy I make up for in misteaks

  4. #4
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lorenw View Post
    Easy solution
    PHP Code:
    sleep(300); 
    this will wait for 5 minutes.
    So that's without the need for db right? And can that be placed anywhere or in specific place in php script?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Using sleep (300) means the user also has to wait for 300 seconds for the request to complete, which surely is not what you want (apart from the fact that PHP will time out after 30 seconds...)

    So I would go with the cron solution. Or, you can implement a message queue and give the message a random delay, but that's a lot more involved to implement.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by lorenw View Post
    Easy solution
    PHP Code:
    sleep(300); 
    this will wait for 5 minutes.
    End user sleep aside, this will also create system over head due to processes continuing to run. This is just bad practice in any language.

  7. #7
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,104
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I learned something.
    What I lack in acuracy I make up for in misteaks

  8. #8
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by lorenw View Post
    I learned something.
    An exception to this is in a language with concurrency. In Golang I might create a new function and start a new goroutine (sort of like a thread) when the server starts. This threaded function would then check for emails to be sent that were either stored in as shared variables memory (not possible in PHP) or read from a db, then sleep for x amount of time. This would ensure that only one process is running to do this action, not many.


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
  •