简体   繁体   中英

php mail() html - sending as plain text

Folks,

I have sent many emails over the years with PHP and never had an HTML problem. I launched a new Apache/PHP server the other day and emails send fine.

All emails send as plain text. Seems like the encoding isn't working.

Even with the:

$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

Being set... the email sending is not HTML, but plain text.

I can take this same PHP script and run it on another server and I get an HTML email.

So I know my PHP script is correct. What server setting in PHP/Apache would cause all emails to be sent as plain text ?

Here is what the recipient receives on their end in the message area of the text email:

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

From: no-reply@yahoo.com
Message-Id: <20180909154528.C4128612EC@ww1.localdomain>
Date: Sun,  9 Sep 2018 11:45:28 -0400 (EDT)

<html><body><h1>Hello, World!</h1></body></html>

My script code:

$to = 'test99@domain.com';
$subject = 'php test';

$from='no-reply@yahoo.com';

$headers ='';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from.' '. "\r\n";

$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '</body></html>';

mail($to, $subject, $message, $headers);

I assume, that your email client is considering the smtp-server "unsafe", and hence is just going to display all the html as plaintext, rather than rendering it. (To avoid triggering hidden scripts, which might be invoked by loading some linked resources like css or images)

PHPs mail() function uses the smtp-server as it is configured in your php.ini . Most of the time, there is nothing configured at all, so the server acts as "smtp-server" itself, which is guaranteed to be detected as spam cause about 99% of the most basic spam-checks will fail.

I would recommend to switch to some of the PHP-Mail-Clients available (such as https://github.com/PHPMailer/PHPMailer ) and configure every email to be sent (using SMTP-Auth) with a server that is known to be a reliable and well configured SMTP-Server, instead of "localhost".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM