bflydesign
bflydesign

Reputation: 483

Split mailing in separate mailings

I tried to send a mailing to 127 persons. I found out there can only be 99 email-adresses in the bcc-part in one mail.

So I was thinking to split up the array of emails into groups of 40 e.g.

I have an array of emails

$emailaddresses = array(
    [0] => email1,
    [1] => email2, ...

So I loop over the array and make a list of them:

foreach($emailaddresses as $email) {
    $list .= $email.',';
}

and then I remove the last ',' and add them to my mail-object:

$mail->bcc = substr_replace($list ,"",-1);

How can I loop over that array until 40 addresses, send a mail with that list and then continue the loop starting from 41->80 and so on...?

Upvotes: 2

Views: 156

Answers (4)

Adam Michael Jones
Adam Michael Jones

Reputation: 1

You can iterate over the list like this instead of using array_chunk:

Pre-seeding the array as a test:

$someArray = array();
for ($i = 0; $i <= 368; $i++) {
    $someArray[$i] = $i;
}

Logic you would use:

$interval = 40;

$segments = floor($i/$interval);

for($i = 0; $i <= $segments; $i++) {
    $elements = join(", ", array_slice($someArray,($i*$interval),$interval));
    // extra logic here; i.e. test/validate addresses
    print $elements;
}

Upvotes: 0

mariusnn
mariusnn

Reputation: 1897

First of all; The easiest and probably cleanest way to send mail is to send one for each, meaning all of them have just their own address in the to-field.

foreach($arrayOfEmail as $email){
    mail($email, $subject, $message);
}

If you must send in chunks, this is my easiest go:

$segments = array_chunk($arrayOfEmail, 40);  // Split in chunks of 40 each
for each($segments as $segment){             // For each chunk:
    $mailTo = implode($segment, ',');        // Concatenate all to one recipient string
    (...)                                    // BCC to headers+++
    mail($myEmail, $subject, $message);      // Send mail
}

Upvotes: 3

Dinesh
Dinesh

Reputation: 4110

why dont you use implode function as

$mail_list=implode(",",$emailaddresses)
it will give yor mail address as
email1,email2,email3............... , lastemail

Upvotes: 0

davidethell
davidethell

Reputation: 12018

I think Dagon is right that you really should just send 1 to each person in a for loop and not use the BCC, but here is a shell of a loop to get you started toward the answer:

$count = 0;
foreach($emailaddresses as $email) {
    $count++;
    // Use the modulus to see if we are at a multiple of 40
    // Add the address to the BCC however you choose.
    if ($count % 40 == 0) {
        // Send the email and start a new one.
    }
}

Upvotes: 1

Related Questions