Forum Moderators: coopster

Message Too Old, No Replies

script to send an email to the user at a time when the user chooses

         

MWpro

5:32 am on Oct 17, 2008 (gmt 0)

10+ Year Member



I want to make a script that, among other things, will let the user pick what time when he wants to receive an email, store this time in a database, and then of course send the email automatically at this specified time.

This script will be for multiple users all picking different times.

This is possible to do with PHP? Would it be reliable?

andrewsmd

12:36 pm on Oct 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can select the times from the database with PHP and see if the current time matches or is greater than the time stored. The problem you run into with PHP is that unless you continuously run it or run it with a scheduler like every minute, you will not be able to just activate it on that time. You would need another program that is specifically designed to run at certain times you specify.

tumr

7:20 pm on Oct 18, 2008 (gmt 0)

10+ Year Member



If you have access to "cron", this is possible.

MWpro

7:43 pm on Oct 18, 2008 (gmt 0)

10+ Year Member



I have cron access.

tumr

7:48 pm on Oct 18, 2008 (gmt 0)

10+ Year Member



Great. If you build a script to email users whose emails are due to be sent, you can then tell cron to run the script:

/usr/local/php5/bin/php5 /home/site/folder/send_email.php

On my server, this tells php5 to run the file specified. (Modify as needed per the location of php and your files on the server.) This is also assuming you're accessing cron through a cpanel-like interface.

MWpro

12:33 am on Oct 19, 2008 (gmt 0)

10+ Year Member



Yes I have cron through cpanel.

But would I run a cron job on the fly, dynamically?

tumr

2:52 am on Oct 19, 2008 (gmt 0)

10+ Year Member



You would set it to run every minute (or whatever), and you'd construct your file so that it would only email anything that was due to be sent.

You'd have to make sure you didn't resend any emails that were already sent once (so $time<=time() wouldn't work). IMO the best approach would be to have a field in the DB called "sent" and set it to 1 once you've sent the email. Then, look for anything due to be sent at or before time() that has sent=0. Send the email, then update the DB to set sent = 1.