电子邮件列表的元组作为正文 - Python

时间:2016-04-12 16:58:49

标签: python list email tuples

我正在使用由服务器(名称,IP)元组组成的列表。我正在使用ping测试每个服务器的连接性。如果ping失败,则将其添加到名为issues的列表中。我试图通过电子邮件将这个失败列表发送给我自己,因为有任何失败。我不确定自己出了什么问题,但我收到了以下错误:

Traceback (most recent call last):
  File "C:/Python27/Scripts/serverconnection.py", line 26, in <module>
    msg['SUBJECT'] = "Server Disconnect Notice"
TypeError: 'str' object does not support item assignment

产生错误的代码在

之下
if len(issues) > 0 :
    body = '\n'.join('%s, %s' % server for server in issues)
    msg = body
    msg['SUBJECT'] = "Server Disconnect Notice"
    msg['FROM'] = "Alli Deacon"
    msg['TO'] = 'allid@atlanticpkg.com'
    msg.attach(text)

2 个答案:

答案 0 :(得分:0)

因为 msg str ,所以你不能像在dict中那样msg['SUBJECT']

尝试以下方法:

msg = dict()
msg['SUBJECT'] = "Server Disconnect Notice"
msg['FROM'] = "Alli Deacon"
msg['TO'] = 'allid@atlanticpkg.com'
msg.send() # i don't know what module for e-mails you use

答案 1 :(得分:0)

我需要以下导入:

from email.MIMEText import MIMEText

然后使用以下内容更新我的代码:

msg = MIMEText(body)

我更新了这些添加后,代码运行正常。