webguync Posted March 17, 2010 Share Posted March 17, 2010 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? Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/ Share on other sites More sharing options...
schilly Posted March 17, 2010 Share Posted March 17, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1027861 Share on other sites More sharing options...
webguync Posted March 18, 2010 Author Share Posted March 18, 2010 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? Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028082 Share on other sites More sharing options...
schilly Posted March 18, 2010 Share Posted March 18, 2010 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 Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028172 Share on other sites More sharing options...
webguync Posted March 18, 2010 Author Share Posted March 18, 2010 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? Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028194 Share on other sites More sharing options...
schilly Posted March 18, 2010 Share Posted March 18, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028217 Share on other sites More sharing options...
webguync Posted March 18, 2010 Author Share Posted March 18, 2010 ok, so is it done entirely via server config or would I need to create a PHP script to upload? Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028234 Share on other sites More sharing options...
schilly Posted March 18, 2010 Share Posted March 18, 2010 you need a php script to do the work. the cron is just the timer that triggers the sciprt. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028237 Share on other sites More sharing options...
webguync Posted March 18, 2010 Author Share Posted March 18, 2010 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? Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028256 Share on other sites More sharing options...
schilly Posted March 18, 2010 Share Posted March 18, 2010 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 protected], [email protected]','subject','body'); } } something like that. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028274 Share on other sites More sharing options...
webguync Posted March 18, 2010 Author Share Posted March 18, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028283 Share on other sites More sharing options...
schilly Posted March 18, 2010 Share Posted March 18, 2010 if($result) { mail('[email protected]','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. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028325 Share on other sites More sharing options...
webguync Posted March 19, 2010 Author Share Posted March 19, 2010 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); Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028348 Share on other sites More sharing options...
schilly Posted March 19, 2010 Share Posted March 19, 2010 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. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028641 Share on other sites More sharing options...
webguync Posted March 19, 2010 Author Share Posted March 19, 2010 thanks, and would I just need to change mail to this? mail('[email protected]','Test Results','$body'); Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028647 Share on other sites More sharing options...
schilly Posted March 19, 2010 Share Posted March 19, 2010 you can do this mail('[email protected]','Test Results',$body); or mail('[email protected]','Test Results',"$body"); if you put a string in single quotes, PHP will not look for any variables inside the string. So your body would of looked like "$body" when you got the email. Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028652 Share on other sites More sharing options...
webguync Posted March 19, 2010 Author Share Posted March 19, 2010 cool thanks. Seems to be working. One more question, if I want to add multiple emails, would I just separate by comma? mail('[email protected]','[email protected]','Test Results',$body); Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028666 Share on other sites More sharing options...
schilly Posted March 19, 2010 Share Posted March 19, 2010 yup. but keep it in the same string as it's the same argument for mail() mail('[email protected],[email protected]','Test Results',$body); you can also do: mail('Name1 <[email protected]>, Name2 <[email protected]>','Test Results',$body); Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028683 Share on other sites More sharing options...
webguync Posted March 19, 2010 Author Share Posted March 19, 2010 COOL, THANKS! Link to comment https://forums.phpfreaks.com/topic/195622-fire-off-email-when-database-has-entry-w-php/#findComment-1028686 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.