admin管理员组文章数量:1434239
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
1 Answer
Reset to default 1If 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
.
本文标签: wp mailwpmail is not sending email if I pass an array of emails
版权声明:本文标题:wp mail - wp_mail is not sending email if I pass an array of emails 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745619269a2666588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论