for some reason the following code does not set the Subject field properly, and certainly does not render the email in HTML… ie, the and tags are gone when email is received.
Thanks!
def sendEmail (self,remove):
message = """From: From Postmaster <%s>
To: To Person <%s>
MIME-Version: 1.0
Content-type: text/html
Subject: blahblah
<b>This is HTML message.</b>
<h1>The following email addresses have been removed</h1>
%s
""" %(self.sender,self.receivers,remove)
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(self.sender, self.receivers, message)
print "Successfully sent email"
MIME has specific rules for how whitespace is handled in its headers. In particular, lines that begin with whitespace are treated as continuation lines for the previous header. In your example, since each line (after the first) begins with whitespace, all of the content will be treated as the value of the
Fromheader. In addition, as @TokenMacGuy pointed out MIME requires a blank line between the headers and the payload of the message. If you reformat your message follows, it should be interpreted correctly:Should address the problems you’re seeing.
Alternatively, you could use the
emailpackage and avoid managing the format of the message yourself: