2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > python发送邮件带附件_Python发送邮件(带附件)

python发送邮件带附件_Python发送邮件(带附件)

时间:2023-11-02 15:47:16

相关推荐

python发送邮件带附件_Python发送邮件(带附件)

import smtplib #发送邮件模块

from email.mime.textimport MIMEText #定义邮件内容

from email.mime.multipartimport MIMEMultipart #用于传送附件

#发送邮箱服务器

smtpserver=‘‘

#发送邮箱用户名密码

user=‘15291625900@‘

password=‘*******‘

#发送和接收邮箱

sender=user

receives=[‘zhaofeng612326@‘,‘zf263283998@‘]

#发送邮件主题和内容

subject=‘python带附件邮件发送‘

content=‘

python带附件邮件发送测试

#构造附件内容,添加E:\Python_script\logo.png为附件

send_file=open(r"E:\Python_script\logo.png",‘rb‘).read()

att=MIMEText(send_file,‘base64‘,‘utf-8‘)

att["Content-Type"]=‘application/octet-stream‘

# filename为附件所显示的名称

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!")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。