Jump to content

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


eMonk

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.

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?

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"; 
}

Archived

This topic is now archived and is closed to further replies.

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