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

python发送邮件带附件_python 发送带附件邮件

时间:2020-03-05 06:11:06

相关推荐

python发送邮件带附件_python 发送带附件邮件

import smtplib

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

from email.mime.application import MIMEApplication

def sendEmail(title, text, send, to, passwd, smtp_server, file):

'''

发送带附件的邮件

:param title: 邮件标题

:param text: 邮件正文

:param send: 发送者邮箱

:param passwd: 授权码

:param to: 接收者邮箱

:param smtp_server: 发送邮件的服务器

:param file: 需要发送的附件

:return:

'''

msg = MIMEMultipart()

msg['From'] = send

msg['To'] = to

#文字部分

msg['Subject'] = title # 主题

strstr=text #文字内容

att = MIMEText(strstr,'plain','utf-8')

msg.attach(att)

#附件

att = MIMEApplication(open(file,'rb').read()) #你要发送的附件地址

att.add_header('Content-Disposition', 'attachment', filename=file) #filename可随意取名

msg.attach(att)

server = smtplib.SMTP()

server.connect(smtp_server) #连接smtp邮件服务器

server.login(send,passwd) #登录smtp邮件服务器

server.sendmail(send, to, msg.as_string()) #发送

server.close() #关闭

if __name__ == '__main__':

smtp_server = '' # 使用QQ邮箱的SMTP服务器,可切换

from_mail = '*****@'

mail_pass = '*****'

to_mail = '******@'

title = 'test'

text = 'send test'

file = 'report_-04-08-11-02-30.html'

sendEmail(title=title, text=text, send=from_mail, to=to_mail, passwd=mail_pass, smtp_server=smtp_server, file=file)

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