1

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( 'me@emailid.com', $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 );
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.

| improve this answer | |
  • Yes, I can run the loop. However, my question is even wp_mail() allow to pass an array or multiple recipients than why it is not working? – pixelngrain Jun 21 '17 at 10:00
  • BTW, I have tried foreach loop and it is not sending email. – pixelngrain Jun 21 '17 at 10:06
  • @pixelngrain Try and dump your email address array by var_dump. There is most likely an issue in your array. – Jack Johansson Jun 21 '17 at 10:08
  • I have already tried that. See here array(1) { [0]=> string(21) "me@myemailid.com" } I have also tried implode() but no luck. :( – pixelngrain Jun 21 '17 at 10:47
  • @pixelngrain I updated the answer for you. – Jack Johansson Jun 21 '17 at 11:07

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.