你好,我是UP:法学院毕业的Python程序员兆锋(点击查看🏆近期的付费答疑记录),
如有学习问题,欢迎加入学习群和我交流❤交流群
主要内容
视频内容
点击下方链接,直达本讲视频
点击查看【bilibili】
本讲代码
# -*- coding: utf-8 -*-
# @Time : 2020/8/23 2:22
# @公众号 :Python自动化办公社区
# @File : 24_用Python发送带附件的邮件.py
# @Software: PyCharm
# @Description:
# -*- coding: utf-8 -*-
# @Time : 2020/8/23 2:15
# @公众号 :Python自动化办公社区
# @File : 23_用Python发送普通的文字邮件.py
# @Software: PyCharm
# @Description:
import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
host_server = 'smtp.sina.com'
sender_sina = 'your_email@sina.com'
pwd = 'your_pwd'
sender_sina_mail = 'your_email@sina.com'
receiver = 'others_email@sina.com'
mail_title = '这是标题'
mail_content = '这是正文'
msg = MIMEMultipart()
msg['Subject'] = Header(mail_title, 'utf-8')
msg['From'] = sender_sina_mail
msg['To'] = Header('mail_title', 'utf-8')
msg.attach(MIMEText(mail_content, 'html', 'utf-8'))
# 添加附件
attachment = MIMEApplication(open('student.xls', 'rb').read())
attachment.add_header('Content-Disposition', 'attachment', filename='student.xls')
msg.attach(attachment)
try:
smtp = SMTP_SSL(host_server)
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_sina, pwd)
smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
smtp.quit()
print('success')
except smtplib.SMTPException:
print('error')
配套文档
拓展内容
- Pandas官方文档中文版PDF下载
- 详解 | Python&PyCharm的软件下载和安装
- 我在视频中用的代码字体和页面主题
- 🌈彩蛋文件夹,全是自动化办公干货,不点后悔系列
- Python知识星球👉点击直达
提问与答疑
提供以下3种交流方式: