Do you receive any errors from the script when you execute it? I copied your code an i got this error message:
"From:" header missing in scriptname on line 17
Try the following sample of code
Code: Select all
<?php $count_query = mysql_query("SELECT COUNT(id) FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
$rows = mysql_fetch_array($count_query);
if ($rows[0] != 0) {
$query = mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
while ($array = mysql_fetch_array($query)) {
$mail_address = $array['email'];
}
$to = $mail_address;
$subject = 'Someone has sent a message to your tag';
$message = 'test';
$headers = 'From: example@example.com';
$mail = mail($to, $subject, $message, $headers);
if ($mail) { echo 'Message sent'; }
//edit for production
else {
echo $mail_address;
}
}
else {
// Display alternate message informing visitor or whoever
// that there was no email address matching the tag
} ?>
This code does the following :
1. Counts the rows matching your query. Im going on the assumption that you have one (unique) email address per tag.
2. If there is a match ($rows[0] == 1, again based on unique email premise) the query is performed where the email address is retrieved from the table and assigned to the variable '$mail_address'.
3. All the required variables for the 'mail()' function is assigned and then an attempt is made to send the email.
4. If it sends, good. If not it will echo the email address. This is just to see what email address is retrieved from the table, if any. If the script works, change it. Note i also added
edit for production.
5. The final 'else' section you should use to indicate that no match for the tag was found, or you could remove it.
Hope this helps. If it still doesn't work please paste any errors you receive
Note - if your PRIMARY KEY is not 'id' please change the 'id' in the COUNT() function to the same value as your primary key.