如果我传递了一组电子邮件,则WP_mail不会发送电子邮件

时间:2017-06-21 作者:pixelngrain

发布时event CPT,我想向自定义元框中设置的每个参与者发送电子邮件。

当我经过时array 作为电子邮件地址,它不发送电子邮件

wp_mail( $employee_emails, $subject, $message );
但如果我使用字符串,它就会发送电子邮件。我不明白代码有什么问题,或者wp_mail

wp_mail( \'[email protected]\', $subject, $message );
我的代码
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 );

1 个回复
SO网友:Johansson

如果要将电子邮件发送给多个用户,则可以编写循环。

foreach ($employee_emails as $email) {
    wp_mail( $email, $subject, $message );
}
这将遍历阵列中的所有电子邮件地址,并将电子邮件发送给其中的每一个。

更新您可以将电子邮件地址存储在一个字符串中,用逗号分隔:

$employee_emails = \'\';
foreach ( $employee_ids_to_notify as $employee ) {
    $employee_emails .= get_the_author_meta( \'email\', $employee ).\', \';
}
然后可以将其作为单个字符串传递给wp_mail.

结束

相关推荐