benchew0904 Posted December 3, 2011 Share Posted December 3, 2011 Hi I need to auto send an email to notify user about their next appointment date. Anyone know how to do the auto send email coding or it requires any external php class to do so? I'm using php and xampp. Thanks Ben CHew Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/ Share on other sites More sharing options...
Errant_Shadow Posted December 3, 2011 Share Posted December 3, 2011 The best way I know to have a server automatically execute a script is to use cron jobs with MySQL. Basically you write a script that grabs the data you want and does something with it; I imagine in your case it will grab anyone with an appointment within the next x# hours/days/whatever and parse all of there info into a nice little e-mail, then send them all out. Once that's done, you set up a cron job (do some google searches, there are a bunch of tutorials) to execute that script on a specific schedule. Do you need info about how to execute those database searches, parse the data, or use the mail() function? Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293842 Share on other sites More sharing options...
benchew0904 Posted December 3, 2011 Author Share Posted December 3, 2011 I know how to send an email but i'm not not very sure about cron job. I have googles before but I'm still very unsure of how to use cron job. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293854 Share on other sites More sharing options...
trq Posted December 3, 2011 Share Posted December 3, 2011 Cron has nothing to do with php. Unless you have shell access to your server, it is likely that your host provides some form of interface to cron (or you might not be able to use cron at all). Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293855 Share on other sites More sharing options...
benchew0904 Posted December 3, 2011 Author Share Posted December 3, 2011 Errant_Shadow, how to write a script to grabs the data that I want and workg with it? Basically you write a script that grabs the data you want and does something with it; I imagine in your case it will grab anyone with an appointment within the next x# hours/days/whatever and parse all of there info into a nice little e-mail, then send them all out. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293865 Share on other sites More sharing options...
scootstah Posted December 3, 2011 Share Posted December 3, 2011 If you host allows it you should be able to add cron jobs dynamically with exec() or shell_exec(). Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293942 Share on other sites More sharing options...
Glese Posted December 3, 2011 Share Posted December 3, 2011 benchew, you did not state what the initiation for the automatically sent email is, nevertheless you can simply create a PHP script concluding into the mail() function. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1293975 Share on other sites More sharing options...
benchew0904 Posted December 4, 2011 Author Share Posted December 4, 2011 Sorry, I only know about the basic of php. Mind explaining in simpler term of what you mean by exec() or shell_exec()? So sorry. If you host allows it you should be able to add cron jobs dynamically with exec() or shell_exec(). Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294153 Share on other sites More sharing options...
benchew0904 Posted December 4, 2011 Author Share Posted December 4, 2011 Maybe I rephrase my question. This is an eexample of my database id | name | email | appointment_date 1 John [email protected] 2011/12/05 I would like to auto send an email to notify user 2 days before their scheduled appointment date. I know how to send a normal email using php but unsure of auto send. benchew, you did not state what the initiation for the automatically sent email is, nevertheless you can simply create a PHP script concluding into the mail() function. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294154 Share on other sites More sharing options...
Glese Posted December 4, 2011 Share Posted December 4, 2011 I know how to send a normal email using php but unsure of auto send. benchew, you did not state what the initiation for the automatically sent email is, nevertheless you can simply create a PHP script concluding into the mail() function. Again, you did not mention the initiation, without it a suggestion for a solution is a guess game. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294183 Share on other sites More sharing options...
benchew0904 Posted December 4, 2011 Author Share Posted December 4, 2011 Sorry, what do you mean by "the initiation"? Again, you did not mention the initiation, without it a suggestion for a solution is a guess game. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294189 Share on other sites More sharing options...
trq Posted December 4, 2011 Share Posted December 4, 2011 To auto send an email you would need to execute a script daily, hourly or whatever (this would need to be scheduled via cron). That script would then query your database for all records where the "scheduled appointment date" is two days from the time the script executes. You would then send the email to all the email addresses returned by this query. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294191 Share on other sites More sharing options...
benchew0904 Posted December 4, 2011 Author Share Posted December 4, 2011 How do I create a cron script? And is it that I need to add the cron script in my php mail (shown below)? To auto send an email you would need to execute a script daily, hourly or whatever (this would need to be scheduled via cron). That script would then query your database for all records where the "scheduled appointment date" is two days from the time the script executes. You would then send the email to all the email addresses returned by this query. <?php include 'config.php'; $sql = executeSelectQuery($MYSQL); if($sql){ $to = $_POST ['email']; $subject = "Appointment Date"; $message = "Hello! Please be informed that your next appointment is on $appointment_date"; $from = "[email protected]"; $headers = "From: $from"; $sent = mail($to,$subject,$message,$headers); if ($sent){ $statusMessage = "Mail Successfully Sent."; }else{ $statusMessage = "Mail Unsuccessfully Sent"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294214 Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 CRON scripts are not something you can do with PHP alone. You need your server to run the CRON scrips for the nth time. If your server does not offer that option, you can emulate it by placing the script to run the emails on pages on the web site. This way, whenever someone hits say index.php your CRON script would execute to see if anything should be sent. This is not very efficient. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294215 Share on other sites More sharing options...
Errant_Shadow Posted December 4, 2011 Share Posted December 4, 2011 Here's the basic logic you'll need, but the specific details can be found in many tutorials around the web. (NOTE: All code here is pseudo-code, not meant to actually work) First, you write a script to fetch the data you need require_once("database_connect.php"); // this is1 where you hold the connection details to your database // for this exercise, we'll assume the connection variable is called $con and that you connected to the appropriate database within that script $today = date("Y-m-d"); // creates a variable for the current date (and time) $1Day = date_add($today, new DateInterval('P1D')); // creates a variable for 1 day from today $2Day = date_add($today, new DateInterval('P2D')); // creates a variable for 2 day from today // this is assuming you will be telling everyone who has an appointment any time tomorrow // basically, you'd run this script at 1am // $1Day would be the date and time for 1am tomorrow and // $2Day would be the date and time for 1am the following day // then all you'll need to do is fetch all appointments between those date/times $query = "SELECT * FROM clients WHERE appointment > $1Day AND appointment < $2Day "; // appointment is GREATER than (past) 1am tomorrow but LESS than (before) 1am the next day $results = mysql_query($query, $con) or die(mysql_error()); // $results is not the actual data, you will need to exrtact the data to access it // people say it's poor form to use that die command // but for the purposes of this exercise, it will help you get it squared away // this will cycle through each record that the query fetched while($client = mysql_fetch_array($results)) { // and run your e-mail script here } Once you have that script working, you'll need to talk to your web host service and ask them how you can add cronjobs to your database. If they can't or don't allow you that level of access, maybe you should switch providers. If they provide you a site and database through them, you should have access to it. Quote Link to comment https://forums.phpfreaks.com/topic/252371-auto-send-email/#findComment-1294377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.