Python群发邮件

今天试了试Python发邮件,突然想到能不能群发邮件呢?群发邮件是smtplib的一个bug,不过最终还是解决了。

直接上代码了

import smtplib
msg = MIMEMultipart()

#构造附件1
att1 = MIMEText(open('/home/a2bgeek/develop/python/hello.py', 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="hello.txt"'#这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)

#构造附件2
#att2 = MIMEText(open('/home/a2bgeek/develop/python/mail.py', 'rb').read(), 'base64', 'gb2312')
#att2["Content-Type"] = 'application/octet-stream'
#att2["Content-Disposition"] = 'attachment; filename="123.txt"'
#msg.attach(att2)

#加邮件头
strTo = ['XXX1@139.com', 'XXX2@163.com', 'XXX3@126.com']
msg['to']=','.join(strTo)
msg['from'] = 'YYY@163.com'
msg['subject'] = '邮件主题'
#发送邮件
try:
    server = smtplib.SMTP()
    server.connect('smtp.163.com')
    server.login('YYY@163.com','yourpasswd')
    server.sendmail(msg['from'], strTo ,msg.as_string())
    server.quit()
    print '发送成功'
except Exception, e:
    print str(e)


 细心的读者会发现代码中有这样一句:msg['to']=','.join(strTo),但是msg[['to']并没有在后面被使用,这么写明显是不合理的,但是这就是stmplib的bug。你只有这样写才能群发邮件。查明原因如下: 

The problem is that SMTP.sendmail and email.MIMEText need two different things.

email.MIMEText sets up the “To:” header for the body of the e-mail. It is ONLY used for displaying a result to the human being at the other end, and like all e-mail headers, must be a single string. (Note that it does not actually have to have anything to do with the people who actually receive the message.)

SMTP.sendmail, on the other hand, sets up the “envelope” of the message for the SMTP protocol. It needs a Python list of strings, each of which has a single address.

So, what you need to do is COMBINE the two replies you received. Set msg‘To’ to a single string, but pass the raw list to sendmail.

好了今天就到这里。

参考资料:

http://www.cnblogs.com/xiaowuyi/archive/2012/03/17/2404015.html

http://sar4.com/python/2012/12/31/smtplib-list-object-has-no-attribute-lstrip/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要使用Python进行发邮件,你可以使用smtplib模块来实现。首先,你需要导入所需的模块和库,如email.mime.text、email.header和smtplib。然后,你需要设置发件人的邮箱地址、密码,以及收件人的邮箱地址列表。接下来,你可以编写邮件的正文内容,并创建一个MIMEText对象。然后,设置邮件的发件人、收件人和主题。最后,通过SMTP服务器连接并登录你的邮箱,使用sendmail方法将邮件发送给收件人,最后断开与SMTP服务器的连接。 以下是一个示例代码: from email.mime.text import MIMEText from email.header import Header import smtplib # 设置发件人信息 from_addr = 'your_email@example.com' password = 'your_password' # 设置收件人信息 to_addrs = ['recipient1@example.com', 'recipient2@example.com'] # 设置邮件正文内容 content = '邮件正文内容' # 创建MIMEText对象 msg = MIMEText(content, 'plain', 'utf-8') # 设置邮件的发件人、收件人和主题 msg['From'] = Header(from_addr) msg['To'] = Header(",".join(to_addrs)) msg['Subject'] = Header('邮件主题') # 连接SMTP服务器并发送邮件 server = smtplib.SMTP('smtp.qq.com') server.connect('smtp.qq.com', 587) server.login(from_addr, password) server.sendmail(from_addr, to_addrs, msg.as_string()) server.quit() 请确保将上述代码中的"your_email@example.com"和"your_password"替换为你的真实邮箱地址和密码,"recipient1@example.com"和"recipient2@example.com"替换为你要发送邮件的收件人邮箱地址。这样,你就可以使用Python进行发邮件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python发邮件](https://blog.csdn.net/wow0524/article/details/122339399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值