Page 1 of 1

send mail

Posted: Thu May 28, 2009 11:14 am
by gum1982
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.

Re: send mail

Posted: Thu May 28, 2009 11:26 am
by lmhart
You can typically do this with a trigger

Re: send mail

Posted: Thu May 28, 2009 12:09 pm
by gum1982
does anybody have any good resources for a PHP trigger tutorial.

Re: send mail

Posted: Thu May 28, 2009 12:36 pm
by anand
gum1982 wrote:does anybody have any good resources for a PHP trigger tutorial.
let me try to help you.

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);
}
?>
Now, add a cronjob which will execute every 1 minute.

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

Posted: Thu May 28, 2009 2:13 pm
by gum1982
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

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>
 
And my insert.php is as follows.

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');
 
?>
 
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.

Re: send mail

Posted: Thu May 28, 2009 2:58 pm
by anand

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');
 
?>
 
Its 1:30AM in India, I need to go to bed. If you have any further confusion, you can add me vis yahoo/msn in anandghosh2002 ( Thats my id )

Regards,
Anand