Mushka has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I wanted to ask your advice on whether it's possible to achieve delay delivery for email similar to what Outlook provides. I need to create an email entity now and "ask" the server to deliver it, say, 5 hours later. I was wondering if there was an easier way to do it than storing the generated email somewhere (file, database) and periodically checking it with a script to see if the time has come to actually send it out.

Looking forward to your ideas!

Replies are listed 'Best First'.
Re: Email delay delivery option with Perl
by ww (Archbishop) on Feb 24, 2012 at 17:37 UTC
    How would you make the message (aka "email entity," I presume) persist from the time you create it until time to send it, unless you save it somewhere?

    Perl has magic and Magic, but is still slow at "the inconceivable."

Re: Email delay delivery option with Perl
by kennethk (Abbot) on Feb 24, 2012 at 17:43 UTC
    Sure. Just make your code sleep. I'd think that 'storing' your data may be a better idea depending on how robust a solution you want/need.
    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
Re: Email delay delivery option with Perl
by RichardK (Parson) on Feb 24, 2012 at 19:01 UTC

    You could use the at command to do something later.

    at now +5h send_message.sh # or at teatime send_message.sh

    I think that even windows has an at command ;)

Re: Email delay delivery option with Perl
by Mushka (Acolyte) on Feb 24, 2012 at 18:44 UTC
    Thanks for your responses!

    Ww, im not sure how Outlook does it. I was hoping that i could give my message (ie "email entity" %-) ) to the mail server and tell it to hold it until specified datetime. I was hoping that the server would store it until the later date, not that i would store it till i want to send it.

    Ken, i thought of sleeping too, but unfortunately it isnt a good idea in this case, i think. The script would need to be run every time i want to send a delayed message, and if the previous instance of the script is sleeping it would create a 2nd running instance. Then the 3rd, 4th etc etc. It makes me wary to have so many sleeping scripts. Besides, if the server needs to be restarted (which does not happen often, but enough to take it into consideration), all those emails will be lost.
      Is the mailserver yours (and yours exclusively)?

      This would still be storing the message "somewhere" -- which you excluded in your OP -- but if you own the mailserver, it should not be all that hard to modify your existing code (or code in Mail::Sendmail to interpose a delay between creation and transmission. Just for example, countdown timers are a well-established, well-documented, and simple technology.

      Like I said, it depends on how robust a solution you want/need. If you need a more robust system, I would stash e-mails in a database along with a send time. Then I'd run a cron job that looked in the database for messages that were ripe to send. The message has to persist between generation and sending somehow; sleep keeps in in memory and either a database or possibly Storable stashes it on disk.
      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
Re: Email delay delivery option with Perl
by Mushka (Acolyte) on Feb 24, 2012 at 19:42 UTC
    Ww, the mail server isnt mine and i have no admin access to it, but i like your train of thought!

    Richard, thanks for the tip!! I did not know about the "at" command. I'm gonna explore this approach.