尝试使用 python smtplib 发送电子邮件时,图像源属性在 html 部分被加密/弄乱

问题描述

我正在尝试使用 pythonsmtplib 发送电子邮件。这是我目前使用的代码

import smtplib   
from email.message import EmailMessage  

msg = EmailMessage()
msg['Subject'] = 'Testing emails'
msg['From'] = 'some subject'
msg['To'] =  '[email protected]'
msg['Cc'] = 'contact1@outlook.com'
msg['Bcc'] = ['contact2@outlook.com','[email protected]','[email protected]']

msg.set_content('Teseting emails using python! -  This is a simple text - fallback for the html content')

msg.add_alternative("""\
<!DOCTYPE html>
<head>
  <Meta charset="UTF-8">
  <Meta name="viewport" content="width=device-width,initial-scale=1.0">
  <title>Best way of sending emails!!!!</title>
  <style type="text/css">
    *,html,body {
      margin: 0 auto;
      padding: 0;
      Box-sizing: border-Box;
    }
    
    body {
      background-color: #e3e6de;
    }

    img {
      border-radius: 10px;
    }

    #main-container {
      max-width: 600px;
      margin: 10px auto;
      padding: 20px;
      background-color: #fff;
    }

    #logo {
      text-align: center;
      padding: 10px;
    }

    #title-subtitle {
      text-align: center;
      padding: 10px;
    }

    #title-subtitle h3,h5 {
      margin-bottom: 5px;
    }
<body>
  <div id="main-container">
    <div id="logo">
      <img src="http://drive.google.com/file/d/somehashes/view?usp=sharing" alt="" id="logo-image" height="80px" width="80px">
    </div>
    <hr>
    <div id="title-subtitle">
      <h1 id="newsletter-title">Some Title</h1>
      <h5 id="newsletter-subtitle">Some Subtitle</h5>
    </div>
</body>

</html>
""",subtype='html')   

with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
    smtp.login('my_emailaddress','my_password')
    smtp.send_message(msg)

现在它可以很好地发送我的电子邮件,除了 html 部分中的图像标签。发送图片图片标签的source属性为:

<img src="http://drive.google.com/file/d/somehashes/view?usp=sharing" alt="" id="logo-image" height="80px" width="80px">  

但是当我在电子邮件客户端中收到电子邮件时,图像标签的来源已被“加密/编码/弄乱”,因此图像没有显示!!!!

<img src="https://ecp.yusercontent.com/mail?url=http%3A%2F%2fdrive.google.com%2Ffile%2Fd%2somehashes%2Fview%3Fusp%3Dsharing&amp;t=1609538467&amp;ymreqid=f5ebbadb-3156-bcac-1c0f-4a000001bf00&amp;sig=tMlKSCZcW1UV_0mbIsW0SA--~D" alt="" id="yiv4388721595logo-image" width="80px" height="80px">  

不确定它是否与我使用的 SMTP_SSL 类有关。

谁能想到解决办法?任何帮助和/或建议将不胜感激。

解决方法

好吧,在我转过身之后,我看到不同的人使用了 google drive 提供的不同版本的可共享 url。在我看来,人们正在使用不一致的模式来使谷歌驱动器的链接正常工作!有不同的解决方案可以让谷歌驱动器工作,但它们都已经过时或不一致。

所以我干脆放弃使用 GOOGLE 驱动解决方案!

为了将来参考,以下是我所做的工作:

我将所有这些图片上传到我的网站服务器,并简单地使用它们的链接作为 html 图片标签的源属性,它们在所有标准电子邮件客户端上都运行良好。

因此,我没有使用 google drive 链接,而是在 remote server 上使用了我自己的网站链接,结果奏效了!

<img src="https://myownwebsite.com/path/to/images/logo.png" id="logo-image" height="80px" width="80px">