Dilip
Dilip

Reputation: 13

PHP trigger an email automatically when a certain condition is met

I would like to automate the process of sending this email. I mean when a condition is met then the email should automatically be sent to the given email addresses which is already given as hard-coded values. So for an example in my case a Technician has to complete 40jobs a day and if he finished 35 at the end of the day an email should be sent to the supervisor(provided his email id is already given) with Subject name and the body should be like ” The Technician 1234 has completed only 35 jobs for the day instead of 40. I was wondering how can implement this as Im very new to the field of PHP. Please anyone help me out. If possible please provide me with an example.

Thanks Dilip

Upvotes: 0

Views: 6836

Answers (6)

Gordon
Gordon

Reputation: 317089

Setup a Cron Job that runs at the "end of the day":

23 55 * * *  /path/to/php /path/to/script.php

Have it run a PHP script that can query your Job Store for whatever condition you want to check. For example with Job Store being a database.

$db = new PDO(/* config */);

$result = $pdo->query(
    'SELECT count(id) as "tasks_done" 
     FROM tasks WHERE engineer = "Tom" 
     AND finished_at = now()');

$result = $result->fetchAll();

if($result[0]['tasks_done'] < 40) {
    mail( ... )
}

If the condition is met, send the mail. The above needs refining of course. Dont expect being able to copy/paste it.

Also see:

Upvotes: 1

derkyjadex
derkyjadex

Reputation: 973

I'm assuming here that your problem is the triggering of the event, rather than the sending of the email itself.

In the example you described, you would need to have a script run at a certain time of day that would check for any condition that would require an email sending, and then send the email. Under any unix-like system, cron is the ideal solution to this. If you are on some kind of basic shared hosting you may not be able to set this up. In that case you would need to set up a task to run on a machine that you do have control over that would call a URL that would run the PHP script. This could be a cronjob, or a Scheduled Task under Windows.

If your example was switched around so that, say, an email was to be sent as soon as a technician completed 40 jobs then you would be able to send the email as part of the script that handled the form submission from the technician whenever he completed a task.

Upvotes: 1

Luke
Luke

Reputation: 3353

You could run a cron that uses the php mail function to send a report about each programmer at a set time each day. The cron script would look something like:

<?php
$to = '[email protected]';//the set up email to mail too
$result = mysql_query('select programmer_id, job_count, job_requirement from table'); //query the database
while($job = mysql_fetch_object($result)){
 mail($to,'Job counts for ' . $job->programmer_id,"Completed $job->job_count out of $job->job_requirement jobs","FROM: [email protected]");
}

Upvotes: 2

xil3
xil3

Reputation: 16439

Could have a cronjob that runs at the end of each day which checks what each technician has done and emails it to the manager.

Upvotes: 0

quantumSoup
quantumSoup

Reputation: 28162

Have a script check the conditions and send emails, and run it periodically: http://www.google.com/search?q=cronjob

Upvotes: 1

Sergey Eremin
Sergey Eremin

Reputation: 11080

you should use mail() function:

$technicianId = 1234;
$jobsNeeded = 40;
$jobsDone = getJobsDone($technicianId);
if ($jobsDone <= $jobsNeeded) {
    mail('[email protected]', 'Technician '.$technicianId.' slacking', 'The Technician '.$technicianId.' has completed only '.$jobsDone.' jobs for the day instead of '.$jobsNeeded);
}

Upvotes: 0

Related Questions