7

Python2.7 output format not getting as expected if body of email read from a file. user_info.txt is a file generated by another job which contains all the details of users. The output format of user_info.txt is nice. Where as sending that file as an email, output format changes completely. does am I doing something wrong when reading from the file? Can some body help me on this please?

script:

#!/usr/bin/env python
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')
msg['Subject'] = "User Info"

"""Create the body of the message"""
open_file = open('/tmp/user_info.txt')
user_info = MIMEText(open_file.read().encode("utf-8"), 'plain', 'utf-8')
msg.attach(user_info)
open_file.close()

"""Send the message via gmail SMTP server."""
server = smtplib.SMTP('smtp-relay.gmail.com', 587)
server.sendmail("admin", "[email protected]", msg.as_string())
server.quit()

sample user_info.txt

user_name  Department_no ID          detail1     detail2 


aaaa          4         13           25088       32.000000

bbbbbbb       5         17           33280       42.000000

ccccccccccc   3          9           16896       22.000000

dddddd        5         17           33280       42.000000

eeeeeeeee     5         14           27136       36.000000

Email output:

user_name Department_no ID detail1 detail2

aaaa 4 13 25088 32.000000

bbbbbbb 5 17 33280 42.000000

ccccccccccc 3 9 16896 22.000000

dddddd 5 17 33280 42.000000

eeeeeeeee 5 14 27136 36.000000

Please check the email screenshot:

email

17
  • Is the problem in the extra empty lines, or something else? Commented Apr 27, 2017 at 23:05
  • 1
    When you say the "output format changes completely", what does that mean exactly? What are you getting instead of what you expected? Commented Apr 27, 2017 at 23:07
  • added my email screenshot in the description. Please check Commented Apr 27, 2017 at 23:18
  • 2
    The email is not displaying "correctly" because it's not using a fixed-width font. It seems to me that it contains exactly the same characters as user_info.txt. Paste the contents of your email back into your text editor and I think you'll see that once displayed in a fixed-width font, it's formatted as you expected. What you really want to do is to make sure the email that's sent displays in a fixed-width font. I do not know how to do that. Commented Apr 27, 2017 at 23:49
  • @Sweater-Baron it's not having same fixed width as user_info.txt. copied mail into a text editor, the format is not same as text file. Commented Apr 28, 2017 at 0:20

2 Answers 2

4

When sending email in MIMEText(... 'plain') format you don't have control over exactly how it is displayed by the reciever's mail client. Some will use variable width fonts, collapse whitespace, wrap lines in odd places - generally make your pretty fixed width table ugly.

One option is to send an html message body with the plain text as fallback.

...
msg = MIMEMultipart('alternative')
...
with open('/tmp/user_info.txt') as f:
    raw_text = f.read()

msg.attach(MIMEText(raw_text, 'plain'))
msg.attach(MIMEText(convert_to_html(raw_text), 'html'))

That leaves the question of how to implement that convert_to_html() function. I can see two reasonable options:

  • Just wrap in the basic <html>/<body> tags and a <pre>.
  • Actually parse back your plain text table into cells and make an html table.

If you have the data from before creating the text file, you might find it easier to make the table straight from that.

Either way, basic mail clients should fall back to the plain text version and fancy ones that insist on messing with layout will get the html version that specifies you want your message in table form.

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

Comments

3
+50

Question ... output format changes completely. does am I doing something wrong when reading from the file

Tested with Python:3.4.2 and 2.7.9, your code and given user_info.txt format with no failure.
Could receive user_info.txt in the same format.
So, you are doing nothing wrong.


Try the following, without sending your eMail, to verify your used email.mime module work as expected. The output should be OK.

result = user_info.get_payload(decode=True).decode('utf-8','ignore')
print(result)

In general there is no guarantee that a receiver of a eMail uses the same font, size or the same environment.

If you want to be sure, data layout looks the same use a Portabel Document Format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.