wp_mail send multiple emails in a loop

I'm very confused with wp_mail(). Here my code.

function email_notification_for_admin_and_customer( $order_data ) {
    $subject_email = 'Subject LOREM IPSUM';
    $customer_email = 'Hi Customer, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempus, dui eget luctus accumsan, turpis orci malesuada turpis, eget volutpat ante velit a quam.';
    $admin_email = 'Hi Admin, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempus, dui eget luctus accumsan, turpis orci malesuada turpis, eget volutpat ante velit a quam.';
    $send_email = array(
        array(
            'to' = '[email protected]',
            'subject' = $subject_email,
            'message' = $customer_email
        ),
        array(
            'to' = '[email protected]',
            'subject' = $subject_email,
            'message' = $admin_email
        )
    );

    foreach ($send_email as $key = $value) {
        wp_mail( $value['to'], $value['subject'], $value['message']);
    }

}

I want to send email notification to the admin and customer, but wp_mail() only sends first email, which is to customer. Can you help me. Thank you.

I have same problem with this thread, but with different case.

UPDATE THE ANSWER

I'm using the wp_mail filter to format wordpress plain email into my html email template.

add_filter('wp_mail', 'my_wp_mail_filter');
function my_wp_mail_filter($args) {
    $message = $args['message'];
    $args['message'] = wpet_email_template(apply_filters('wpet_filter_email', $message));
    return $args;
}

This is the function to include html template.

function wpet_email_template($message) {

    // Render Template
    ob_start();
    include('custom-email-template.php');
    $wpet_template = ob_get_contents();
    ob_end_clean();

    // Replace Placeholder
    $message = str_replace('%%MAILCONTENT%%', $message, $wpet_template);

    // Return Template with Data
    return $message;
}

The problem is include_once('custom-email-template.php'); then I change to include('custom-email-template.php');

So this is the problem why the email just sent to customer email (first array of $send_email).

Here the answer

Topic wp-mail email Wordpress

Category Web


Function wp_mail() not suitable for use in a loop.

Unfortunately, a loop iterating over and over hundreds of times could considerably slow down the script, as pointed out in the reference on the PHP_mail() function. It is worth nothing that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient and have many failures while the loop is running - I found this information in SmashingMagazine's Book about Customazing Wordpress, published on March 2016.

I advise to divide the functionality of sending email notifications to admins and customers.

function email_notification_for_customer( $order_data ) {    
    
    $subject_email = 'Subject LOREM IPSUM';
    $customer_email = 'Hi Customer, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempus, dui eget luctus accumsan, turpis orci malesuada turpis, eget volutpat ante velit a quam.';  
    
    /* send to list of customers mails */
    $send_email_to = array(
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]'
    );  

    wp_mail( send_email_to, $subject_email, $customer_email);
}

function email_notification_for_admin( $order_data ) {
    
    $subject_email = 'Subject LOREM IPSUM';
    $admin_email = 'Hi Admin, Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempus, dui eget luctus accumsan, turpis orci malesuada turpis, eget volutpat ante velit a quam.';
    
    /* send to single admin's mail */
    $send_email_to = array(
        '[email protected]'
    );

    wp_mail( $value['to'], $subject_email, $admin_email);
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.