发送带附件的邮件

案例:发送E:\Python_script\目录下 logo.png图片文件到指定的邮箱

import smtplib                           #发送邮件模块
from email.mime.text import MIMEText    #定义邮件内容
from email.mime.multipart import MIMEMultipart  #用于传送附件

#发送邮箱服务器
smtpserver='smtp.163.com'

#发送邮箱用户名密码
user='yuexiaolu2015@163.com'
password='070337shu'

#发送和接收邮箱
sender='yuexiaolu2015@163.com'
receives=['yuexiaolu2015@126.com','yuexiaolu2015@sina.com']


#发送邮件主题和内容
subject='Web Selenium 附件发送测试'
content='<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'


#构造附件内容
send_file=open(r"E:\Python_script\logo.png",'rb').read()

att=MIMEText(send_file,'base64','utf-8')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="logo.png"'

#构建发送与接收信息
msgRoot=MIMEMultipart()
msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
msgRoot['subject']=subject
msgRoot['From']=sender
msgRoot['To'] = ','.join(receives)
msgRoot.attach(att)


#SSL协议端口号要使用465
smtp = smtplib.SMTP_SSL(smtpserver, 465)

#HELO 向服务器标识用户身份
smtp.helo(smtpserver)
#服务器返回结果确认
smtp.ehlo(smtpserver)
#登录邮箱服务器用户名和密码
smtp.login(user,password)

print("Start send email...")

smtp.sendmail(sender,receives,msgRoot.as_string())

smtp.quit()
print("Send End!")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值