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 = "someone@mydomain.com";
$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! :)
Link to comment
Share on other sites

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"=>"someone@mydomain.com",
  "headers"=>"From: $from");
}

mysql_close($con);

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.