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()
相关推荐
Aliano2173 分钟前
TestNGException ClassCastException SAXParserFactoryImpl是Java自带的Xerces解析器——解决办法
java·开发语言·python
漫谈网络6 分钟前
回调函数应用示例
开发语言·python·回调函数
亚林瓜子22 分钟前
pyenv简单的Python版本管理器(macOS版)
开发语言·python·macos·pyenv
夜松云22 分钟前
Qt信号槽机制与UI设计完全指南:从基础原理到实战应用
开发语言·qt·ui·qt designer·布局管理·参数传递·qt信号槽
青钰未央24 分钟前
14、Python时间表示:Unix时间戳、毫秒微秒精度与time模块实战
python·改行学it
珂朵莉MM33 分钟前
2024 睿抗机器人开发者大赛CAIP-编程技能赛-专科组(国赛)解题报告 | 珂学家
开发语言·人工智能·算法·leetcode·职场和发展·深度优先·图论
菥菥爱嘻嘻37 分钟前
JS手写代码篇---手写 new 操作符
开发语言·javascript·原型模式
_龙小鱼_1 小时前
Kotlin变量与数据类型详解
开发语言·微信·kotlin
保利九里1 小时前
java中的包机制
java·开发语言
SRC_BLUE_171 小时前
VulnHub | Breach - 1
开发语言·php·dreamweaver