1

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('userid@company.com', ['userid2@company.com'],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()
user2341103
  • 2,333
  • 5
  • 20
  • 19

1 Answers1

1

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.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thanks,any idea why the table might get messedup?is there a way to escape any junk characters from each cell? – user2341103 Sep 01 '13 at 19:01
  • hm, I've sent it with `html` passed - the table is not messed up. I'm seeing only one html problem - `font` tag is not closed at the end - though, it's not relevant I think. – alecxe Sep 01 '13 at 19:06
  • do you have a way to send the email in outlook? – user2341103 Sep 01 '13 at 19:17
  • Nope. Could you post the html that gets messed up in the outlook? – alecxe Sep 01 '13 at 19:18
  • Nope. Does the html that you send via email persistent? You said it gets messed up sometimes - html changes? If yes, just show what html is messed up on the outlook side. – alecxe Sep 01 '13 at 19:25
  • @alexce - never mind ,I figured out..here is the messed up HTML code http://pastie.org/8289379 – user2341103 Sep 01 '13 at 19:26
  • There are strange `!` chars there, see ``,`!ht="100">` and ``..do you have it in the source html that you send? – alecxe Sep 01 '13 at 19:29
  • @alexce - do you know why the code is not continuos.."!" breaks it up..as you can see the code @"Yes<! td>Provided" will be one cell – user2341103 Sep 01 '13 at 19:30
  • @alexce - yes,i have it in the source code I sent..while doing the email conversion from the text file..outlook is getting this strange "!" character..how to escape this character ? – user2341103 Sep 01 '13 at 19:33
  • Ok, thanks. I'm not actually sure how do you want to escape it. It really breaks up the html while being inside the closing `td` tags. It just shouldn't be there. – alecxe Sep 01 '13 at 19:35
  • yes,that seems to be problem,need some ideas on how to fix it? – user2341103 Sep 01 '13 at 19:39