在尝试使用 python smtplib 发送电子邮件时,图像源属性在 html 部分中得到 encrypted/messed

image source attribute is getting encrypted/messed up in the html part while trying to send an email using python smtplib

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

import smtplib   
from email.message import EmailMessage  

msg = EmailMessage()
msg['Subject'] = 'Testing emails'
msg['From'] = 'some subject'
msg['To'] =  'contact0@gmail.com'
msg['Cc'] = 'contact1@outlook.com'
msg['Bcc'] = ['contact2@outlook.com','contact3@yahoo.com','contact4@gmail.com']

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">  

但是当我在电子邮件客户端中收到邮件时,图像标记的来源已“encrypted/encoded/messed up”,因此图像没有显示!!!!

<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 class 有关。

谁能想到什么解决办法?任何帮助 and/or 的建议将不胜感激。

好吧,我看到不同的人在使用 google 驱动器提供的可共享 url 的不同版本。在我看来,人们正在使用不一致的模式来使 google 驱动器的 link 正常工作!有不同的解决方案可以使 google 驱动器正常工作,但它们要么已过时,要么不一致。

所以我干脆停止使用 GOOGLE 驱动解决方案!

为了将来参考,以下是我所做的解决方法:

我将所有这些图像上传到我的网站服务器,并简单地将它们的 link 用作 html 图像标签的源属性,它们在所有标准电子邮件客户端上都能正常工作.

因此,我没有使用 google drive links,而是在 remote server 上使用了我自己的网站 links,他们成功了!

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