smtplib主题字段TypeError:'str'对象不支持项目分配

时间:2016-10-20 18:01:54

标签: python python-2.7 smtplib

我正在尝试使用smtplib发送电子邮件时向主题字段添加字符串值,但不幸的是,msg ['Subject']总是抛出错误:

TypeError:'str'对象不支持项目分配

以下是我的代码:

    for host in self.hostnames:
        for h in host:
            start = 'Create'
            s = str(h)
            l = str(self.lease)
            u = str(self.username)
            f = str(listFlavor)
            subject = start + ' ' + s + ' ' + l + ' ' + u + ' ' + f

            msg            = 'Add Lease days of %s' % str(self.lease)
            msg['Subject'] = subject
            msg['From']    = self.username
            msg['To']      = 'test@test.com'

            s = smtplib.SMTP('test.test.com')
            s.sendmail('test@test.com', self.username, msg.as_string())
            s.quit()

1 个答案:

答案 0 :(得分:2)

是的,这是真的。

我已添加此行以初步声明消息。

Enter a number (q to quit): -43456 -4356 Enter a number (q to quit): 43456 4356 Enter a number (q to quit): -12321 -123 Enter a number (q to quit): 299792458 297458 Enter a number (q to quit): 0 0 Enter a number (q to quit): 1 1 Enter a number (q to quit): -1 -1 Enter a number (q to quit): q

这是一个dict并保存添加到msg的值。