wp mail - wp_mail send multiple emails in a loop

admin2025-01-07  6

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

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

Share Improve this question edited May 24, 2021 at 12:41 Dhimas Kirana asked Jun 15, 2020 at 10:43 Dhimas KiranaDhimas Kirana 13 bronze badges 10
  • 1 wp_mail returns a true and false value to indicate success, but your code doesn't bother to check for this and just assumes that it worked. How are you populating the $send_email array? I'm assuming you're using actual data in your code, but we can't see that so we can't eliminate it as a possible cause. Is there anything in your PHP error log? @example.com is a test domain, it won't send emails to example.com – Tom J Nowell Commented Jun 15, 2020 at 11:07
  • I have return $send_email array on this function and there's no error. I see the $send_email array data as I wish. but the problem is when calling wp_mail. Is wp_mail can't call twice? You can check this out stackoverflow.com/questions/52197907/… . I have same problem with this but with different case. – Dhimas Kirana Commented Jun 15, 2020 at 23:33
  • You wouldn't see an error in $send_mail, no. You should check the return value of wp_mail() to see if there are any errors, or hook wp_mail_failed and log any errors out (e.g. there's an example in the 'Debugging wp_mail (after WordPress 4.4)' section on this page). I don't know of any reason you can't call wp_mail() twice, but it's obvious something is breaking it in your case yes. – Rup Commented Jun 16, 2020 at 0:05
  • 1 I got error message "Message body empty". By the way, I'm using template for default wp_mail(). Here's my code gist.github.com/dhmskrn/7015bc49606d472aaac9346448b02834 – Dhimas Kirana Commented Jun 16, 2020 at 1:39
  • 1 I try to send message with wp_mail without template (plain text) , call wp_mail() twice and it works.. So, I think the problem came from the template. – Dhimas Kirana Commented Jun 16, 2020 at 1:47
 |  Show 5 more comments

1 Answer 1

Reset to default 0

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);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260970a704.html

最新回复(0)