Does Outlook get HTML php email address as code?

I did send HTML email using PHP, but my client gets this as clean code. Here is the PHP code for sending mail:

$subject = 'HTML e-mail test'; $message = '<html> <body> <h1>TEST</h1> </body> </html>'; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "To: <".$to.">\r\n"; $headers .= "From: <".$email.">\r\n"; $mailb = mail($to, $subject, $message, $headers); 

This works fine for me, but they get it like:

 Content-type: text/html; charset=iso-8859-1 To: < email@email.com > From: < email@email.com > <html> <body> <h1>TEST</h1> </body> </html> 

Is there something wrong with my headlines? Or is it their Outlook ? How can I fix this problem?
Thanks in advance!

+4
source share
6 answers

I see:

 Content-typetext/html; charset=iso-8859-1 

where should i see:

 Content-type: text/html; charset=iso-8859-1 

Is this a cut / paste error? In your code or in your result?

Note that you probably want to include text as a multi-page message in text and text / text types, since HTML-only mail often gets high spam ratings (using SpamAssassin, SpamBouncer, etc.).

UPDATE # 1:

It seems that \r\n interpreted as two new lines instead of one. There may be different errors on different platforms when implementing SMTP. You may be able to change the end of the line to only \n . If this works with your system, do not rely on it, as it may not work on another system.

Also, consider switching to another method of sending mail. phpMailer and SwiftMailer are recommended by the PHP internal mail() function.

UPDATE # 2:

Based on Incognito's suggestion, here you can use the code to better organize the headers, as well as create part of the plaintext:

 filter_var($to, FILTER_VALIDATE_EMAIL) or die("Invalid To address"); filter_var($email, FILTER_VALIDATE_EMAIL) or die("Invalid From address"); $subject = 'HTML e-mail test'; $messagetext = 'TEST in TEXT'; $messagehtml = '<html> <body> <h1>TEST</h1> <p>in HTML</p> </body> </html>'; // We don't need real randomness here, it just a MIME boundary. $boundary="_boundary_" . str_shuffle(md5(time())); // An array of headers. Note that it up to YOU to insure they are correct. // Personally, I don't care whether they're a string or an imploded array, // as long as you do input validation. $headers=array( 'From: <' . $email . '>', 'MIME-Version: 1.0', 'Content-type: multipart/alternative; boundary="' . $boundary . '"', ); // Each MIME section fits in $sectionfmt. $sectionfmt = "--" . $boundary . "\r\n" . "Content-type: text/%s; charset=iso-8859-1\r\n" . "Content-Transfer-Encoding: quoted-printable\r\n\r\n" . "%s\n"; $body = "This is a multipart message.\r\n\r\n" . sprintf($sectionfmt, "html", $messagehtml) . sprintf($sectionfmt, "plain", $messagetext) . "--" . $boundary . "--\r\n"; $mailb = mail($to, $subject, $body, implode("\r\n", $headers)); 

I’m not saying that this is the right way to do it in any way, but if you like the strategy and you think that you can save such a code without viewing it for 6 or 12 months (i.e. it makes sense at first sight) Then feel free to use or adapt it.

Disclaimer: this is unverified and sometimes I make typos ... just to keep people on their toes. :)

+3
source

One good way to make sure to change the headers to this is:

 $to = ' bob@example.com '; $subject = 'Website Change Reqest'; $headers = "From: " . strip_tags($_POST['req-email']) . "\r\n"; $headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n"; $headers .= "CC: susan@example.com \r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 

Also, double check the rest of the code by comparing this:

http://css-tricks.com/sending-nice-html-email-with-php/

0
source

I would recommend not using the PHP mail() function. Yes, it can send emails, but for something with any complexity it just complicates the job.

I would suggest using a class like phpMailer , which will give you more flexibility. This makes it easy to do things like send HTML emails, add attachments, and use alternative MTAs.

0
source

I had this problem myself, it was fixed using \ n instead of \ r \ n. I use only \ r \ n for the last line in the header.

0
source

The problem is that some anti-virus email scanners consider everything that follows the mime header as body text.

The fix is ​​to make the mime header last.

I had the same problem with one of my clients and I hit her a little.

0
source

This is a problem with Microsoft Outlook (Windows).

The solution is to remove the "\ r" at the end of the headers and just use the "\ n".

0
source

All Articles