To send the email on some given date and time I am making a PHP script which fetches the data of the emails whose sending date and time has been reached and then send emails to those records. I am following the steps below and integrating it in an API, that is.

GitHub code

Steps: 


Get the communication records whose time has reached.
Run the loop of above and get the email addresses.
Run the loop of email addresses and send emails.


This is my PHP code:

&lt;?php
include('iSDK/src/isdk.php');
$myApp = new iSDK();
// Test Connnection
if ($myApp-&gt;cfgCon("connectionName")) {
    echo "Connected...";
} else {
    echo "Not Connected...";
}
$query = mysql_query('SELECT * FROM `emails` WHERE `senddate` &lt;= NOW()');
while ($row = mysql_fetch_assoc($query)) {
    $conDat = array('Email' =&gt; $row['email']);
    $conID = $myApp-&gt;addCon($conDat);
    $clist = array($conID);
    $email = $myApp-&gt;sendEmail($clist, 'kamranasadi431@gmail.com', '~Contact.Email~',
        'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
}
?&gt;


What changes should be made in it?
Use PDO, not mysql_* as they are deprecated and dangerous.
Break up your code into functions, this makes it easier to call and test.
Throw and catch exceptions.
You can omit the ending ?>


NOTE: This is untested code as I don't have the isdk.php file.

    &lt;?php

    require('iSDK/src/isdk.php');

    $iSDK = new iSDK();

    try{
        checkConnection($iSDK);
        performQuery($iSDK, getDBConnection("", "", "", ""));
    } catch(RuntimeException $e){
        die($e-&gt;getMessage());
    }

    function checkConnection(&amp;$iSDK){
        # Throw an exception if we don't have a connection, otherwise return true
        if (!$iSDK-&gt;cfgCon("connectionName")) {
            throw new RuntimeException('Could not obtain connection.');
        }
    }

    function performQuery(&amp;$iSDK, $PDO){
        $sth = $PDO-&gt;prepare("SELECT * FROM `emails` WHERE `senddate` &lt;= NOW()");
        $sth-&gt;setFetchMode(PDO::FETCH_ASSOC); # setting the fetch mode
        $sth-&gt;execute();

        while($row = $sth-&gt;fetch()) {
            $conDat = array('Email' =&gt; $row['email']);
            $conID = $iSDK-&gt;addCon($conDat);
            $clist = array($conID);
            $email = $iSDK-&gt;sendEmail($clist, 'kamranasadi431@gmail.com', '~Contact.Email~', 'ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');
        }
    }

    function getDBConnection($host, $dbname, $user, $pass){
        try {
            return new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
        } catch(PDOException $e) {
            die($e-&gt;getMessage());
        }
    }