开发者

PHP sendmail() formatting not preserved

开发者 https://www.devze.com 2023-01-09 12:31 出处:网络
I am creating a web application for a client that has the ability to send out emails. I am using TinyMCE for my text editor, which works quite nicely. I am using sendmail() with PHP Swiftmailer to han

I am creating a web application for a client that has the ability to send out emails. I am using TinyMCE for my text editor, which works quite nicely. I am using sendmail() with PHP Swiftmailer to handle the actual sending of the email. Swiftmailer works nicely, as well.

The only problem that I am running into is that when I receive the email (in Gmail), the formatting is not rendered correctly. I am receiving the following in the body of my email:

<p>Oh Hello! This is a test <开发者_StackOverflow社区strong>message</strong>. Here is a link: <a href=\\\"http://www.google.com\\\">Google</a>.</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>Line breaks!<br /> <br /> Shift breaks!</p>\r\n<p>&nbsp;</p>\r\n<p>Bye!</p>

The links render, and that's about it. What am I missing?

Thanks!


Check out the second and third code blocks in the documentation for how to set the HTML Content-Type...

http://swiftmailer.org/docs/header-parameterized


You need to send your e-mail in an actual HTML format. You are instead sending the plain text of the HTML. The links render because Google is nice and automatically links what it believes to be a valid URL.

I strongly recommend the Pear mail class. There are convenient functions for setting HTML and plain text message bodies.


What I would do in your case is something like this:

<?php

$headers = "From: Me <me@myemail.com>\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";  //States it is HTML Content
$headers .= "Content-Transfer-Encoding: 7bit\r\n";

$subject = "This is my subject";

$message = <<<MESS
<p>Oh Hello! This is a test <strong>message</strong>. Here is a link: <a href="http://www.google.com">Google</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Line breaks!<br /> <br /> Shift breaks!</p>
<p>&nbsp;</p>
<p>Bye!</p>
MESS;

if(mail("", $subject, $message, $headers) == True){
     echo "Message Sent";
} else {
     echo "Message NOT Sent"
}

?>

NOTE: GMAIL is REALLY picky about how it displays emails. Best to create a gmail account and test for yourself how it looks. Hotmail is similar

0

精彩评论

暂无评论...
验证码 换一张
取 消