Log in

View Full Version : Complicated Auto Email Script



lucipubl
04-09-2007, 05:59 PM
I'm searching for a script that when executed will (1) take information from a form, (2) take a date from one of the form's fields and subtract two days from it [e.g., if the form field says March 18, 2007 then the script will make that March 16, 2007]; then (3) generate an automated email to be delivered on the revised date to the recipient indicated in the email address form field. Each email will have a tailored message comprised of items from the forms; I do have some experience in your run-of-the-mill forms and formmail scripts.

However, I don't know how to make the script make a date change and then direct my email to run the automated message on that date. Any direction or help would be much-appreciated. Thanks!

boogyman
04-09-2007, 06:11 PM
I am assuming this is probably like some kind of reminder that you want to send 2 days prior to some event?

If you want it to send an email in the future... eg not at time of execution you are going to need to store the data somewhere, most likely in a database then make a script that runs every day at a certain time, and check the day then checks if any mail is supposed to be sent that day, then send it accordingly

that is the only way I know how, but maybe someone else knows of how to do something like this more efficiently

mburt
04-09-2007, 06:28 PM
Or... store all the address somewhere, and when you need to send it, call a page which runs a function to send it. Similar to the mail(); function with PHP.
Heck, I could even set up a dynamic page which counts down the days, then submits a form.
There is several ways of doing this, but we'll need more info on how you want it done.


However, I don't know how to make the script make a date change
Hmm?

($mydate)-2

lucipubl
04-09-2007, 06:31 PM
pissa,
thanks for the reply! that makes sense; running it in a database. should the script i write be executed on the webpage or implemented in the database itself? as in, do i write a script that sends the new info the the database, then a separate script within the database that sends the email? I'm fairly certain i can configure the database to work with my email system.

thank you for taking the time to answer my question. i'll see if this works, otherwise i'll continue my research and will be open to any other suggestions. Thanks!

lucipubl
04-09-2007, 06:36 PM
thanks mburt - that gives me another option. basically, i'm designing a site for a client and venturing for the first time into this particular situation. it will work like this: (1) someone orders a product for another person from my client's website; (2) two days before that product is to be delivered, the person who ordered the product will get an automated email reminding them that their order will be delivered to the recipient in two days.

sounds simple, I know, and I've previously contracted coders to do this kind of stuff for me since i'm more of a graphic designer. however, i'm trying to learn more and more myself so i can do the whole thing start to finish.

mburt
04-09-2007, 06:42 PM
implemented in the database itself?
Databases can't run functions, sorry :).


do i write a script that sends the new info the the database, then a separate script within the database that sends the email? I'm fairly certain i can configure the database to work with my email system.
If you're able to do that, it should work. Remember though, that scripts aren't stored in a database, the e-mails are.

boogyman
04-09-2007, 06:45 PM
well what i would do is write it all into one script. info/data is generally the only thing that is stored in the database (data tier). You would need to write a script to perform the two functions, and I would say write it all into one file.


($mydate)-2
That works for the 3rd on in each month. if its the 1st or the 2nd you would need to take into account the month also, and since we have 365.25 days per year, if the month is march (month after feb) you would need to check if its a leap year.

Depending on how global/generalized you want to make this, you could hard code leap years into an array, but ideally you really should make it so it doesnt matter what date/year the script is ran, whether it be 2007 or 3007

mburt
04-09-2007, 06:49 PM
That works for the 3rd on in each month. if its the 1st or the 2nd you would need to take into account the month also, and since we have 365.25 days per year, if the month is march (month after feb) you would need to check if its a leap year.

Depending on how global/generalized you want to make this, you could hard code leap years into an array, but ideally you really should make it so it doesnt matter what date/year the script is ran, whether it be 2007 or 3007
Point taken. I'd say you should just, as you said hardcode the offset values into an array. There is some mathematical formula I'm sure, somewhere.

lucipubl
04-09-2007, 06:55 PM
okay, I think I have enough overview here. Thanks! Just one more question - since the form submit will be the triggering event to put the info into the database, what should be the triggering event to make the email actually send? should it be written so that it immediately (upon form submission) sets up the db/email to generate a 'send at a later date' email? which leaves me with much more research to do, unless there's a triggering event I'm overlooking that would prompt it (like launching something every day?).
Again, thanks for your help. By the way, any suggestions on databasing resources (book titles, websites, etc.)?

boogyman
04-09-2007, 07:25 PM
you would need to structure your database so that it stores all of the data that is given in the form like

name , email, product,,, yada yada yada would all be records that you would need




if(form_submitted) {
check the date
if date is within 2 days before estimated arrival {
get info
send email
}
else {
if ( connect to database) {
if ( able to write ) {
write data
success message
}
else {
give error
}
else {
unable to connect
}
}
}
else {
print form
}


there is some psuedo code for when the for is initially made... you would need to write a seperate script that would run every day, preferably in the morning probably, that would check the date and would search your database for the date anticipated arrival, and use a "foreach loop" to populate and send each email accordingly.


www.w3schools.org << standards / online tutorials
www.http://dev.mysql.com/doc/refman/5.0/en/ <<mysql manual / tutorial

mburt
04-09-2007, 07:29 PM
When using databases, I would always connect at the very first of the script, just to ensure there are no leaks.