hello
i am trying to write a script where users can insert some text into my database to be sent as an email at a specific time in the future can someone pls advise me on how to approach this.
i need to insert the time into the database when the users wants the email to be sent. but how do i get the database to act upon that time and automatically send the text as an email.
any help would be really appreciated.
send mail
Moderator: General Moderators
Re: send mail
You can typically do this with a trigger
Re: send mail
does anybody have any good resources for a PHP trigger tutorial.
Re: send mail
let me try to help you.gum1982 wrote:does anybody have any good resources for a PHP trigger tutorial.
Firstly, before starting, I assume that in your database, you'll have these 4 columns. I am naming that table as `email`
1) Email of receiver. = `email` (Suppose)
2) Time at which you want to send the mail. `send_time` (Suppose)
3) Email message. `message` (Suppose)
4) Email subject. `subject` (Suppose)
Now, you need to put up a cronjob. Which should be something like this.
Code: Select all
<?php
include_once (includes/database-connection.php); //This mean your database connection file, if you want to do this manually, you can do that as well.
$timenow = time(); // This step is not necessary though.
//Now we have to see if time now = time at which we need to send the mail.
$query = mysql_query("SELECT * FROM email WHERE send_time='$timenow';");
while($query=mysql_fetch_array($query);) {
$email = $query['email'];
$message = $query['message'];
$subject = = $query['subject'];
$headers = //Include header info here..
mail($email, $subject, $message, $headers, -$from);
}
?>So, each minute it'll check if there is a message which needs to be delivered or not.
Hope this will help you. Do reply.
Re: send mail
hi im pretty new to php so i hope you can bare with me.
what format do i parse the date and time into my database. ive setup a simply database with a table called send with 5 fields.
email = varchar
time = time
message = text
subject = varchar
date = date
my form is simply
And my insert.php is as follows.
so i have my information in the database does your script run in the background and keep checking the database.
im stuck on the time and date formats.
thanks for the reply.
what format do i parse the date and time into my database. ive setup a simply database with a table called send with 5 fields.
email = varchar
time = time
message = text
subject = varchar
date = date
my form is simply
Code: Select all
<form method="POST" action="insert.php">
E-Mail:
<input type="text" name="email" id="email" size="19"><br>
<br>
<br>
Message:<br>
<textarea rows="9" name="message" id="message" cols="30"></textarea><br>
<br>
Subject:
<input type="text" name="subject" id="subject" size="19"><br>
<br>
Date to be sent:
<input type="text" name="date" id="date" size="19"><br>
<br>
Time:
<input type="text" name="time" id="time" size="19"><br>
<br>
<input type="submit" value="Submit" name="submit">
</form>
Code: Select all
<?php
include "conninfo.php";
$email = $_POST["email"];
$message = $_POST["message"];
$subject = $_POST["subject"];
$date = $_POST["date"];
$time = $_POST["time"];
$query = "INSERT INTO send (email, message, subject, date, time) VALUES ('$email', '$message', '$subject', '$date', '$time')";
mysql_query($query) or die('Error, insert query failed');
?>
im stuck on the time and date formats.
thanks for the reply.
Last edited by Benjamin on Fri May 29, 2009 10:57 am, edited 1 time in total.
Reason: Added [code=php] tags.
Reason: Added [code=php] tags.
Re: send mail
Code: Select all
<form method="POST" action="insert.php">
E-Mail:
<input type="text" name="email" id="email" size="19"><br>
<br>
<br>
Message:<br>
<textarea rows="9" name="message" id="message" cols="30"></textarea><br>
<br>
Subject:
<input type="text" name="subject" id="subject" size="19"><br>
<br>
Message to be sent after :
Days: <br>
<input type="text" name="day" id="min" size="10"><br>
<br>
Hours: <br>
<input type="text" name="hrs" id="hrs" size="10"><br>
<br>
Minutes: <br>
<input type="text" name="min" id="min" size="10"><br>
<br>
<input type="submit" value="Submit" name="submit">
</form>
Code: Select all
<?php
include "conninfo.php";
$email = $_POST["email"];
$message = $_POST["message"];
$subject = $_POST["subject"];
$min = $_POST["min"];
$hrs = $_POST["hrs"];
$day = $_POST["day"];
if($min == 0) { $min='1'; }
if($hrs == 0) { $hrs='1'; }
if($day == 0) { $day='1'; }
$date = 60*$min*$hrs*$day;
$time = time() + $date;
$query = "INSERT INTO send (email, message, subject, time) VALUES ('$email', '$message', '$subject', '$time')";
mysql_query($query) or die('Error, insert query failed');
?>
Regards,
Anand
Last edited by Benjamin on Fri May 29, 2009 10:58 am, edited 1 time in total.
Reason: Removed [quote] of entire post.
Reason: Removed [quote] of entire post.