unnecessary exclamation marks(!)'s in HTML cod

2019-02-26 10:49发布

I am emailing the content of a text file "gerrit.txt" @ http://pastie.org/8289257 in outlook using the below code, however after the email is sent when I look at the source code( @http://pastie.org/8289379) of the email in outlook ,i see unnecessary exclamation markds(!)'s in the code which is messing up the output, can anyone provide inputs on why is it so and how to avoid this ?

from email.mime.text import MIMEText
from smtplib import SMTP

def email (body,subject):
    msg = MIMEText("%s" % body, 'html')
    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()

2条回答
Fickle 薄情
2楼-- · 2019-02-26 10:58

adding a '\n' in between my html string , some random 20 characters before "!" was appearing solved my problem

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-02-26 11:08

Some info available here: http://bugs.python.org/issue6327

Note that mailservers have a 990-character limit on each line contained within an email message. If an email message is sent that contains lines longer than 990-characters, those lines will be subdivided by additional line ending characters, which can cause corruption in the email message, particularly for HTML content. To prevent this from occurring, add your own line-ending characters at appropriate locations within the email message to ensure that no lines are longer than 990 characters.

I think you must split your html to some lines. You can use textwrap.wrap method.

查看更多
登录 后发表回答