Jump to content

mysqli PHP mail looping


adrianle

Recommended Posts

I have a chunk of mail() code I use regularly that works great.  I have a need now to pull a list of email addresses from a table (easily done), and now I need to loop through these to send the same email to each. We're only talking about 20 emails or so.  I just need some basic guidance on how to loop through these (assuming an array is used in some way??) and any traps or hints to be aware of.  I've googled much on the topic, and not learned a lot. I've read it's a good idea to put a 5 second sleep or so in between each email sent, but outside of that - I could use some guidance. Thanks all-

Link to comment
Share on other sites

There's nothing difficult about that. If you know how to send email, then you just add the same code in loop (so it gets executed as many times as there are recipients), and this is it. For example, if you use swiftmailer, your code might look like this:

 

$transport=Swift_MailTransport::newInstance(); //create transport
$mailer=Swift_Mailer::newInstance($transport); //create mailer using created transport
$message=Swift_Message::newInstance("YOUR_SUBJECT_GOES_HERE") //create subject
->setBody("YOUR_body_GOES_HERE", "text/html"); //create message




while ("YOUR_CODE_TO_FETCH_EMAILS_GOES_HERE")
    {
    $message->setFrom(array("FROM_EMAIL_ADDRESS"=>"FROM_NAME"));
    $message->setTo(array("TO_EMAIL_ADDRESS"=>"TO_NAME"));


    $result=$mailer->send($message); //send message
    }
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.