send mail

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

send mail

Post 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.
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

Re: send mail

Post by lmhart »

You can typically do this with a trigger
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Re: send mail

Post by gum1982 »

does anybody have any good resources for a PHP trigger tutorial.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: send mail

Post 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.
gum1982
Forum Newbie
Posts: 24
Joined: Tue Dec 16, 2008 11:15 am

Re: send mail

Post 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.
Last edited by Benjamin on Fri May 29, 2009 10:57 am, edited 1 time in total.
Reason: Added [code=php] tags.
anand
Forum Commoner
Posts: 80
Joined: Fri May 22, 2009 11:07 am
Location: India
Contact:

Re: send mail

Post 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
Last edited by Benjamin on Fri May 29, 2009 10:58 am, edited 1 time in total.
Reason: Removed [quote] of entire post.
Post Reply