php mail() html - sending as plain text - php

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

Related

how to use clickabe link (href link) in php mail

i want to use click able link in email, but it is not reflecting in email sending through php mail function, below is my code
$url = "<html><a href='www.google.com'>Link</a></html>";
$message = "Hi test url ".$url." ";
$$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: Admin <test#ex.com> \r\n";
#mail('test1#ex.com',$subject,$message,$headers);
Content which i'm getting from email:
Hi test url <html><a href='www.google.com'>Link</a></html> ##NEW##
You don't seem to be sending HTML mail correctly. Usually I will recommend using a third-party PHP library, like SwiftMailer, to simplify the process.
Edit: If you still wish to use php's mail() function, you will have to specify the headers correctly. This article could help.
To use the HTML in phpmailler:
$mail->isHTML(true)
You review the documantation
phpMailler

Modify date header in PHP email?

Is it possible to manipulate the date header using PHP's mail function/sendmail (website server is linux so I believe it's using sendmail underneath)?
I'm trying to test error handling of a (windows based) email checker, and need to generate an invalid date header in a message, but when I send my message using php mail, even though I am including a custom Date header, it seems to get overridden by the mail server and my custom header ignored. I'm guessing I might need an additional parameter for sendmail to tamper with that header, but I can't seem to find what that parameter would be, or whether this is even possible.
Here is what I am sending (with the personal domain info removed)
$headers = "From: ..................\r\n";
$headers .= "Message-ID: <" . md5(uniqid(time())) . "#..................>\n";
$headers .= "MIME-Version: 1.0\n";
//$headers .= "Date: ".date("D, d M Y H:i:s") . " UT\n"; //a valid header for comparison
$headers .= "Date: Tuesday\n"; // intentionally bogus email header
$headers .= "Reply-To: ..................\n";
$headers .= "Return-Path: ..................\r\n";
$headers .= "X-Priority: 3\r\nX-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP/".phpversion()."\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
$headers .= "\n";
$success=mail($to, $s_subject, $s_emailmsg, $headers);
Assuming you wrote an email checker that runs via procmail, an easier way might be to construct an email message yourself and send that directly to the test script via standard input.
It's easier to write unit tests that way as it bypasses any interference by other mail servers. When you do follow this, make sure that the first line starts with MAIL FROM xx#yy.com.

PHP mailer to send mail with HTML contents

I am using php mail method to send HTML mails to people from my domain. I am using the following code to send mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
mail($id, $subject, $message,$headers);
However, the problem is that the code doesnt work when the size of message increases. It works fine for smaller messages.
Is there any way to overcome that ??
I resolved my issue. It was due to less number of breaking tags br in the html code. I introduced some br tags here and there and it flew :)

PHP mail(): Header appears in mail body

I have a problem with a mailing application I'm currently developing with php on a linux server. After sending an email to a bunch of different addresses with different clients on it, sometimes these mails can't be read by the receivers.
For example the body appears to start with this:
boundary="=_2cac04098ebf51c342bd57eab2200e38"
Message-ID: <lo5huc.id4ip6qutsch.lforce.de>
Date: Mon, 11 Jul 2011 06:01:24 +0200 (CEST)
--=_2cac04098ebf51c342bd57eab2200e38
I really have no clue what's happening to my mails. Each line in the header is separated by \n, the boundary entry has a leading \t. Though the client seems to read a line break which is not there while parsing my header. It also happens in other parts of the header.
Has anyone ever had a similar problem? Please help me!
Andy
UPDATE: I'm pretty sure it's no coding error. I've been coding this mail stuff for years (even wrote my own mail client) and it worked perfectly. Right now we use the RMAIL class which is also from a bigger open source project. I think it's more like a problem with my system configuration... but that's just a guess.
Make sure you seperate every header line correctly, for instance:
<?php
//To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I suggest using PHPMailer, easy to use, takes care of all nesseccery headers, easy attachment sending, multiple recipients etc.
http://phpmailer.worxware.com/index.php?pg=phpmailer

Sending emails with styling and formatting [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Php mail: how to send html?
formatting email in php?
I'm using PHP to send emails, like this:
$to = $emailAddress;
$subject = "qwerty";
$message = "foo" . PHP_EOL . "bar";
$headers = 'From: "asdf" <email#email.com>';
mail($to, $subject, $message, $headers);
When I attempt to put HTML code within the message, it just sends the actual HTML as plaintext. This leads me to ask - how can I format emails as I can format webpages?
Make sure you're setting your headers for text/html, like so:
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
See Example #4 in the PHP Manual for the full example.
To add style to your email, you first need to add a header as mentioned by jmort253, like this:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Once you have added this to you header. You can do styling in the actual message, ie:
$message = "<html><body><h1>Welcome</h1><p>Thanks for the email</p></body></html>";
Make sure that you do inline CSS or put it in the header, don't put it in an external CSS file. As for images, you'll need to include full paths to your server, so that the client mail application can find the image on the web.
Hope this helps.
I recommend not reinventing the wheel. Instead use phpmailer class or something similar. Will solve many other upcoming problems as well :)

Categories