Search code examples
phpsendmailinfusionsoft

To send email on some future time


I am new to php, I am making a schedule page which is getting data form database. From database I am picking the send time and dates of the emails which have come. What am trying to do is that, if the send date and time has come the system should send email to that address.

For email sending i am using this API.

This is my Code what I should add in this to perform such functionality. Am trying to get 3 things. 1. to fetch array from database whose time has been reached. 2. storing them in array using loop. 3. sending them email through a loop. This is my 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 communication.senddatetime,emails.email
                        FROM communication
                        INNER JOIN emails
                        ON communication.communication_id=emails.communication_id
                        WHERE senddatetime <= NOW()'); //or die(mysql_error())
 while($row = mysql_fetch_assoc($query)){
 $email=array($fetch);
 $result=mysql_num_rows($query);
 for($i=0; $i<$result; $i++){
 $obj=mysql_fetch_object($query);
 $emailaddress=$obj->email;
 }

 for($i=0; $i<$result; $i++){
 $conDat = array('FirstName' => 'Hassan',
                          'Email'     => $email);
        $conID = $myApp->addCon($conDat);
        $clist = array($conID);
$email = $myApp->sendEmail($clist,'[email protected]','~Contact.Email~','ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');}
}

?>

Solution

  • You should use cron jobs.

    First, you must create a php script whose look if there is any email to be sent (it can be a simple php file that get the time and that compare it with a timestamp in your database, then get info about the email you want to send, compose the email with your headers, subject and body and finish it by sending it.)

    Second you must execute it in shell (I assume you're working with a Linux system) to test if it is working ok. To execute a php in shell you can use the next command

    php /route/to/your/php/script.php var1 var2 .... varN
    

    Maybe the command php could not work so you must to know which php client you have installed (very important, you must first have installed a php client in your system -- apt-get install php5-cli). You can know which php have you installed in your system typing the next in a shell

    which php
    

    You will find more information about how to exec php in shell in the url Shell run/execute php script with parameters

    And the third thing you must to do is to program the script in cronjob. For example, if you want to execute it in 5 minutes intervals you could write the next in your cron (crontab -e)

    */5 * * * * php /route/to/your/php/script.php var1 var2 > /dev/null
    

    Or

    */5 * * * * /usr/bin/php /route/to/your/php/script.php var1 var2 > /dev/null
    

    You have other option to make a php an exec file and so you mustn't use php before your script and it's to add the next line before you open your php

    #!/usr/bin/php
    <?php