Jump to content

Auto send email


benchew0904

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

Maybe I rephrase my question.

This is an eexample of my database

id  |  name  |  email    |    appointment_date

1        John        john@abc.com      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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 = "123@abc.com";
$headers = "From: $from";
$sent = mail($to,$subject,$message,$headers);

if ($sent){
    $statusMessage = "Mail Successfully Sent.";
}else{
    $statusMessage = "Mail Unsuccessfully Sent";
}

?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.