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 = '[email protected]'
        # 第三方客户端登录授权密码
        password = 'XPLTKDYUZFCUAZGC'
        #接收邮件的邮箱
        receiver = '[email protected]'
        # 邮件的主题
        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()
相关推荐
ai大师2 分钟前
(附代码及图示)Multi-Query 多查询策略详解
python·langchain·中转api·apikey·中转apikey·免费apikey·claude4
小小爬虾23 分钟前
关于datetime获取时间的问题
python
我很好我还能学1 小时前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
蓝婷儿1 小时前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
渣渣盟2 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
chao_7892 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
糯米导航2 小时前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计
糯米导航2 小时前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计
MessiGo3 小时前
Javascript 编程基础(5)面向对象 | 5.1、构造函数实例化对象
开发语言·javascript·原型模式
大霞上仙3 小时前
nonlocal 与global关键字
开发语言·python