Jump to content

Pulling email addresses from DB and mail()ing to them


ajsuk

Recommended Posts

Hey there, another nooby question I'm sure but again I'm stuck so I shall have to ask. ;)

As the title says really, I'm looking to grab email addresses from a database to make up a recipients list for this email script...
I've got this which does the job but having a script run 500 times over sending emails doesn't sound the best of ideas...
[code]
<db connect code etc...>

while($row = mysql_fetch_array($result))
{
$to = $row['email'];
$subject = "Test Email";
$message = "Test Email Message";
$from = "[email protected]";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
}

mysql_close($con);
?>[/code]
Is there some way I can store the results in a variable to be used as the mail-to value for sending to all at once rather than one by one?

Thanks for any help you can give! :)
Try this:
[code]<?php
// <db connect code etc...>

while($row = mysql_fetch_array($result))
{
$mailto[] = array("to"=>$row['email'],
                  "subject"=>"Test email",
                  "message"=>"Test email message",
                  "from"=>"[email protected]",
  "headers"=>"From: $from");
}

mysql_close($con);

foreach($mailto as $mail) mail($mail['to'],$mail['subject'],$mail['message'],$mail['headers']);
?>
[/code]
Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.