To send email on some future time

I am making a php script which fetches the data of the records whose sending date and time has been reached, and then sends email to those records.
I want to do the following steps.
Step 1: get the communication records whose time has reached.
Step 2: run the loop of above and get the email addresses.
Step 3: run the loop of email addresses and send emails.

This is my php code…

<?php include('iSDK/src/isdk.php'); $myApp = new iSDK(); // Test Connnection if ($myApp->cfgCon("connectionName")) { echo "Connected..."; } else { echo "Not Connected..."; } $query = mysql_query('SELECT * FROM `emails` where senddate = NOW()'); $email=array(); $row = mysql_fetch_array($query); $count=mysql_num_rows($query); for($i=0; $i<$count; $i++){ $emailadd = $email['email']; for($i=0; $i<$count; $i++){ $conDat = array('Email' => $emailadd); $conID = $myApp->addCon($conDat); $clist = array($conID); $email = $myApp->sendEmail($clist,'[email protected]','~Contact.Email~','ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody'); } } if(isset($email)) { echo "Email Sent Successfully"; } else{echo"Email Sent Failed";} ?>

any one tell me whats should be changed in this code?

You are using deprecated code. Use PDO or Mysqli and we will be happy to help you.

I have provided a complete PDO database download to get you started in the right direction. Download it, set your login credentials and import the sql for the database.

If you are still having trouble at that point I will be glad to help.

http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

I have downloaded PDO but don’t know how to use, am at beginner level, just started , so please guide me with some easy steps.
Thanks for replying.

Sure. Have you created the database and imported the sql to mysql? The import file is in the sql folder. You can use phpmyadmin or any other mysql manager to import it. In the file config.php set the username and password to your database. Also set $admin_email to your email so you can test the form to email. Thats all there is for it to work. When you open index.php in your browser you should see output from the database. Let me know when you are at this point and we can go from there to do what you want.

Yes I have done all those steps.
Now ?

Post your database sql including sample records so I can recreate your database on my end. I will make sure you are starting with a proper DB design and will be able to step through this in sync with you.

This is my database


ticket_inspector_new(1).zip (34.2 KB)

  • UPDATED ANSWER

There are a few issues with the database, but we can get to that later. The first problem in sending the emails is your query. NOW() says if it is the exact second ONLY to send the email. It should be less than than or equal to NOW(). This is the format of NOW() - 2008-11-11 12:45:34. The value is the exact time it is at any given moment.

Going with what you have, Query should be

SELECT * FROM `emails` where senddate <= NOW() AND status ='pending'

As I understand it, the end game is to send an email when we have reached or exceeded send date, correct?

yes correct you are.
email is to sent when its time has reached.

This will handle sending the emails. See if you can do the update from pending to sent based on this code.

[php]<?php
/* SITE: PHPHelp
USER: kamran431
Ver 1.0
*/

$hostdb = ‘localhost’;
$dbname = ‘ticket_inspector_new’;
$username = ‘root’;
$password = ‘’;

try
{
$pdo = new PDO(“mysql:host=localhost;dbname=$dbname”, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql  = "SELECT * FROM `emails` where senddate <= NOW() AND status ='pending'";
$stmt = $pdo->prepare($sql);
$stmt->execute();

$result = $stmt->fetchAll();

if (count($result))
    {
    foreach ($result as $row)
        {
        echo '<pre>';
        print_r($row);
        echo '</pre>';
        mail("{$row['email']}",$subject,$message,"From: [email protected]\n");
        }
    }
else
    {
    echo "No rows returned.";
    }
}

catch (PDOException $e)
{
echo 'ERROR: ’ . $e->getMessage();
}
?>[/php]

To send email I Am using an API.
https://github.com/infusionsoft/PHP-iSDK
How I can integrate this code with this API sendEmail function.

$clist = array(123,456,789);
$status = $app->sendEmail($clist,"[email protected]","~Contact.Email~", “”,"",“Text”,“Test Subject”,"",“This is the body”);
Thanks again for such guidelines, & help.

What is the reason you are using that api?

I am working on a project, so it is the requirement to use this API.

Sir, I waiting for your response.
Respond me as soon possible.

Sponsor our Newsletter | Privacy Policy | Terms of Service