adrianle Posted October 3, 2017 Share Posted October 3, 2017 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- Quote Link to comment Share on other sites More sharing options...
requinix Posted October 3, 2017 Share Posted October 3, 2017 More or less you just put your existing code into the loop, then tweak a couple variables so that it sends to the right email address. Posting current code and what you've attempted will make it easier to explain. Quote Link to comment Share on other sites More sharing options...
phpmillion Posted October 4, 2017 Share Posted October 4, 2017 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 } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.