Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP convert some angle brackets to html entities in PHPMailer

I'm using PHPMailer and have this line of code inside a for loop to add lines of an HTML table to the body of an email. (full pastebin of php is here: http://pastebin.com/iujp27hU)

$body .= "<tr><td style='". $rush_part_style . "'>" . $row[0]
     . "</td> <td align='center' style='" . $rush_part_style . "'>" . $quantity_to_order
     . "</td> <td>" . $row[2] . "</td></tr>";

The emails normally come out just fine but every once in a while, one of the angled brackets of the td tags is encoded to its HTML entity.

enter image description here

When I view the source in Outlook, here's what it looks like:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">h3, p, table td {font-family:arial;}</style>
<table><tr><td><u>SD Part Number</u></td><td><u>Quantity</u></td><td><u>UOM</u>/td></tr>    
              - - some rows removed for brevity - -    
<tr><td style="">P1400642-00048-C</td><td align="center" style="">26</td><td>EA</td></tr>
<tr><td style="">P1400642-00046-C</td>&lt; td align='center' style=''&gt;20</td>    <td>EA</td></tr>

<tr><td style="">P1400642-00106-C</td><td align="center" style="">25</td><td>EA</td></tr>
<tr><td style="">P1400642-00036-C</td><td align="center" style="">67</td><td>EA</td></tr>
...

I don't get why it randomly encodes one of those < > to their character entities. This problem has happened before but I can send 100+ emails without it happening. The part number has no bearing on whether it appears or not.

How can I troubleshoot this?

--Additional Info Edit--

Testing a very large order of 202 lines produces these formatting errors upon sending. This is with no attributes at all, just pure HTML. PHPMailer mangles it upon Send().

enter image description here

like image 292
S.Mason Avatar asked Jan 06 '15 21:01

S.Mason


People also ask

Which is the PHP function that converts certain characters to HTML entities?

The htmlentities() function converts characters to HTML entities.

What are angle brackets used for in PHP?

So after some consulting with a colleague, he explained it roughly as follows: When you are type hinting an array in a php docblock you use the angle brackets <> to indicate types at different indices in the array. You use curly braces {} for associative arrays where you specify the keys and types their values have.

What is angle bracket HTML?

Angle brackets are commonly used to enclose a code of some type. For example, HTML tags and PageMaker tags are enclosed in angle brackets. They are also used on chat channels and in chat rooms to frame a comment or expression, such as <groan!> or <g>, which is short for <grin>.

What is the use of Htmlentities () function in PHP?

htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities.


1 Answers

Try outputting newline characters \r\n at the end of your table rows

$body    .= "<tr><td style='". $rush_part_style . "'>" . $row[0] . "</td> <td align='center' style='" . $rush_part_style . "'>" . $quantity_to_order . "</td> <td>" . $row[2] . "</td></tr>\r\n";

I had a similar issue, and on inspecting the source of the html email, it appeared that newline characters were somewhat arbitrarily inserted. Adding newline after closing </tr> was the fix for me.

like image 74
billrichards Avatar answered Oct 04 '22 12:10

billrichards