0

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_1@example.com',
            'subject' => $subject_email,
            'message' => $customer_email
        ),
        array(
            'to' => 'email_2@example.com',
            '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

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
    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. 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
    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 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. Jun 16, 2020 at 1:47

1 Answer 1

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_1@example.com',
        'email_1@example.com',
        'email_1@example.com',
        'email_1@example.com',
        'email_1@example.com'
    );  

    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_2@example.com'
    );

    wp_mail( $value['to'], $subject_email, $admin_email);
}
4
  • "not suitable for use in a loop" - why not?
    – Rup
    Jun 16, 2020 at 10:51
  • Why do you need to use this in a loop, if it is already a wrapper for PHPMailer, with all the loops inside? With my answer I wanted to suggest that the option of using wp_mail() in a loop is the wrong architectural approach. Jun 16, 2020 at 11:07
  • Note that by using the to field you're exposing customer emails it should be BCC if one email is being sent to multiple recipients
    – Tom J Nowell
    Jun 16, 2020 at 11:13
  • I added more detailed explanation to answer post. Jun 16, 2020 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.