0

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);
Matthew
  • 9
  • 2
  • Which SMTP server are you using? Is it set up to send plain-text emails only? Could you show off some code as well? – Jefrey Sobreira Santos Sep 09 '18 at 16:10
  • 1
    can you share your mail script where you use the headers to the mail() function? – unixmiah Sep 09 '18 at 16:11
  • Hi, it seems you have a newline before your content-type ; any empty line between headers tells the email client the text after the newline is the email content, not it's headers. That being said, if your script works fine in one server and not the new one, maybe this is a config issue with newline character (is it linux servers in both cases, or one is a windows server ?). By the way, you shouldn't have an empty line before your From header. – Pierre Sep 09 '18 at 16:11
  • SMTP = sendmail on localhost – Matthew Sep 09 '18 at 16:17
  • Both Linux servers... Centos – Matthew Sep 09 '18 at 16:18

1 Answers1

1

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".

user3783243
  • 5,368
  • 5
  • 22
  • 41
dognose
  • 20,360
  • 9
  • 61
  • 107
  • The email client is working.... when it sends from the other server... the HTML is displayed as it should be. – Matthew Sep 09 '18 at 16:41
  • @Matthew receiving the same email from 2 different servers doesn't mean my email client will provide the same level of trust for both servers. – dognose Sep 09 '18 at 17:17