Reputation: 2433
I have the HTML code @http://pastie.org/8289257 in a text file "gerrit.txt" which will create a table with some contents ,I convert the text file to a .htm file and open it the output looks perfectly fine,this tells me the HTML code is fine,but when I email(using outlook) it using the below code,table sometims gets messed up. I need ideas on what other ways can I send email?I tried SMTP as below which doesnt seem to work...
from email.mime.text import MIMEText
from smtplib import SMTP
def email (body,subject):
msg = MIMEText("%s" % body)
msg['Content-Type'] = "text/html; charset=UTF8"
msg['Subject'] = subject
s = SMTP('localhost',25)
s.sendmail('[email protected]', ['[email protected]'],msg=msg.as_string())
def main ():
# open gerrit.txt and read the content into body
with open('gerrit.txt', 'r') as f:
body = f.read()
subject = "test email"
email(body,subject)
print "Done"
if __name__ == '__main__':
main()
Upvotes: 1
Views: 322
Reputation: 474201
Your code is completely correct except that you need to pass html
type to MIMEText
:
msg = MIMEText("%s" % body, 'html')
I've tested it with my gmail account, seen html code in the message without setting html
type.
Alternatively, you can use mailer package as suggested here.
Upvotes: 1