python邮件发送给多人时,只有第一个人能收到的问题

问题:在python3.5中使用sendmail进行邮件发送,mailInfo["to"]为逗号分隔的str类型,结果只有第一个邮件地址能收到邮件。
解决方法:经过多次测试发现MIMEText()["to"]的数据类型与sendmail(from_addrs,to_addrs,...)的to_addrs不同;前者为str类型,多个地址使用逗号分隔,后者为list类型。
解决示例如下:
...
mailInfo = {
        "from": "xxxx@xxx.com",
        "to": "xxx@xxx.com,xxx@xxx.com,xxx@xxx.com",
        "hostname": "smtp.xxx.com",
        "username": "xxxx@xxx.com",
        "password": "xxxxx",
        "mailsubject": "主题",
        "mailtext": "正文内容",
        "mailencoding": "utf-8"
    }
def sendsmtp(mailInfo):
    smtp = SMTP_SSL(mailInfo["hostname"])
    smtp.set_debuglevel(1)
    smtp.ehlo(mailInfo["hostname"])
    smtp.login(mailInfo["username"], mailInfo["password"])
    msg = MIMEText(mailInfo["mailtext"], "plain", mailInfo["mailencoding"])
    msg["Subject"] = Header(mailInfo["mailsubject"], mailInfo["mailencoding"])
    msg["from"] = mailInfo["from"]
    msg["to"] = mailInfo["to"]
    smtp.sendmail(mailInfo["from"], mailInfo["to"].split(','), msg.as_string())
    smtp.quit()

                
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值