Page 1 of 1

email notification from form

Posted: Tue Jan 29, 2013 3:22 pm
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

Re: email notification from form

Posted: Tue Jan 29, 2013 3:35 pm
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.

Re: email notification from form

Posted: Tue Jan 29, 2013 4:52 pm
by Christopher
Or SwiftMailer ... see which one makes the most sense to you. They are both good libraries.

Re: email notification from form

Posted: Tue Jan 29, 2013 7:35 pm
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?

Re: email notification from form

Posted: Tue Jan 29, 2013 9:25 pm
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

Re: email notification from form

Posted: Tue Jan 29, 2013 9:32 pm
by Christopher
What is in the $to variable after you implode the addresses?

Re: email notification from form

Posted: Tue Jan 29, 2013 9:44 pm
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

Re: email notification from form

Posted: Tue Jan 29, 2013 11:06 pm
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');

?>

Re: email notification from form

Posted: Thu Jan 31, 2013 12:56 am
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 ..........