wp_mail vs mail functions and header arrays

Both the mail() function in PHP and wp_mail() in WordPress do support passing an array or string of headers, but the difference is:

  • With mail(), the array keys are the header names and its values are the respective header values, e.g. array( 'Content-type' => 'text/html; charset=utf-8' ).

  • wp_mail() on the other hand, expects that the header name and value are put in the array values, e.g. array( 'Content-type: text/html; charset=utf-8' ).

So this statement is not necessarily true:

If you want to use an array for the headers in wp_mail, you have to
convert it text

Because you could actually instead convert the format of your $mailheader array to:

$mailheader = array(
    'MIME-Version: 1.0',
    'Content-type: text/html; charset=utf-8',
    'From: [email protected]',
);

Or create a headers array specifically for wp_mail(), from that array, like so:

$headers = array();
foreach ( $mailheader as $key => $value ) {
    $headers[] = "$key: $value";
}

Example by Codex (see https://developer.wordpress.org/reference/functions/wp_mail/#comment-348):

To send HTML formatted mail, you also can specify the Content-Type
HTTP header in the $headers parameter:

$to = '[email protected]';
$subject="The subject";
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');

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

And the documentation also stated that:

  • $headers can be a string or an array, but it may be easiest to use
    in the array form.

  • When you are using the array form, you do not need to supply line
    breaks (“\n” or “\r\n”).

    And thus, we could save time from trying to figure out whether CRLF (\r\n) or LF (\n) should be used..