我尝试使用Python脚本发送电子邮件,但是以我期望的格式发送的邮件没有按照我的预期显示。下面是我的发送电子邮件方法 -
def send_mail(data):
sender = 'fromuser@host.com'
receivers = ['touser@host.com']
message = """From: fromuser@host.com
To: touser@host.com
Subject: Send mail from python!!
"""
body = 'Some Text\n'
for item in data:
body = body + '{name} - {res}\n'.format(name=item['name'], res=item['res'])
message = message + body
try:
smtpObj = smtplib.SMTP('corp.host.com' )
smtpObj.sendmail(sender, receivers, message)
print "Mail sent"
except smtplib.SMTPException:
print "You can't spam. Mail sending failed!"
这里的数据只有键值对。
我在Outlook邮箱中收到了这样一封电子邮件 -
在我的Outlook中,发件人:
部分显示以下字符串,这是错误的 -
fromuser@host.com To: touser@host.com Subject: Send mail from python!!
同时,“收件人”(To:
)和“主题”(Subject:
)为空,这也是错误的。
在正文中,我看到所有内容都在一行中,但我希望结果显示为-
Some Text
machinA - 0
machineB - 0
machineC - 0
我该如何表示我的数据以在Outlook邮箱中显示如此?