unnecessary exclamation marks(!)'s in HTML code
Asked Answered
L

4

6

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('[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()
Lignocellulose answered 1/9, 2013 at 23:2 Comment(6)
Python doesn't add those exclamation marks, I can tell you that much.Sanjay
@MartijnPieters - I am running this on a ubuntu machine..does it have anything do with the platform?any tips on how to fix it?Lignocellulose
No idea; depends on what other components touch that email. You can write or print the msg.as_string() part and verify that no exclamation marks are present, for a start.Sanjay
@MartijnPieters - msg.as_string() doesnt have it either,any other ideas?Lignocellulose
Nope; check your email server. You didn't provide any details of that setup.Sanjay
@MartijnPieters - servers settings ...Internal POP setting: Server name: qcmail1.company.com Port: 995 Encryption method: SSL Internal IMAP setting: Server name: qcmail1.company.com Port: 993 Encryption method: SSLLignocellulose
F
8

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.

Fortyfour answered 2/9, 2013 at 8:0 Comment(0)
P
1

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

Pancreas answered 22/1, 2018 at 15:50 Comment(0)
R
0

I also faced the same issue, Its because outlook doesn't support line more than 990 characters it starts giving below issues.

  1. Nested tables
  2. Color change of column heading
  3. Adding unwanted ! marks .

Here is solution for the same.

if you are adding for single line you can add

"line[:40]" + \r\n + "line[40:]".

If you are forming a table then you can put the same in loop like

"<td>" + line[j][:40]+"\r\n"+line[j][40:] + "</td>"
Ravenna answered 17/10, 2021 at 3:52 Comment(0)
O
0

In my case the html is being constructed outside of the python script and is passed in as an argument. I added line breaks after each html tag within the python script which resolved my issue:

import re
result_html = re.sub(">", ">\n", html_body)
Ozone answered 9/2, 2023 at 15:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.