Forum Moderators: coopster
I have figured out how to send a single variable to a list from mysql and how to extract 10 headlines on a page for a registered individual without sending it via email.
But when I try to send the 10 headlines to each person on the full list, the script sends only one headline at a time. In other words, person A gets 10 emails with one headline each, person B gets 10 emails, and so on.
Can anyone suggest how I can improve the script below so that all 10 headlines can be sent in one email?
$our_email = "my_email";
$from = $our_email;
$subject = "Test Email";
$query = "SELECT email, title, url_title FROM list LIMIT 0, 10";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$to = "{$row['email']}";
$message = <<<EOF
<a href=http://www.site.com/folder/{$row['url_title']}>{$row['title']}</a>
EOF;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
$mail = mail($to, $subject, $message, $headers);
if ($mail) {
echo "<p>Email message sent successfully to $to.<br><br>";
}
else {
echo "<p>Email message didn't send to $to.<br><br>";
}
}
You need to take your mail function out of the while loop. Ideally you concatenate the data you want inside the while loop to build your message string. Then pass the full string into the message body.
$data .= "<a href=http://www.site.com/folder/".$row['url_title'].">".$row['title']."</a>";
echo $data;
dc
I can pull all of the headlines but send to only one email from the database with this --
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$email = "$row[email]";
$data .= "$row[title] [site.com...]
}
$to = $email;
etcetera...
But no more. When I pull $email = "$row[email]" out of the loop and put it elsewhere, I can't extract emails at all. Something like this within the loop --
$email .= "$row[email].",";
-- will create many duplicate emails. Any suggestions?