Why are some < characters are being replaced with &lt;

This is the place for queries that don't fit in any of the other categories.

Why are some < characters are being replaced with &lt;

Postby rbalbat » Sat Oct 26, 2013 7:21 pm

I have a python script that reads in an HTML document then sends it out as part of an email. I am finding that a few of the < and > characters are being replaced with &lt; and &gt; when I open the file in my mail viewer. Any idea why this is happening?

Code: Select all
# Import libraries
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

buffer_results = open('Overview.html', 'r')
HTMLContent = buffer_results.read()
print HTMLContent

# Send out email notification stating the test execution is completed.
fromaddr = "soapuixxxx@gmail.com"
content = MIMEMultipart()
content['From'] = fromaddr
recipient_list = ["aaa@bbb.com","ccc@ddd.com"]
content['To'] = COMMASPACE.join(recipient_list)
content['Subject'] = "results are done"
content.attach(MIMEText(HTMLContent, 'html'))
server = smtplib.SMTP('ese1-mailrelay1.xxx.lan', 25)
text = content.as_string()
server.sendmail(fromaddr, recipient_list, text)
Last edited by Yoriz on Sat Oct 26, 2013 9:01 pm, edited 1 time in total.
Reason: First post lock, Added code tags
Posts: 1
Joined: Sat Oct 26, 2013 7:11 pm

Re: Why are some < characters are being replaced with &lt;

Postby Yoriz » Sat Oct 26, 2013 9:02 pm

Hi, welcome to the forum.
Ive added code tags to your code for you , please read the 'new users read this' link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
Posts: 1030
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Why are some < characters are being replaced with &lt;

Postby ochichinyezaboombwa » Mon Oct 28, 2013 11:39 pm

< and > are a part of HTML / XML markup syntax. In other words, they are 'special'.
If you also use them as a part of plain text, it might confuse a hell out of parsing engines.
Code: Select all
<p> <Hello!> </p>

So to decrease confusion they are escaped: < to "&lt;" and > to "&gh;" . You can a table of all html escape characters by searching the Internet.
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests