python smtplib multipart电子邮件身体不显示在iphone上 [英] python smtplib multipart email body not showing on iphone

查看:147
本文介绍了python smtplib multipart电子邮件身体不显示在iphone上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python中的smtplib发送带有图像的电子邮件。电子邮件显示在我的桌面和iphone gmail应用程序上,但在标准的iphone邮件应用程序,身体不会出现。这是我的代码:

I am trying to send an email with an image using smtplib in python. The email shows up fine on my desktop and on the iphone gmail app, but on the standard iphone mail app the body doesn't appear. Here is my code:

    def send_html_email(self, subject, html, to_email,from_email, password, from_name, image=None):
        msg = MIMEMultipart('alternative')
        msg['From'] = from_name
        msg['To'] = to_email
        msg['Subject'] = subject

        html_message = MIMEText(html, "html")
        msg.attach(html_message)

        if image:
            msgImage = MIMEImage(image)
            msgImage.add_header('Content-ID', '<image1>')
            msg.attach(msgImage)

        session = smtplib.SMTP("smtp.gmail.com:587")
        session.starttls()
        session.login(from_email, password)
        session.sendmail(from_email, to_email, msg.as_string().encode('utf-8'))
        session.quit()

似乎当我不添加图像时,电子邮件与身体一起发送。任何关于如何使其与图像一起工作的想法?

It seems that when I do not add an image, the email sends fine with the body. Any ideas on how to get it working with the image as well?

推荐答案

这似乎起作用:

def send_html_email(self, subject, html, to_email, from_email, password, from_name, image=None):
    msgRoot = MIMEMultipart('related')
    msgRoot['From'] = from_name
    msgRoot['To'] = to_email
    msgRoot['Subject'] = subject
    msg = MIMEMultipart('alternative')
    msgRoot.attach(msg)
    html_message = MIMEText(html, "html")
    msg.attach(html_message)

    if image:
        msgImage = MIMEImage(image)
        msgImage.add_header('Content-ID', '<image1>')
        msgRoot.attach(msgImage)

    session = smtplib.SMTP("smtp.gmail.com:587")
    session.starttls()
    session.login(from_email, password)
    session.sendmail(from_email, to_email, msgRoot.as_string().encode('utf-8'))
    session.quit()

这篇关于python smtplib multipart电子邮件身体不显示在iphone上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆