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,'kamranasadi431@gmail.com','~Contact.Email~','ccAddresses', 'bccAddresses', 'contentType', 'JK', 'htmlBody', 'txtBody');}
}

?>
有帮助吗?

解决方案

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

其他提示

Another way to schedule an email to be sent at a later time is by way of a system command combining sendmail with at, like so:

/usr/sbin/sendmail -f from@from.com to@to.com < message.txt | at 16:00 today

This may be a little cleaner than running a cron job every few minutes that queries a database for emails that need to be sent. You can run the above system command from your PHP script using php's shell_exec function.

If you don't have a server available on your end 24/7, you can also take the advantage of the cloud for your purpose.

An easy way to set-up this, is using Jenkins on a IaaS, or on PaaS, where you just need to create your job and specify when you would like to launch it, without installing or configuring anything.

A cron job configuration could be done in the way it is specified here. Complete step by step guide could be also find here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top