This topic is locked

Sample script to send email on timely fashion

7/14/2010 8:40:21 AM
PHPRunner Tips and Tricks
J
Jane author

Let's say there is an ExpiryDatefield in the table with events.

To send email if event was expired create new php file (dailysendmail.php for example), place this file under generated output directory.

Here is a sample script (for MySQL database):

<?php

include("include/dbcommon.php");

global $dal;

$dalTableName = $dal->Table("TableName");

$rstmp = $dalTableName->Query("DateDiff(now(),ExpiryDate)>0","");;

while ($datatmp = db_fetch_array($rstmp))

{

//send email for each record

$email=$datatmp["EmailField"];

//EmailField is your actual field name

$msg="This event was expired\r\n";

$msg.="FieldName1: ".$datatmp["FieldName1"]."\r\n";

$msg.="FieldName2: ".$datatmp["FieldName2"]."\r\n";

$subject="Expired";

runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg));

}

//FieldName1, FieldName2 and ExpiryDate are your actual field names, TableName is your actual table name

?>


To apply this sample for MSSQL database edit SQL query in the following way:

$rstmp = $dalTableName->Query("DATEDIFF(day, GETDATE(), ExpiryDate)>0","");



and for MS Access:

$rstmp = $dalTableName->Query("DateDiff('d', Now(), ExpiryDate)>0","");


Then use cron to run this script on timely fashion i.e. once a day.

You can use SetCronJobservice to run this script if cron is disabled on your web server:

http://www.setcronjob.com/'>http://www.setcronjob.com/
Here is a step-by-step instruction on how to set up cron job usign SetCronJob:

  1. register on the http://www.setcronjob.com/'>http://www.setcronjob.com/
  2. activate your account and login,
  3. go to Control Panel and click on the 'Create new cron job' link,
  4. fill 'URL to call' with correct URL to your script. For example your generated files are under 'phprunner' directory:

    http://yourhosting.com/phprunner

    So correct URL will be:

    http://yourhosting.com/phprunner/dailysendmail.php
  5. Save.

U
unsearcheable 5/9/2011

Great tip, but how can i add fields from another table ? Put all the select statement in query ?

Sergey Kornilov admin 5/9/2011

Use DAL's CustomQuery() function and put any SQL quey you want there.

U
unsearcheable 5/9/2011

Thanks.

O
oermops 9/18/2011

I have a similar situation. As a hobby I am involved with a train museum where I also maintain the personnel records. We have several tests that expire after a few years. I would like to have our engineers be notified by email when their test(s) have expire. Different tests have different expiration dates and need to have a pair of fields for each test. For example, test1 + 1 year =test1expired; test2 + 3 years = test2expired; test3 + 5 years = test3expired.... You get the idea. I am running MYSQL and PHPRunner doesn't accept the UPDATE commands I have tried to enter in the SQL editor.
How can I calculate the expiration dates for these tests and have my engineers receive emails when their tests have expired?

K
Kevin 11/23/2011

I have this script which will monitor oracle db process if up or down.And I want it to send email if it's down and the time it's back to online. However my script just keep on sending "Email Up" if the db is up or "Email Down" if the db is down.Is there any way to trap it so that it would only send Email Up or Down once?
This is script will not be run on cron jobs, I'm running it in background.

here is the sample script:
Code:

!/bin/sh

while :

do

db_check=ps -ef|grep ${ORACLE_SID}|grep pmon|wc -l

status=expr $db_check
if [ $status -lt 1 ];

then

echo "Down" > log.txt

else

echo "Up" > log.txt

fi
log_check=cat log.txt | grep Up
if [ "$log_check" = "Up" ];then

echo "Email Up"

else

if [ "$log_check" = "Down" ];then

echo "Email Down"

fi

fi

done

A
Anapolis 4/1/2014

[size="3"]Hello Jane and Sergey,
I am going to try this script with a project where an appointment table has 2 date fields -- '14days' and '30days' in '2014-08-21' format.
My script in the events.php for this form calculates the appointment date minus 14 days and minus 30 days and updates the appointment table in those two fields for each appointment (which are typically set weeks or months later than today's date).
If the appointment date is 2014-07-21 then the 30days field is 2014-06-21, the 14 days field value is 2014-07-07.
A special email goes out 30 days before and another email 14 days before.
I want the script to run perhaps 3 times a day, responding to new appointment entries over the working day.
But, and here's my question:
What is the most logical way to EXCLUDE the same record from being emailed multiple times Today if the cron job runs 3 or 4 times a day?
If I ran the job once every 24 hours that would automatically take care of it. But I don't want a big backlog of emails waiting to all have to run on one execution.
I thought of adding 2 fields to accompany the 14days and 30days date fields that would be written to on an Update/Set query after the cron job executes an email.
If the record's 30 days field triggers an email then the followup would be writing a value such as "yes" or "done" in "30confirm" or '14confirm'.
The email script then filters with ".. WHERE 30daysconfirm != 'done'" or something of that nature.
Since I am suspicious of my own "solution" is there a better solution to prevent the same email from multiple execution during the same day when the cron job executes 3 or 4 times that day?
Thank you for suggestions or a response!

[/size]

Sergey Kornilov admin 4/1/2014

There are thousands of ways to handle this. Probably the easiest one is to separate email generation and the the actual email sending processes.
Run cron job once a day to generate emails. Instead of sending emails right away store them in a separate table. Run another cron job every few hours or minutes to send the next batch of emails say 1000 at a time. Scan the table with generated emails, send them out 1000 at once, delete those emails from the queue and enjoy.

A
Anapolis 4/1/2014



There are thousands of ways to handle this. Probably the easiest one is to separate email generation and the the actual email sending processes.
Run cron job once a day to generate emails. Instead of sending emails right away store them in a separate table. Run another cron job every few hours or minutes to send the next batch of emails say 1000 at a time. Scan the table with generated emails, send them out 1000 at once, delete those emails from the queue and enjoy.


Thank you, Sergey!
I have looked all around at scripts and tutorials but I haven't found an explanation of "storing generated emails".
Using a PHPRunner example --

global $dal;
//select emails from Users table
$tblUsers = $dal->Table("UsersTableName");
$rs = $tblUsers->QueryAll();
while ($data = db_fetch_array($rs))
{
$email.=$data["EmailAddress"].", ";
$from="admin@test.com";
$msg="Check what's hot this season";
$subject="Monthly newsletter";
$ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from));
if(!$ret["mailed"])
echo $ret["message"]."
";
}


is each loop going to a table with an auto_increment id and then the generated result of $ret=runner_mail(array('to' => $email, 'subject' => $subject, 'body' => $msg, 'from'=>$from)); into a blob field or something like that?
And then a function kicked off by another cron job loops through each table row with the blob field executing as written?

Sergey Kornilov admin 4/1/2014

There is no explanation. Tutorial assumes that emails are sent right away.
What you need to do is to create a new table with fields like to, body, subject etc and instead of sending emails store those emails in this table.
Second cron job will pull emails from there and actually send them.
I'm talking about a brand new functionality that needs to be implemented from scratch.

A
Anapolis 4/1/2014



There is no explanation. Tutorial assumes that emails are sent right away.
What you need to do is to create a new table with fields like to, body, subject etc and instead of sending emails store those emails in this table.
Second cron job will pull emails from there and actually send them.
I'm talking about a brand new functionality that needs to be implemented from scratch.


I guessed that a few hours ago and had already created the table you mentioned. I just have never seen literally what "stored emails" look like inside the fields that are holding those elements. My emails have quite a bit of information and some of the information is in German with umlauts.
I have tried to adapt the way the php email function assembles the elements from live values and table lookups. Inserting the same named strings into my table values.
Thanks for your help, Sergey, I will keep Googling.