I created a function which returns an array of email addresses:

// returns all wanted email adresses
leweb_get_events_detail();

This is the given array:

Array
(
    [0] => mail@example.com
    [1] => mail2@example.com
)

How can I add this emails to the headers array that it looks like this?

function leweb_send_mail_to_author() {

    $to = leweb_get_events_detail();
    $subject = 'The subject';
    $body = '<strong>The email body content</strong>';
    $headers = array('Content-Type: text/html; charset=UTF-8','From: Owner <owner@owner.com>', 'BCC:mail@example.com', 'BCC:mail2@example.com');

    wp_mail( $to, $subject, $body, $headers );
}
有帮助吗?

解决方案

You'd write them the same way you would any other email, the knowledge you need to do this is Email header knowledge, not WP knowledge. Those are raw email headers, and aren't a special WP format.

Since each item in the array is a line in the header of the email, we need to know how an email client writes out a BCC line.

This article describes all the various headers: https://mailformat.dan.info/headers/from.html

We can also get an example of specifying multiple emails in a header field by looking at the headers in any Email client. For example, here's a header field for an email I recieved from Github:

Cc: Tom J Nowell <contact@tomjn.com>, Author <author@noreply.github.com>

I got this in gmail by using the 3 dot dropdown and clicking show original. You won't see a BCC field this way, but given that multiple emails are always specified the same way, I trust you can make the minor leap and reuse the same format in the BCC field. E.g.

    $headers = [
        'Content-Type: text/html;'
        'charset=UTF-8',
        'Cc: Tom J Nowell <contact@tomjn.com>, Author <author@noreply.github.com>',
    ];

    wp_mail( $to, $subject, $body, $headers );

I trust you can make the final adjustment yourself, I won't cover looping over an array of strings and joining them together as that's basic PHP that I trust you can handle

许可以下: CC-BY-SA归因
scroll top