email notification from form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

email notification from form

Post by Alpal »

I run a small sporting association site and have built a forum in the administration area. What I want to do is notify all Board Members when a new Topic is inserted into the database. The current script works fine (it is within a form on an insert record page), but will only send to predefined email address or addresses. I would like to modify the "to" so it sends emails from a query. My knowledge of php is not sufficient to write it.

<?php

$to = "email@mysite.com";
$subject = "DO NOT REPLY";
$message = "This is an automated message, A new topic has been introduced in the mysite forum!";
$headers = "From: noreply@mysite.com\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";

mail($to, $subject, $message, $headers)or die('An error has occurred');

?>

Would like to replace
$to = "email@mysite.com";
with the result of a query, something like
SELECT email
FROM members
WHERE boardmember is not null

Any assistance will be greatly appreciated
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: email notification from form

Post by requinix »

Grab yourself a copy of PHPMailer. It's easy to use. Then adding multiple recipients is just a function call (in a loop) away.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: email notification from form

Post by Christopher »

Or SwiftMailer ... see which one makes the most sense to you. They are both good libraries.
(#10850)
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: email notification from form

Post by Alpal »

Have read the about files for both php mailer and swift mailer and am even more confused. Downloaded both and had a look through them, could not find a solution. As I said I struggle with php. Am wondering if this is at all possible?
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: email notification from form

Post by Alpal »

found this solution but it sends 5 emails to each address (there are 5 email addresses in the table) ???

<?php
$result = mysql_query("SELECT email FROM members WHERE boardmember is not null");

while($row = mysql_fetch_array($result))
{
$addresses[] = $row['email'];
}
$to = implode(", ", $addresses);

$subject = "DO NOT REPLY";
$message = "This is an automated message, A new topic has been introduced in the mysite forum!";
$headers = "From: noreply@mysite.com\r\n";

mail($to, $subject, $message, $headers)or die('An error has occurred');

?>

Any assistance will be greatly appreciated
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: email notification from form

Post by Christopher »

What is in the $to variable after you implode the addresses?
(#10850)
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: email notification from form

Post by Alpal »

not sure, how would i find out? Ran the query and it returns 5 email addresses.
The received email shows all 5 email addresses in the "to" field
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: email notification from form

Post by Alpal »

Found a solution, thank you very much for your assistance.
Solution:
<?php

$result = mysql_query("SELECT email FROM members WHERE boardmember is not null");

while($row = mysql_fetch_array($result))
{
$addresses[] = $row['email'];
}
$to = implode(", ", $addresses);
$subject = "DO NOT REPLY";
$message = "This is an automated message, A new topic has been introduced in the mysite forum!";
$headers = "From: noreply@mysite.com\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";

mail($to, $subject, $message, $headers)or die('An error has occurred');

?>
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: email notification from form

Post by Alpal »

Added a few extras to the automated email

<?php

$result = mysql_query("SELECT email FROM members WHERE boardmember is not null");

while($row = mysql_fetch_array($result))
{
$addresses[] = $row['email'];
}
$to = implode(", ", $addresses);
$subject = "NEW FORUM TOPIC";



$result2 = mysql_query(
"SELECT `post_topic`
FROM `post_table`
ORDER BY post_ID DESC
LIMIT 1");

while($row = mysql_fetch_array($result2))
{
$topic = $row['post_topic'];
}

$result3 = mysql_query(
"SELECT `post_by`
FROM `post_table`
ORDER BY post_ID DESC
LIMIT 1");

while($row = mysql_fetch_array($result3))
{
$topic_by = $row['post_by'];
}

$message = "<p>NOTIFICATION ONLY - AUTOMATED EMAIL</p>
<p>A new topic has been introduced in the -------- forum</p>
<p>PLEASE VISIT THE FORUM - DO NOT REPLY TO THIS EMAIL!</p>
<p>The title of the new topic is: <strong>$topic</strong></p>
<p>Topic created by: $topic_by</p>";
$headers = "From: info@mysite.com\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";

mail($to, $subject, $message, $headers)or die('An error has occurred');

?>

Had to let everyone on the Board of our association know that they would be receiving emails from this address and to check their spam filters to ensure the email arrives in their inbox.
Apart from that everything works fine, the email looks ok and the "post_title" and the "post_by" are filled in from the database as the record is inserted.
While this all works, am not sure if it is the correct way to do things, or if there is a better way?
Would appreciate any comments or advice anyone has to offer, thanks in advance ..........
Post Reply