Mārcis
Mārcis

Reputation: 91

Invalid email headers using WordPress wp_mail()

I get an invalid header when I try to send an email using WP Mail SMTP, the test emails work fine. It has been working perfectly for months but from 25.01. it is not working.

My code:

 $headers = "From: $email\n;";
 $headers .= "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-Type: text/html; charset=UTF-8";
 if (wp_mail($to, $subject, $message, $headers, $attachments)) {
   echo "<h3 style='color:green; text-align: center;'>Email sent!</h3>";
  }

Error thrown: invalid_parameter: Invalid headers

Upvotes: 1

Views: 199

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253968

As the default character encoding for wp_mail() is UTF-8, you don't need charset=UTF-8 and also, the Mime version is not needed.

So assuming that all other variables are correctly defined, try to use the following instead:

$headers  = "Content-Type: text/html;\r\n";
$headers .= "From: $email;\r\n";

if ( wp_mail( $to, $subject, $message, $headers ) ) {
    echo '<h3 style="color:green; text-align:center;">Email sent!</h3>';
}

Tested and works in last WordPress version.

I have removed $attachments argument… If you are using it, you can define it back.

Upvotes: 0

Related Questions