0

I wanted to make a small cronmailer for my personal use. And here's the part wich do not work

# Create the body of the message (a plain-text and an HTML version).
text = "LOG OUTPUT: " + sys.argv[1] + "\n"
logdata = open(sys.argv[2], "rb") 
for row in logdata:
        text = text + row

html = """\
<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: {0} <br> {1} 
    </p>
  </body>
</html>
""".format(unicode(str(sys.argv[1]),'utf-8'),  unicode(str(logdata),'utf-8'))


part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

server = smtplib.SMTP(str(server) + ":" + str(port))
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()   

I get the mail. With the Plain Text and the attachment (left the code out, cause it works). But not the HTML part. The only thing I get there is:

<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: test

Thanks for any help and a Happy New Year

11
  • I can't help but notice that the output ends right where the <br> element that you didn't close starts. If you're lucky, changing that to <br /> might actually solve your problem. Commented Jan 1, 2015 at 16:26
  • You want to include content of the file? afaik, unicode(str(logdata),'utf-8') doesn't return the content of the file Commented Jan 1, 2015 at 16:26
  • @FrEaKmAn how can i add the content of the file right into the HTML ? it only contains text. Commented Jan 1, 2015 at 16:28
  • Read the file with logdata.read(). Commented Jan 1, 2015 at 16:30
  • 1
    @SvenBamberger, you have "the content of the file" in variable text (obtained very laboriously when a .read() as suggested by FrEaKmAn would be simpler, but, that's a detail) -- so escape and insert it. Escaping is important and you're not doing it now! You're inserting a string such as "<open file 'foo.txt', mode 'rb' at 0x109a08810>" and those unescaped angle brackets no doubt cause your problem... Commented Jan 1, 2015 at 16:33

1 Answer 1

2

Here are likely-better ways to build that html string (in Python 2):

import cgi

text = "LOG OUTPUT: " + sys.argv[1] + "\n"
with open(sys.argv[2], "rb") as f:
    text += f.read()

html = """\
<html>
  <head></head>
  <body>
    <p>LOG OUTPUT: {0}<br/>
    <pre>{1}</pre>
    </p>
  </body>
</html>""".format(unicode(str(sys.argv[1]), 'utf-8'),
                  unicode(cgi.escape(text), 'utf-8'))

Note that in Python 3 the escaping function is html.escape (so you'd better not name your own variable html as it will clash:-), but for Python 2, where the escaping function is the peculiarly named cgi.escape, this should be OK.

Sign up to request clarification or add additional context in comments.

1 Comment

Just modified this <p>LOG OUTPUT: {0}<br/> <pre>{1}</pre> into <p><pre>{0}</pre> to Prevent double lined LOG OUTPUT: XXXX

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.