Sending out notification emails

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
User avatar
shasta59
AppGini Super Hero
AppGini Super Hero
Posts: 231
Joined: 2013-01-08 19:40
Location: Calgary, Alberta, Canada

Sending out notification emails

Post by shasta59 » 2013-01-17 17:21

In one of the app I have designed I have added the following to a hook file. Various individuals, depending upon job function and location need to be notified when a new report is entered. They also need to get a quick summary of the data entered.

I did not want to code in each email address and, when they change, have to change them again.

Design criteria used for table:

Send email to correct person, location (city) and function.

Instead I designed a table which has the following fields:

Name, email address, email re, city

Name - name of person to be notified
email address - email to send notification to
location - city person is located in
email re - this is a multiple lookup field with various values as it is used for various notifications.

(see attached image file for view of table detail view)
detail view - notify table.jpg
Table detail view
detail view - notify table.jpg (44.2 KiB) Viewed 6734 times
The name of the person is entered, city they are in, what emails they should get regarding certain topics and their email address.

The following hook code also sends out an email to the person who entered the report. I have used the actual cities to give the example a litle more realism. I have also expanded the code out to make it easier. My actual code is much much tighter but not so easy to figure out if you are just going to use the code in a copy paste situation. This is why there are three sets - one for each city. My actual code has only one set but wanted to keep it simple here in case you are not fully up on coding. If you are you should be able to figure out, quite easily, how to put the city name in the email message etc. and not have to have a sub elseif for each city. This way you can use the code in other instances.

I have not put the url which is also sent out to each person as this forum does not allow so many url's in a message. The line at the end of the Calgary one is also sent out with the email so they can just click on the link to be taken to the database.

Explanations to refer to as you look at code:

The SELECT line looks in the sql database, managers table and looks for the city in location and then in the send_email_re field it looks for a match for %Discrepancy%. If it find these matches it the sends an email to each person who is in that city who is supposed to get emails regarding discrepancy reports.

yourdatabase.Managers - (Managers is the name of the table where the data is entered as to who to send to.
location: this is the field in the table for the data submitted to pick where to send the data
LIKE '%Discrepancy% - this is one of the keywords entered in the 'email re' field

function discrep_after_insert($data, $memberInfo, &$args){

foreach($data as $field => $value){
$messageData .= "$field: $value \n";
}
if ($data['city'] == 'Calgary'){

$result = mysql_query("SELECT * FROM yourdatabase.Managers WHERE location= 'Calgary' AND send_email_re LIKE '%Discrepancy%'");
while($row = mysql_fetch_array($result))
mail($row['email'] , 'city name' , 'A new Inventory Discrepancy entry in Calgary has been entered for you to attend to. https://yourwebsite/index.php?signIn=1'.\n"."\n". $messageData. "\n"."\n" , 'From: [email protected]');

} elseif ($data['city'] == 'Edmonton') {
$result = mysql_query("SELECT * FROM yourdatabase.Managers WHERE location= 'Edmonton' AND send_email_re LIKE '%Discrepancy%'");
while($row = mysql_fetch_array($result))
mail($row['email'] , 'Edmonton' , 'A new Inventory Discrepancy entry in Edmonton has been entered for you to attend to. ');

} elseif ($data['city'] == 'Winnipeg') {
$result = mysql_query("SELECT * FROM yourdatabase.Managers WHERE location= 'Winnipeg' AND send_email_re LIKE '%Discrepancy%'");
while($row = mysql_fetch_array($result))
mail($row['email'] , 'Winnipeg' , 'A new Inventory Discrepancy entry in Winnipeg has been entered for you to attend to. ');
}


foreach($data as $field => $value){
$messageData .= "$field: $value \n";
}
mail($memberInfo['email'], 'thanks for the entry', $messageData, 'From: [email protected]');
return TRUE;
}


I am not a writer so my explanation may not be that easy to understand. If not just ask and I will post an even more detailed explanation.

Alan
Calgary, Alberta, Canada - Using Appgini 5.50 -

benzoD
Veteran Member
Posts: 69
Joined: 2013-01-31 21:16

Re: Sending out notification emails

Post by benzoD » 2013-02-26 18:48

shasta59, thank you so much for this. you are the best!

maramk
Veteran Member
Posts: 38
Joined: 2013-08-13 11:19

Re: Sending out notification emails

Post by maramk » 2014-03-03 15:03

Good day,

I want to send out a notification email (alert e-mail) to the project manager if a contract expire in a months time and then in3,2 and in a weeks time in the table:

CREATE TABLE IF NOT EXISTS `procurement_reporting_below_r500` (
`procurement_reporting_below_r500k_id` int(11) NOT NULL AUTO_INCREMENT,
`contract_number` varchar(40) DEFAULT NULL,
`programme_name_as_per_budget` int(11) DEFAULT NULL,
`cost_centre_number` varchar(40) DEFAULT NULL,
`type_of_services` text,
`industrial_sector` int(11) DEFAULT NULL,
`planned_method_of_procurement` int(11) DEFAULT NULL,
`total_awarded_value` decimal(1,0) DEFAULT NULL,
`source_of_funding` int(11) DEFAULT NULL,
`advertisement_date` date DEFAULT NULL,
`briefing_session` int(11) NOT NULL,
`briefing_session_date` date DEFAULT NULL,
`closing_date` date DEFAULT NULL,
`evaluation_date` date DEFAULT NULL,
`date_of_adjudication` date DEFAULT NULL,
`contract_signoff_date` date DEFAULT NULL,
`contract_started` date DEFAULT NULL,
`contract_terminated` date DEFAULT NULL,
`contract_duration` varchar(40) DEFAULT NULL,
`project_manager` int(11) DEFAULT NULL,
`delivery_lead_time` varchar(40) DEFAULT NULL,
PRIMARY KEY (`procurement_reporting_below_r500k_id`),
KEY `programme_name_as_per_budget` (`programme_name_as_per_budget`),
KEY `industrial_sector` (`industrial_sector`),
KEY `planned_method_of_procurement` (`planned_method_of_procurement`),
KEY `source_of_funding` (`source_of_funding`),
KEY `briefing_session` (`briefing_session`),
KEY `project_manager` (`project_manager`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

in the hook procurement_planning_above_r500.php the following code is inserted both in before_insert and before_update function and the correct date is displayed

function procurement_planning_above_r500_before_insert(&$data, $memberInfo, &$args){

$std = strtotime($data['contract_started']);
$ste = strtotime($data['contract_terminated']);
$years = date('Y',$ste) - date('Y',$std);
$months = date('m',$ste) - date('m',$std);
if ($months < 0)
{
$years--;
$months = $months + 12;
}

$days = date('d',$ste) - date ('d',$std);

if ($days < 0)
{
$months--;
$days = $days + 30;
}
$data['contract_duration'] = $years . " Years " . $months . " Months " . $days . " Days";

return TRUE;
}

Now: What email code (if I create notifyemail.php) must be written in the notifyemail.php order to notify the project manager of the status of the contract or when budget plans are due? I also presume I must create a cron job as well?

Please assist.

Thank you.

Kind regards.

Mara

Post Reply