使用Python同时向多个抄送和多个收件人发送电子邮件 [英] Send e-mail to multiple CC and multiple TO recipients simultaneously using python

查看:21
本文介绍了使用Python同时向多个抄送和多个收件人发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅分别尝试了多个收件人和多个抄送,工作正常,但当我同时尝试这两个时,出现错误:

文件

"pathContinuumanaconda2envsmypythonlibsmtplib.py", 第870行,在Sendmail senderRs[each]=(code,resp)TypeError: 不可散列的类型:‘list’"

编码:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

strFrom = 'fasdf@dfs.com'

cc='abc.xyz@dfa.com, sdf.xciv@lfk.com'

to='sadf@sdfa.com,123.lfadf@fa.com'

msg = MIMEMultipart('related')
msg['Subject'] = 'Subject'
msg['From'] = strFrom
msg['To'] =to
msg['Cc']=cc

#msg['Bcc']= strBcc

msg.preamble = 'This is a multi-part message in MIME format.'


msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)


msgText = MIMEText('''<html>

<body><p>Hello<p>
        </body>
        </html> '''.format(**locals()), 'html')
msgAlternative.attach(msgText)



import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp address')
smtp.ehlo()
smtp.sendmail(strFrom, to, msg.as_string())
smtp.quit()

推荐答案

附件Tofrom应为字符串,并且发送邮件应始终采用列表形式。

cc=['abc.xyz@dfa.com', 'sdf.xciv@lfk.com']

to=['sadf@sdfa.com','123.lfadf@fa.com']

msg['To'] =','.join(to)

msg['Cc']=','.join(cc)   

toAddress = to + cc    

smtp.sendmail(strFrom, toAddress, msg.as_string())

这篇关于使用Python同时向多个抄送和多个收件人发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆