Current Location: Home> Latest Articles> Use array_chunk to split arrays for batch mailing

Use array_chunk to split arrays for batch mailing

M66 2025-04-27

In PHP programming, sending emails in batches is a common requirement. Especially when we need to send emails to a large number of users, it may cause performance bottlenecks or exceed the restrictions of the email service provider because of sending too many emails. In order to effectively manage the sending tasks of emails, we can use the array_chunk() function in PHP to split the user array into multiple small arrays and send emails batch by batch. This improves the performance of the code and avoids the problems caused by sending a large number of emails at once.

This article will show how to use array_chunk() to split an array and send mail in batches in combination with the mail() function.

1. Basic ideas

First, we need an array containing all recipient information. Suppose we already have an array containing all email addresses. We can then use array_chunk() to split this array into multiple small arrays, each small array size being the batch size we specified.

For example, if we have an array of 1000 mailboxes and send 100 mailboxes in batches, array_chunk() will split the original array into 10 subarrays containing 100 mailboxes. We can iterate through each subarray and send emails batch by batch.

2. Use array_chunk() to split array

First, suppose we have an array with multiple email addresses:

 $emails = [
    "user1@example.com", "user2@example.com", "user3@example.com", 
    "user4@example.com", "user5@example.com", "user6@example.com",
    // Other email addresses...
];

Next, we use the array_chunk() function to split it into 2 mailboxes per batch:

 $chunks = array_chunk($emails, 2);

After executing the above code, $chunks will become a two-dimensional array, each subarray contains two email addresses.

 Array
(
    [0] => Array
        (
            [0] => user1@example.com
            [1] => user2@example.com
        )

    [1] => Array
        (
            [0] => user3@example.com
            [1] => user4@example.com
        )

    [2] => Array
        (
            [0] => user5@example.com
            [1] => user6@example.com
        )
    // Other subarrays...
)

3. Send emails in batches

Now that we have split all email addresses into multiple batches, we can use the mail() function to send emails batch by batch. We will loop through each batch and send mail for each batch.

 $subject = "Important notice";
$message = "This is our latest notification,Please view related content。";
$headers = "From: no-reply@m66.net";

foreach ($chunks as $chunk) {
    // Send emails in batches each time
    foreach ($chunk as $email) {
        if (mail($email, $subject, $message, $headers)) {
            echo "Email sent to: $email<br>";
        } else {
            echo "Email sending failed: $email<br>";
        }
    }
}

The above code first sets the subject, content and header information of the email, and then uses a nested foreach loop to send emails to the email addresses in each subarray one by one.

4. Control the size of batch sending

By controlling the second parameter of array_chunk() , we can flexibly control the batch size of each send. For example, if you need to send 50 mails per batch, just set the second parameter to 50:

 $chunks = array_chunk($emails, 50);

This way, each batch will contain up to 50 email addresses. Depending on the limitations of your server and mailing service, you can adjust this value to balance performance and available resources.

5. Conclusion

By using the array_chunk() function, we can easily split an array with a large number of mailbox addresses into multiple smaller subarrays and send emails batch by batch. This method not only helps you avoid the performance bottlenecks caused by sending a large number of emails at one time, but also allows you to better control the frequency of emails, thereby increasing the success rate of emails.

Of course, more factors may need to be considered in actual use, such as SMTP configuration, security of email content, and recipient privacy protection. If you need to further optimize the effectiveness of email sending, you can consider using professional email sending services such as SendGrid, Mailgun, etc., which provide more advanced features, such as batch mail delivery, mail tracking and analysis.