Wordpress wp_mail sends my html message as a text attachment

This is the code to send mail:

$to = $_POST['leadEmail']; $subject = "my subject"; $message = ( $_POST['leadEmailPref'] == "html" ) ? $messageh : $messagep ; $headers[] = "From: My Name < ny_name@gmail.com >"; $headers[] = "Content-type: ".$_POST['leadEmailPref'] ; wp_mail( $to, $subject, $message, $headers ); 

When I go to my inbox, the message is sent as an attachment, and I do not see the message. Obviously, I do not want the attachment, and I want the one who received the letter to immediately see the message.

+4
source share
2 answers

Your Content-type: Invalid. It should be text/html or text/plain , not html .

$headers[] = "Content-type: text/".$_POST['leadEmailPref'] ;

+8
source

You must try:

 add_filter( 'wp_mail_content_type', 'set_html_content_type' ); wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' ); function set_html_content_type() { return 'text/html'; } remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); 
+1
source

Source: https://habr.com/ru/post/1489895/


All Articles