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", "user1@test.com", 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: