【问题标题】:python smtplib multipart email body not showing on iphonepython smtplib多部分电子邮件正文未在iphone上显示
【发布时间】:2014-06-18 16:45:27
【问题描述】:

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

    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()

似乎当我不添加图像时,电子邮件与正文一起发送正常。关于如何让它与图像一起工作的任何想法?

【问题讨论】:

    标签: python iphone email multipart smtplib


    【解决方案1】:

    这似乎有效:

    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()
    

    【讨论】:

    • 对我不起作用。在 OUTlook 中打开电子邮件时,可以正常看到图像。无法在 iPhone 上查看或下载图像...
    猜你喜欢
    • 2018-11-28
    • 2013-12-16
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 2019-10-16
    • 2011-10-04
    • 2021-04-26
    相关资源
    最近更新 更多