#53906 closed defect (bug) (invalid)
wp_mail_content_type doesn't work with pluggable function wp_mail
Reported by: | skarabeq | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | minor | Version: | 5.8 |
Component: | Keywords: | ||
Focuses: | Cc: |
Description
I have created my own plugin that overrides the Pluggable function wp_mail, but into the variable $message
, it is "text/plain". I have tried to add a filter to change it to "text/html", but it doesn't work. Here is the code of the plugin:
<?php /** Plugin Name: wp_mail override Description: Example plugin wp_mail override Author: Example Version: 1.0 Author URI: https://example.com */ function xxxx_set_content_type( $content_type ) { return 'text/html'; } add_filter('wp_mail_content_type', 'xxxx_set_content_type' ); if (!function_exists('wp_mail')) { function wp_mail( $to, $subject, $message, $headers = '', $attachments = [] ) { var_dump($message); } }
When this plugin is active and I have to try to reset my password I have the following text into a variable $message
:
Someone has requested a password reset for the following account: Site Name: example Username: exampleUser If this was a mistake, ignore this email and nothing will happen. To reset your password, visit the following address: http://local-env.dev/wp-login.php?action=rp&key=8CC4r2GttU32W5njL0AU&login=exampleUser&wp_lang=en_US This password reset request originated from the IP address 127.0.0.1.
Change History (4)
#2
@
3 years ago
- Keywords close added
Hi there, welcome back to WordPress Trac! Thanks for the report.
Setting the type to text/html
using the wp_mail_content_type
filter changes the Content-Type
header of the email, but does not perform anything else to actually convert the message to HTML, you still need to do that yourself.
When this plugin is active and I have to try to reset my password I have the following text into a variable
$message
If you look at the page source at that point, you'll notice that even though the browser renders the message as a single line, it actually consists of multiple lines in text format,
string(362) "Someone has requested a password reset for the following account: Site Name: WordPress Trunk (dev) Username: admin If this was a mistake, ignore this email and nothing will happen. To reset your password, visit the following address: http://develop.wordpress.test/build/wp-login.php?action=rp&key=...&login=admin&wp_lang=en_US "
So you'll need to run something like nl2br()
to convert it to HTML:
if ( ! function_exists( 'wp_mail' ) ) { function wp_mail( $to, $subject, $message, $headers = '', $attachments = [] ) { $message = nl2br( $message ); var_dump( $message ); } }
which would then result in:
string(434) "Someone has requested a password reset for the following account:<br /> <br /> Site Name: WordPress Trunk (dev)<br /> <br /> Username: admin<br /> <br /> If this was a mistake, ignore this email and nothing will happen.<br /> <br /> To reset your password, visit the following address:<br /> <br /> http://develop.wordpress.test/build/wp-login.php?action=rp&key=...&login=admin&wp_lang=en_US<br /> <br />
Related: #18493
In WordPress 5.7.2 it is the same, but the URL is with strange brackets -
<http://local-env.dev/wp-login.php?action=rp&key=...&login=exampleUser&wp_lang=en_US>
The brackets were removed in [47086] / #44589 for WordPress 5.4.
In WordPress 5.7.2 it is the same, but the URL is with strange brackets -
<http://local-env.dev/wp-login.php?action=rp&key=8CC4r2GttU32W5njL0AU&login=exampleUser&wp_lang=en_US>