Help with mailing from table in database?

Hi there,

I am not certain if this is the correct board, but here goes…

I am attempting to create a mailing list for my subscribers with PHP and MySQL.
I have a table called pb_subscribers, and it has three sections: subscriber_id, subscriber_name, and subscriber_address.
The subscriber_id will not be used when mailing the subscribers, but instead, it is used for when I want to delete a user.

Here’s my code:

$link = 'http://' . $_SERVER['HTTP_HOST']; 
while ($row = mysql_fetch_assoc(mysql_query('SELECT * FROM pb_subscribers')) { 
$to = $row['subscriber_name'] . '<' . $row['subscriber_address'] . '>'; 
} 
$subject = $site->website_title_1() . " | New Post"; 
$emessage = "Hey there,\
\
A new post called \\"" . $_POST['title'] . "\\" has been posted on " . $site->website_title_1() . ".\
Go to " . $link . " to see the post!\
\
Thanks,\
Pocket Blogger"; 
$headers = "From: noone@pocket-blogger_com"; 
mail($to,$subject,$emessage,$headers);  

Basically, I want “$to” to be equaled to all the values in the database, so I can mail the users. But, my code isn’t working… What am I doing wrong, and what can I do to fix this?

The code is (mostly) correct, but the order in which you have put things isn’t.

Try this:


// These variables only need to be set once (from what I can see)
$link = 'http://' . $_SERVER['HTTP_HOST'];
$subject = $site->website_title_1() . " | New Post"; 
$emessage = "Hey there,\
\
A new post called \\"" . $_POST['title'] . "\\" has been posted on " . $site->website_title_1() . ".\
Go to " . $link . " to see the post!\
\
Thanks,\
Pocket Blogger"; 
$headers = "From: noone@pocket-blogger_com"; 

// query database
$result=mysql_query('SELECT subscriber_name, subscriber_address FROM pb_subscribers');

// loop through results
while ($row = mysql_fetch_assoc($result)) { 
  $to = $row['subscriber_name'] . '<' . $row['subscriber_address'] . '>'; 
  mail($to,$subject,$emessage,$headers); 
}

Hey,

Thank you very much, the code works! :smiley: