python自动发邮件

一、发送邮箱配置(以163邮箱为例)

设置步骤参考:https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2a5feb28b66796d3b

二、python实现自动发邮件

python发邮件需要两个模块:smtplib和email

  • smtplib模块主要负责发送邮件:是一个发送邮件的动作,连接邮箱服务器,登录邮箱,发送邮件(有发件人,收信人,邮件内容)。
  • email模块主要负责构造邮件:指的是邮箱页面显示的一些构造,如发件人,收件人,主题,正文,附件等。
bash 复制代码
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage


class EmailManage:
    def send_email(self):
        # 发送邮件的用户名和客户端密码
        smtpserver = 'smtp.163.com'
        sender = 'sender@163.com'
        # 第三方客户端登录授权密码
        password = 'XPLTKDYUZFCUAZGC'
        #接收邮件的邮箱
        receiver = 'receiver@qq.com'
        # 邮件的主题
        subject = "全部类型"
        #邮件类型为"multipart/alternative"的邮件包括纯文本正文(text/plain)和超文本正文(text/html)。
        #邮件类型为"multipart/related"的邮件正文中包括图片,声音等内嵌资源。
        #邮件类型为"multipart/mixed"的邮件包含附件。向上兼容,如果一个邮件有纯文本正文,超文本正文,内嵌资源,附件,则选择mixed类型。
        message = MIMEMultipart('mixed')

        # 文字内容
        text = "你好\n文字内容测试\n访问路径:\nhttp://www.baidu.com"
        text_plain = MIMEText(text, 'plain', 'utf-8')
        message.attach(text_plain)

        # html内容
        html = """\
        <html>
          <body>
            <p>This is an example email body.</p>
            <p>It can be in HTML or plain text.</p>
          </body>
        </html>
        """
        text_html = MIMEText(html, 'html', 'utf-8')
        text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
        message.attach(text_html)

        #图片内容
        sendimagefile = open(r'D:\python\file\1.jpg', 'rb').read()
        image = MIMEImage(sendimagefile)
        image.add_header('Content-ID', '<image1>')
        image["Content-Disposition"] = 'attachment; filename="1.png"'
        message.attach(image)

        # 附件
        sendfile = open(r'D:\python\file\测试.txt', 'rb').read()
        text_att = MIMEText(sendfile, 'base64', 'utf-8')
        text_att["Content-Type"] = 'application/octet-stream'
        text_att["Content-Disposition"] = 'attachment; filename="ceshi.txt"'
        message.attach(text_att)

        message['form'] = sender
        message['to']= receiver
        message['subject'] = subject

        #登录smtp服务器
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(sender,password)
        #发送邮件
        smtp.sendmail(sender,receiver,message.as_string())
        #关闭连接
        smtp.quit()

if __name__ == '__main__':
    EmailManage().send_email()
相关推荐
tanyongxi6614 分钟前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔16 分钟前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法
wearegogog12337 分钟前
C语言中的输入输出函数:构建程序交互的基石
c语言·开发语言·交互
Fine姐40 分钟前
The Network Link Layer: 无线传感器中Delay Tolerant Networks – DTNs 延迟容忍网络
开发语言·网络·php·硬件架构
HAPPY酷1 小时前
给纯小白的Python操作 PDF 笔记
开发语言·python·pdf
liulilittle1 小时前
BFS寻路算法解析与实现
开发语言·c++·算法·宽度优先·寻路算法·寻路
阿珊和她的猫1 小时前
autofit.js: 自动调整HTML元素大小的JavaScript库
开发语言·javascript·html
喜欢吃燃面2 小时前
C++算法竞赛:位运算
开发语言·c++·学习·算法
传奇开心果编程2 小时前
【传奇开心果系列】Flet框架实现的家庭记账本示例自定义模板
python·学习·ui·前端框架·自动化
草莓熊Lotso2 小时前
《详解 C++ Date 类的设计与实现:从运算符重载到功能测试》
开发语言·c++·经验分享·笔记·其他