zoukankan      html  css  js  c++  java
  • 【代码片段】Python发送带图片的邮件

    # coding=utf-8
    import smtplib
    from email.mime.text import MIMEText
    
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.mime.image import MIMEImage
    
    msg_from = 'xxxxx@qq.com'  # 发送方邮箱
    passwd = 'xxxxxxxxxxx'  # 填入发送方邮箱的授权码
    msg_to = 'xxxx@qq.com'  # 收件人邮箱
    
    def send():
        subject = "python邮件测试"  # 主题
        msg = MIMEMultipart('related')
        content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>', 'html', 'utf-8')  # 正文
        # msg = MIMEText(content)
        msg.attach(content)
        msg['Subject'] = subject
        msg['From'] = msg_from
        msg['To'] = msg_to
    
        file = open("QR.png", "rb")
        img_data = file.read()
        file.close()
    
        img = MIMEImage(img_data)
        img.add_header('Content-ID', 'imageid')
        msg.attach(img)
    
        try:
            s = smtplib.SMTP_SSL("smtp.qq.com", 465)  # 邮件服务器及端口号
            s.login(msg_from, passwd)
            s.sendmail(msg_from, msg_to, msg.as_string())
            print("发送成功"
        except: 
    print("发送失败")
    finally:
    s.quit()

      

     
  • 相关阅读:
    linux系统根目录文件系统空间不足导致的错误
    python---对象
    公共函数
    PHP接口(interface)和抽象类(abstract)
    mysql引擎
    InstallShield自定义图片资源
    InstallShield 创建自己的Dialog
    InstallShield:自己备份
    注册表和ODBC
    IS脚本学习
  • 原文地址:https://www.cnblogs.com/wyongbo/p/python_send_email.html
Copyright © 2011-2022 走看看