• Python学习笔记第二十三天(HTML格式的邮件)


    Python发送HTML格式的邮件

    Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。

    # 实例 1
    import smtplib 
    from email.mime.text import MIMEText 
    from email.header import Header 
    sender = 'from@XX.com' 
    receivers = ['XXXXXX@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 
    mail_msg = """ 

    Python 邮件发送测试...

    这是一个链接

    """
    message = MIMEText(mail_msg, 'html', 'utf-8') message['From'] = Header("baidu", 'utf-8') message['To'] = Header("测试", 'utf-8') subject = 'Python SMTP 邮件测试' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message.as_string()) print("邮件发送成功") except smtplib.SMTPException: print("Error: 无法发送邮件")
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Python发送带附件的邮件

    发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后利用smtplib.smtp发送。

    # 实例 2
    import smtplib 
    from email.mime.text import MIMEText 
    from email.mime.multipart import MIMEMultipart
    from email.header import Header 
    sender = 'from@XX.com' 
    receivers = ['XXXXXX@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 
    #创建一个带附件的实例 
    message = MIMEMultipart() 
    message['From'] = Header("百度", 'utf-8')
    message['To'] = Header("测试", 'utf-8') 
    subject = 'Python SMTP 邮件测试' 
    message['Subject'] = Header(subject, 'utf-8')
    #邮件正文内容 
    message.attach(MIMEText('这是Python 邮件发送测试……', 'plain', 'utf-8'))
    # 构造附件1,传送当前目录下的 test.txt 文件 
    att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8') 
    att1["Content-Type"] = 'application/octet-stream' 
    # 这里的filename可以任意写,写什么名字,邮件中显示什么名字 
    att1["Content-Disposition"] = 'attachment; filename="test.txt"' 
    message.attach(att1) 
    # 构造附件2,传送当前目录下的 baidu.txt 文件 
    att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8') 
    att2["Content-Type"] = 'application/octet-stream' 
    att2["Content-Disposition"] = 'attachment; filename="baidu.txt"' 
    message.attach(att2) 
    try: 
        smtpObj = smtplib.SMTP('localhost') 
        smtpObj.sendmail(sender, receivers, message.as_string()) 
        print("邮件发送成功" )
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    HTML文本中添加图片

    邮件的 HTML 文本中一般邮件服务商添加外链是无效的,正确添加图片

    import smtplib 
    from email.mime.image import MIMEImage 
    from email.mime.multipart import MIMEMultipart 
    from email.mime.text import MIMEText 
    from email.header import Header 
    sender = 'from@XX.com' 
    receivers = ['XXXXXX@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 
    msgRoot = MIMEMultipart('related')
    msgRoot['From'] = Header("百度", 'utf-8') 
    msgRoot['To'] = Header("测试", 'utf-8') 
    subject = 'Python SMTP 邮件测试' 
    msgRoot['Subject'] = Header(subject, 'utf-8') 
    msgAlternative = MIMEMultipart('alternative') 
    msgRoot.attach(msgAlternative)
    mail_msg = """ 

    Python 邮件发送测试...

    菜鸟教程链接

    图片演示:

    """ msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8')) # 指定图片为当前目录 fp = open('test.png', 'rb') msgImage = MIMEImage(fp.read()) fp.close() # 定义图片 ID,在 HTML 文本中引用 msgImage.add_header('Content-ID', '') msgRoot.attach(msgImage) try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, msgRoot.as_string()) print("邮件发送成功" except) smtplib.SMTPException: print("Error: 无法发送邮件")
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    今天学习的是Python发送邮件学会了吗。 今天学习内容总结一下:

    1. Python发送HTML格式的邮件
    2. Python发送带附件的邮件
    3. HTML文本中添加图片
  • 相关阅读:
    复杂查询方法-视图、子查询、函数等
    数据分析实战 | 多元回归——广告收入数据分析
    基于SSM的OA办公系统
    【架构艺术】(零) 环境搭建
    我的大学期末网页作业 仿学校网站制作实现 HTML+CSS西北大学新闻网带psd带js
    C++怎么判断windows系统是64位还是32位
    ps2021版神经滤镜能用吗,ps2021没法用神经元滤镜
    【主流技术】聊一聊消息队列 RocketMQ 的基本结构与概念
    R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
    Base64隐写
  • 原文地址:https://blog.csdn.net/qq_54129105/article/details/127954336