Saturday, August 6, 2022
HomeWordPress Developmentphp - wp_mail vs mail features and header arrays

php – wp_mail vs mail features and header arrays


I have been having points utilizing wp_mail and mail features in an utility when specifying header components within the operate.

I often arrange the header as an array, as in

$mailheader = array(
                'MIME-Model' => '1.0',
                'Content material-type' => ' textual content/html; charset=utf-8',
                'From' => 'fred@sender.com'  ,
);

Utilizing the mail() command, the headers are processed accurately, and the message is shipped as an HTML mail.

mail("to@area.com", "my topic", "<p>A message right here.</p>", $mailheader);

But when I exploit the identical command with the wp_mail operate (on a WP 6.x web site), the message is shipped as plain textual content:

wp_mail("to@area.com", "my topic", "<p>A message right here.</p>", $mailheader);

If you wish to use an array for the headers in wp_mail, it’s a must to convert it textual content:

        foreach ($mailheader as $key => $worth) {

            $header_wp .= "$key: $worth rn";   // for wp-mail header which does not do arrays
     }

(Notice the usage of double quotes within the assertion, so the rn is processed correctly.)

After which use this wp_mail command:

wp_mail("to@area.com", "my topic", "<p>A message right here.</p>", $header_wp );

This may end in an HTML-formatted message when utilizing wp_mail.

I’ve verified this on completely different WP 6.01 websites with PHP variations 7.3 and eight.x. The processing of the header by wp_mail occurs earlier than it’s despatched to phpMailer.

I spent a few weeks preventing this one, so wished to alert others.

I will put the proper code in my reply to the query.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments