Forum Moderators: coopster

Message Too Old, No Replies

Email database variables to a list

         

scottb

10:16 pm on Sep 10, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



I am trying to send headlines out of mysql to a small email list. Each recipient needs to receive a group of 10 story headlines that are updated daily. The headlines in the email link to the full story on our site.

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>";
}

}

dreamcatcher

7:22 am on Sep 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi scottb,

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

scottb

10:17 pm on Sep 12, 2008 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks, Dreamcatcher. That solved the problem of displaying the headlines. Hope you don't mind one more question because I've spent many hours trying to solve a second problem.

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?