Jump to content

Sending emails in a foreach loop. Best way to do this?


Go to solution Solved by jazzman1,

Recommended Posts

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 by AbraCadaver

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.

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 BCrecipients list to see all addresses in the list. So, emailing users separating in the foreach loop would be best for this?

  • Solution

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 by jazzman1
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.