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
buffer_results.close()   

# Send out email notification stating the test execution is completed.
COMMASPACE = ', '
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)
server.ehlo()
server.ehlo()
text = content.as_string()
server.sendmail(fromaddr, recipient_list, text)
server.quit
Last edited by Yoriz on Sat Oct 26, 2013 9:01 pm, edited 1 time in total.
Reason: First post lock, Added code tags
rbalbat
 
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!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
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.
Consider:
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.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests