【问题标题】:Embed both images and text inside email body在电子邮件正文中嵌入图像和文本
【发布时间】:2020-10-01 14:57:17
【问题描述】:

我是 python 的新手,我想构建一些东西来使用 python smtp 发送电子邮件,我想 还从全局路径嵌入图像,同时我还在 html 中传递参数

到目前为止,这是成功的。发送电子邮件。但图像没有嵌入邮件正文中,而是作为单独的附件发送我如何将所有内容嵌入一个正文中

msg = MIMEMultipart('related')
msg['Subject'] = "My text dated"
msg['From'] = MY_ADDRESS
msg['To'] = MY_ADDRESS

email = "someome@gmail.com"
name = "someome"

这是我试图传递 thress 参数以进行格式化的 html 标记

image_cid = make_msgid()

html = """\
<html>
  <head></head>
    <body>
      <img src="cid:{image_cid}" alt="Logo" style="width:250px;height:50px;"><br>
       <p><h4 style="font-size:15px;">email.{email}</h4></p>
      <p><h4 style="font-size:15px;">Name.{name}</h4></p>            
    </body>
</html>
""".format(image_cid = image_cid[1:-1] ,email =email,name = name )
# Record the MIME types of text/html.

from glob import glob 
list_of_images = glob('*.jpg')

这里循环遍历当前目录中所有格式的 jpg 图像

for filename in list_of_images:
fp = open(filename, 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(filename))
msg_img.add_header('Content-Disposition', 'inline', filename=filename)
msg.attach(msg_img)


part2 = MIMEText(html, 'html')

# Attach parts into message container.
msg.attach(part2)


# Send the message via local SMTP server.
s = smtplib.SMTP(host='rtrt.rty.com', port=567)
s.starttls()
s.login(USERNAME, PASSWORD)
s.send_message(msg)
del msg
        
    # Terminate the SMTP session and close the connection
s.quit()

【问题讨论】:

    标签: python python-3.x python-2.7


    【解决方案1】:

    我已经设法将文本和图像嵌入到一个图像中,现在我想嵌入多个图像,但只获得最后一个图像

    msg['Subject'] = "My text dated"
    msg['From'] = MY_ADDRESS
    msg['To'] = MY_ADDRESS
    
    
    # set the plain text body
    msg.set_content('This is a plain text body.')
    
    # now create a Content-ID for the image
    image_cid = make_msgid()
    # if `domain` argument isn't provided, it will 
    # use your computer's name
    email = "someome@gmail.com"
    name = "someome"
    # set an alternative html body
    msg.add_alternative("""\
    <html>
        <body>
            <p>This is an HTML body.<br>
               It also has an image.
             <p><h4 style="font-size:15px;">email{email}</h4></p>
             <p><h4 style="font-size:15px;">Name{name}</h4></p> 
            </p>
            <img src="cid:{image_cid}"> 
        </body>
    </html>
    """.format(email="someome@gmail.com",name=name,image_cid=image_cid[1:-1]), subtype='html')
    # image_cid looks like <long.random.number@xyz.com>
    # to use it as the img src, we don't need `<` or `>`
    # so we use [1:-1] to strip them off
    
        
        
    from glob import glob 
    list_of_images = glob('*.jpg')
            
    for filename in list_of_images: 
    # now open the image and attach it to the email
     with open(filename, 'rb') as img:
        # know the Content-Type of the image
        maintype, subtype = mimetypes.guess_type(img.name)[0].split('/')
        # attach it
        msg.get_payload()[1].add_related(img.read(), 
                                             maintype=maintype, 
                                             subtype=subtype, 
                                             cid=image_cid)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-16
      • 2018-07-05
      • 2016-09-07
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 2019-08-29
      相关资源
      最近更新 更多