2017-06-21 27 views
0

在發佈event CPT時,我想發送電子郵件給每個在自定義元框中設置的參與者。如果傳遞數組變量,WordPress wp_mail不發送電子郵件

當我通過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); 
+0

'的var_dump($ employee_emails);'說明了什麼? – CBroe

+0

@CBroe我已經調試發佈之前。它給出了一個電子郵件ID –

+0

@CBroe另外,我剛剛測試過在'single.php'中調用'ac_send_event_notification()'函數,並在頁面加載時發送一封電子郵件。所以我相信一定有錯誤的鉤子或與我的代碼'HOOK' –

回答

0

,它完美地工作。

$employee_emails = array('[email protected]', '[email protected]'); 

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

OR試試下面的方法,它不是好的做法!

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

我不知道我的代碼有什麼問題,一切正常,仍然沒有發送電子郵件,我嘗試過array,implode但是沒有運氣 –

+0

或者try循環方法) – GNANA