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()
相关推荐
LyaJpunov7 分钟前
C++中move和forword的区别
开发语言·c++
程序猿练习生11 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
一名路过的小码农21 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
m0_6312704024 分钟前
标准c语言(一)
c语言·开发语言·算法
万河归海42824 分钟前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
Messiah___30 分钟前
【论文阅读】Slim Fly: A Cost Effective Low-Diameter Network Topology 一种经济高效的小直径网络拓扑
开发语言·php
农民小飞侠1 小时前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
指尖流烟1 小时前
C#调用图表的使用方法
开发语言·c#
战神刘玉栋1 小时前
《程序猿之设计模式实战 · 观察者模式》
python·观察者模式·设计模式
敲代码不忘补水1 小时前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践