How to check from header in wp_mail wordpress
In WordPress, you can check the headers of an email sent using the wp_mail
function by using the wp_mail_failed
action hook.
Here's an example of how you can do it:
function check_email_headers($error, $msg, $header, $attachments) {
// Check the headers
if (isset($header['X-Mailer'])) {
echo "X-Mailer: ". $header['X-Mailer']. "<br>";
}
if (isset($header['Content-Type'])) {
echo "Content-Type: ". $header['Content-Type']. "<br>";
}
//... check other headers you're interested in
}
add_action('wp_mail_failed', 'check_email_headers', 10, 4);
In this example, the check_email_headers
function is called whenever an email is sent using wp_mail
. The function takes four arguments: $error
, $msg
, $header
, and $attachments
. The $header
argument is an array of headers, where each key is the header name and each value is the header value.
You can access the headers using the $header
array, like this:
if (isset($header['X-Mailer'])) {
echo "X-Mailer: ". $header['X-Mailer']. "<br>";
}
This code checks if the X-Mailer
header is set, and if so, it echoes the value of that header.
You can also use the wp_mail
function's headers
argument to set custom headers. For example:
$headers = array('X-Mailer' => 'WordPress');
wp_mail($to, $subject, $message, $headers);
This code sets the X-Mailer
header to WordPress
when sending the email.
Note that the wp_mail
function returns true
on success and false
on failure. If the email fails to send, the wp_mail_failed
action hook is triggered, and you can access the error message using the $error
argument.