【问题标题】:send html email with embedded image using python script使用 python 脚本发送带有嵌入图像的 html 电子邮件
【发布时间】:2022-01-16 03:21:51
【问题描述】:

我是 Python 的新手。我想将带有嵌入在左上角的公司徽标的基于 html 的电子邮件发送到电子邮件正文。

使用以下代码,电子邮件绝对可以正常工作,但不再附加嵌入的图像。不知道我在哪里做错了。谁能帮帮我。

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.MIMEImage import MIMEImage

msg = MIMEMultipart('alternative')
msg['Subject'] = "My text dated %s" % (today)
            msg['From'] = sender
            msg['To'] = receiver

html = """\
<html>
<head></head>
<body>
  <img src="cid:image1" alt="Logo" style="width:250px;height:50px;"><br>
  <p><h4 style="font-size:15px;">Some Text.</h4></p>
</body>
</html>
"""

# The image file is in the same directory as the script
fp = open('logo.png', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header('Content-ID', '<image1>')
msg.attach(msgImage)

part2 = MIMEText(html, 'html')
msg.attach(part2)

mailsrv = smtplib.SMTP('localhost')
mailsrv.sendmail(sender, receiver, msg.as_string())
mailsrv.quit()

【问题讨论】:

    标签: python email


    【解决方案1】:

    我发现了这个问题。这是更新后的代码供您参考。

    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.MIMEImage import MIMEImage
    
    msg = MIMEMultipart('related')
    msg['Subject'] = "My text dated %s" % (today)
    msg['From'] = sender
    msg['To'] = receiver
    
    html = """\
    <html>
      <head></head>
        <body>
          <img src="cid:image1" alt="Logo" style="width:250px;height:50px;"><br>
           <p><h4 style="font-size:15px;">Some Text.</h4></p>           
        </body>
    </html>
    """
    # Record the MIME types of text/html.
    part2 = MIMEText(html, 'html')
    
    # Attach parts into message container.
    msg.attach(part2)
    
    # This example assumes the image is in the current directory
    fp = open('logo.png', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    
    # Define the image's ID as referenced above
    msgImage.add_header('Content-ID', '<image1>')
    msg.attach(msgImage)
    
    # Send the message via local SMTP server.
    mailsrv = smtplib.SMTP('localhost')
    mailsrv.sendmail(sender, receiver, msg.as_string())
    mailsrv.quit()
    

    【讨论】:

    • 效果很好.. 但对于最近的 python 3 版本,MIMEIMAGE 位于from email.mime.image import MIMEImage
    • 此答案还正确嵌入了图像以供使用 iOS 邮件查看 - 我遇到了一个问题。谢谢。
    【解决方案2】:

    如果你想要一些简单的东西:

    from redmail import EmailSender
    email = EmailSender(host="smtp.myhost.com", port=1)
    
    email.send(
        sender="me@example.com",
        subject="Example email",
        receivers=["you@example.com"],
        html="""
            <h1>Hi, take a look at this image:</h1>
            {{ my_image }}
        """,
        body_images={"my_image": "path/to/image.png"}
    )
    

    图像使用 Jinja 嵌入到 HTML 中,它会自动创建 img 标签。如果您喜欢这些格式,也可以直接将bytesmatplotlib.FigurePIL.Image.Image 传递给body_images

    如果您想对图像的属性进行更多控制,例如更改宽度和高度:

    email.send(
        sender="me@example.com",
        subject="Example email",
        receivers=["you@example.com"],
        html="""
            <h1>Hi, take a look at this image:</h1>
            <img src="{{ my_image.src }}" width=500 height=350>
        """,
        body_images={"my_image": "path/to/image.png"}
    )
    

    PyPI点安装它:

    pip install redmail
    

    这个库可以做很多事情:包括来自各种表单的附件、美化表格并将其嵌入 HTML 正文、从磁盘加载模板、参数化等。它也经过了很好的测试(100% 的测试覆盖率)并记录在案。我是图书馆的作者,对自我推销感到抱歉。我认为它很棒,值得分享。

    文档:https://red-mail.readthedocs.io/en/latest/index.html

    源码:https://github.com/Miksus/red-mail

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 2012-12-08
      • 2012-07-29
      • 1970-01-01
      相关资源
      最近更新 更多