Author Topic: fire off email when database has entry (w/ PHP)  (Read 771 times)

0 Members and 1 Guest are viewing this topic.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
fire off email when database has entry (w/ PHP)
« on: March 17, 2010, 06:57:22 PM »
Is there a way use PHP to send an email to multiple people when there is an entry in a MySQL table?

for example when a field contains data, and email is sent(automatically) w/ the contents of that data?

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #1 on: March 17, 2010, 07:10:45 PM »
yea it's pretty easy.

read up on the mail function: www.php.net/manual/en/function.mail.php

so when you update data in whatever table it is, check for the value you want then use mail() to email the people required.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #2 on: March 18, 2010, 09:29:48 AM »
yea, I understand pretty much how the mail function works and I want to send an email whenever a value ==1 in my field, but not sure how to tie the two together. Would I need to have UPDATE SQL statement?

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #3 on: March 18, 2010, 12:31:25 PM »
there is a couple ways.

-either when you do an update statement check to see if the value is set to one and if it is send out the email.
-set up a cron to automatically check the database after a certain period of time and send out emails for every record it finds with value = 1

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #4 on: March 18, 2010, 01:12:28 PM »
I like the idea of the second one (cron). Can you provide any info on how that is done? Will it only send off emails if the value == 1?

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #5 on: March 18, 2010, 01:40:52 PM »
you'll have to check with your web server to see if they allow for crons to be set up. it's basically a daemon that runs on your web server where you can set triggers to go off after certain time intervals (minute, hour, day, month, etc).

In this case, it would trigger a PHP script which would select all the rows from your db where value = 1 then send off an email to your group.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #6 on: March 18, 2010, 02:31:53 PM »
ok, so is it done entirely via server config or would I need to create a PHP script to upload?

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #7 on: March 18, 2010, 02:41:32 PM »
you need a php script to do the work. the cron is just the timer that triggers the sciprt.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #8 on: March 18, 2010, 03:14:29 PM »
The hosting company is sometimes hard to get a hold of. going back the first solution with the update. Could you provide a quick example with how this is done?

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #9 on: March 18, 2010, 03:59:59 PM »
so you'd likely be doing this via a form post when you update the record.

if($_POST['submitButton']){
	

	
//validate post data here
	

	

	
//run update query. value and id are from your $_POST data
	
$sql "UPDATE table set value = '$value' WHERE id = $id";
	
mysql_query($sql);
	

	
//check value to see if it meets our threshold 
	
if(
$value == 1){
	

	
	
mail('email@domain.com, email2@domain.com','subject','body');
	

	

	
}




}


something like that.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #10 on: March 18, 2010, 04:21:39 PM »
I have code which created the log entry, so I should just be able to add the email part to this right?


<?php
//include Database Class
require_once('databaseClass.php');

$userID $_POST['userID'];
$sect 1;
$time gmmktime();
$attempt 1;

$db = new Database('localhost','username','password','DBName');

$sql "INSERT INTO og_March2010 (user_id,section,date,attempt) VALUES ($userID,$sect,$time,$attempt)";
$result $db->query($sql);

if(
$result) {
	
$replySQL "SELECT log_id FROM og_March2010 WHERE user_id=$userID";
	
$kickback $db->query($replySQL);
	
$kickback->get_rows();
	
$row $kickback->fetch_assoc();
	
$logID $row['log_id'];
	
$db->close();

	
echo 
'triumph=sieg&logid='.$logID;
	
}

?>


anytime there is an entry I would want an email to be sent.

Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #11 on: March 18, 2010, 06:31:48 PM »
[php]

if($result) {

mail('user@domain.com','subject','body');

   $replySQL = "SELECT log_id FROM og_March2010 WHERE user_id=$userID";
   $kickback = $db->query($replySQL);
   $kickback->get_rows();
   $row = $kickback->fetch_assoc();
   $logID = $row['log_id'];
   $db->close();

   echo 'triumph=sieg&logid='.$logID;
   }
/php]

you can put $userID,$sect,$time,$attempt in a string if you want to include them in the body of the email.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #12 on: March 18, 2010, 08:18:24 PM »
thanks, in order to turn the variables into a string would it be best to use
str_replace?


$body  
str_replace($userID$sect$time,$attempt);






Offline schilly

  • Devotee
  • Posts: 870
  • Gender: Male
    • View Profile
    • Damn Semicolon
Re: fire off email when database has entry (w/ PHP)
« Reply #13 on: March 19, 2010, 12:12:48 PM »
nope. just concatenate them.

$body  "User: $userID\n\nSect: $sect\n\nTime: $time\n\nAttempt: $attempt";

\n will give you a new line in an email.

Offline webguyncTopic starter

  • Devotee
  • Posts: 923
    • View Profile
Re: fire off email when database has entry (w/ PHP)
« Reply #14 on: March 19, 2010, 12:17:46 PM »
thanks, and would I just need to change mail to this?


mail
('user@domain.com','Test Results','$body');