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