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

On publish event CPT, I want to send emails to every participant set in the custom meta boxes.When I pass array as email

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745619270a4636423.html

相关推荐

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

    On publish event CPT, I want to send emails to every participant set in the custom meta boxes.When I pass array as email

    9小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信