eMonk Posted August 2, 2013 Share Posted August 2, 2013 Would it be better not to mail and loop or to create a BCC list by imploding the recipients list and send them out in one single blast? Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/ Share on other sites More sharing options...
AbraCadaver Posted August 2, 2013 Share Posted August 2, 2013 (edited) BCC I think, if it is a generic bulk email. If you have a cron or something that the web interface is not waiting to complete then it may not matter. But with BCC you're not going to be able to detect a failure with individuals in PHP. You'll need to monitor the queue for the sending address for bounces. Edited August 2, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443260 Share on other sites More sharing options...
lemmin Posted August 2, 2013 Share Posted August 2, 2013 Depending on your goal, you might want to set up a listserv. Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443264 Share on other sites More sharing options...
DavidAM Posted August 2, 2013 Share Posted August 2, 2013 Keep in mind, the mail specification does NOT require that the BCC list NOT be distributed. That is, the people in the TO and CC (which would be empty in your example) will NOT see the BCC list. However, the full list of BCC recipients might be shown to all BCC recipients in the list. That would then be effectively the same as putting everyone in the TO list (in this case). The "correct" way to do this is to send a separate message to each recipient, or use a true list service. Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443269 Share on other sites More sharing options...
eMonk Posted August 2, 2013 Author Share Posted August 2, 2013 The emails being sent out are notifications that their accounts have been approved along with their login and password. I don't want the people in the BCC recipients list to see all addresses in the list. So, emailing users separating in the foreach loop would be best for this? Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443271 Share on other sites More sharing options...
kicken Posted August 2, 2013 Share Posted August 2, 2013 If you're going to include their login and password in the email, then you have to send out individual emails since each email needs to be customized for the target recipient. Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443274 Share on other sites More sharing options...
Solution jazzman1 Posted August 2, 2013 Solution Share Posted August 2, 2013 (edited) Mails in a loop or mail function insde a loop - NEVER. Put everything into array() and implode them. Something like: // this data comes from the database $headerFields = array('BCC: example_1@example.com,example_2@example.com,example_3@example.com,example_4@example.com,example_5@example.com'); $headers = 'MIME-Version: 1.0' . "\n" . 'Content-type: text/plain; charset=UTF-8' . "\n" . 'Content-Transfer-Encoding: base64' . "\n"; 'X-Priority: 1 (Higuest)' . "\n"; 'X-MSMail-Priority: High' . "\n"; 'Importance: High' . "\n"; $headers .= implode(',', $headerFields)."\n"; $message = "UserName: $user_name \n UserPass: $password \n"; if(mail(null, '=?UTF-8?B?'.base64_encode($subject).'?=', base64_encode($message), $headers . '')){ echo "Message sent"; /* redirect the page */ exit; } else { echo "Message failed"; } Edited August 2, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280778-sending-emails-in-a-foreach-loop-best-way-to-do-this/#findComment-1443280 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.