smtplib 解决密送失效问题
一般常见的都是
msg[‘Bcc’]=’mail_url’ # 密送地址
这种方式在smtplib中好像并不起作用,所以找了不少资料,终于扎到了解决方法
实际上密送应该添加到sendemail的时候的收件地址后面
他应该是这样的
email_url=’xxxx@163.com
msg[‘Bcc’]=’email_url’ # 密送地址
然后在代码中是这样的
代码语言:javascript复制msg.sendmail(sender, [receiver_qq,mail_bcc], msg.as_string()) # 重点是中间的收件人地址是一个列表,
将刚才定义的收件人填入列表后面即可完成密送,这才是正确的方式
代码语言:javascript复制 import smtplib
from email.mime.text import MIMEText
mail_host = 'smtp.126.com'
mail_user = 'xxx@126.com'
mail_pwd = 'hellopwd'
mail_to = 'xxao@gmail.com'
mail_cc = 'xx@xx.com'
mail_bcc = 'xx@qq.com'
content = 'this is a mail sent with python'
#表头信息
msg = MIMEText(content)
msg['From'] = mail_user
msg['Subject'] = 'this is a python test mail'
msg['To'] = mail_to
msg['Cc'] = mail_cc
msg['Bcc'] = mail_bcc
try:
s = smtplib.SMTP()
s.connect(mail_host)
#login
s.login(mail_user,mail_pwd)
#send mail
s.sendmail(mail_user,[mail_to,mail_cc,mail_bcc],msg.as_string())
s.close()
print 'success'
except Exception ,e:
print e
参考文章https://www.oschina.net/code/snippet_89296_9112