如果传递数组变量

时间:2017-06-21 08:27:53

标签: php wordpress email

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

当我将array作为电子邮件地址传递时,它不会发送电子邮件

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

但如果我使用字符串而不是发送电子邮件。我不明白代码或wp_mail

的错误
wp_mail( 'me@emailid.com', $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 个答案:

答案 0 :(得分:0)

我使用如下,它工作得很好。

$employee_emails = array( 'test@gmail.com', 'testsecond@gmail.com' );

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

OR 尝试以下方法,这不是很好的做法!,

foreach($group_emails as $email_address)
{
   wp_mail($email_address, 'my subject', 'my message', $headers);
}