最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp mail - wp_mail is not sending email if I pass an array of emails

matteradmin6PV0评论

On publish event CPT, I want to send emails to every participant set in the custom meta boxes.

When I pass array as email addresses it is not sending email

wp_mail( $employee_emails, $subject, $message );

but if I use string then it sends the emails. I don't understand what is wrong with the code or wp_mail

wp_mail( '[email protected]', $subject, $message );

My Code

function ac_send_event_notification( $ID, $post ) {

    if ( wp_is_post_revision( $ID ) ) {
        return;
    }

    // employees details
    $employees  = rwmb_meta( 'ac_event_employees', [], $ID );
    $positions  = rwmb_meta( 'ac_event_positions', [], $ID );
    $operations = rwmb_meta( 'ac_event_operations', [], $ID );

    // event details
    $operation_user_ids = [];
    if ( ! empty( $operations ) ) {
        foreach ( $operations as $operation ) {
            $operation_user_ids[] = ac_get_event_participants_ids_by_operation( $operation );
        }
    }
    $position_user_ids = [];
    if ( ! empty( $positions ) ) {
        foreach ( $positions as $position ) {
            $position_user_ids[] = ac_get_event_participants_ids_by_position( $position );
        }
    }
    $operation_ids = array_reduce( $operation_user_ids, 'array_merge', [] );
    $position_ids  = array_reduce( $position_user_ids, 'array_merge', [] );

    sort( $employees );
    sort( $operation_ids );
    sort( $position_ids );

    $employee_ids_to_notify = array_unique( array_merge( $employees, $operation_ids, $position_ids ) );
    sort( $employee_ids_to_notify );

    // get employees email ids
    if ( ! empty( $employee_ids_to_notify ) ) {
        foreach ( $employee_ids_to_notify as $employee ) {
            $employee_emails[] = get_the_author_meta( 'email', $employee );
        }
    }

    // Sending email to the participants

    $author = $post->post_author; /* Post author ID. */
    $name   = get_the_author_meta( 'display_name', $author );

    $subject = sprintf( 'New Event Created by %s', $name );
    $message = "Hello,\n\n";
    $message .= "There is a new event created by {$name}.\n\n";
    $message .= "Check out all details with the following link.\n\n";
    $message .= get_the_permalink( $ID );

    wp_mail( $employee_emails, $subject, $message );

}
add_action( 'publish_event', 'ac_send_event_notification', 10, 2 );

On publish event CPT, I want to send emails to every participant set in the custom meta boxes.

When I pass array as email addresses it is not sending email

wp_mail( $employee_emails, $subject, $message );

but if I use string then it sends the emails. I don't understand what is wrong with the code or wp_mail

wp_mail( '[email protected]', $subject, $message );

My Code

function ac_send_event_notification( $ID, $post ) {

    if ( wp_is_post_revision( $ID ) ) {
        return;
    }

    // employees details
    $employees  = rwmb_meta( 'ac_event_employees', [], $ID );
    $positions  = rwmb_meta( 'ac_event_positions', [], $ID );
    $operations = rwmb_meta( 'ac_event_operations', [], $ID );

    // event details
    $operation_user_ids = [];
    if ( ! empty( $operations ) ) {
        foreach ( $operations as $operation ) {
            $operation_user_ids[] = ac_get_event_participants_ids_by_operation( $operation );
        }
    }
    $position_user_ids = [];
    if ( ! empty( $positions ) ) {
        foreach ( $positions as $position ) {
            $position_user_ids[] = ac_get_event_participants_ids_by_position( $position );
        }
    }
    $operation_ids = array_reduce( $operation_user_ids, 'array_merge', [] );
    $position_ids  = array_reduce( $position_user_ids, 'array_merge', [] );

    sort( $employees );
    sort( $operation_ids );
    sort( $position_ids );

    $employee_ids_to_notify = array_unique( array_merge( $employees, $operation_ids, $position_ids ) );
    sort( $employee_ids_to_notify );

    // get employees email ids
    if ( ! empty( $employee_ids_to_notify ) ) {
        foreach ( $employee_ids_to_notify as $employee ) {
            $employee_emails[] = get_the_author_meta( 'email', $employee );
        }
    }

    // Sending email to the participants

    $author = $post->post_author; /* Post author ID. */
    $name   = get_the_author_meta( 'display_name', $author );

    $subject = sprintf( 'New Event Created by %s', $name );
    $message = "Hello,\n\n";
    $message .= "There is a new event created by {$name}.\n\n";
    $message .= "Check out all details with the following link.\n\n";
    $message .= get_the_permalink( $ID );

    wp_mail( $employee_emails, $subject, $message );

}
add_action( 'publish_event', 'ac_send_event_notification', 10, 2 );
Share Improve this question edited Jun 21, 2017 at 9:38 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Jun 21, 2017 at 8:29 pixelngrainpixelngrain 1,3901 gold badge23 silver badges50 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If you want to send the email to more than one user, then you can write a loop.

foreach ($employee_emails as $email) {
    wp_mail( $email, $subject, $message );
}

This will loop through all the email addresses in the array and send the email to each one of them.

UPDATE

You can store your email addresses into a single string, separated by commas:

$employee_emails = '';
foreach ( $employee_ids_to_notify as $employee ) {
    $employee_emails .= get_the_author_meta( 'email', $employee ).', ';
}

Then you can pass it as a single string to wp_mail.

Post a comment

comment list (0)

  1. No comments so far