尝试使用python smtplib发送电子邮件时,图像源属性在html部分中被加密/乱码 [英] image source attribute is getting encrypted/messed up in the html part while trying to send an email using python smtplib

查看:52
本文介绍了尝试使用python smtplib发送电子邮件时,图像源属性在html部分中被加密/乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 python smtplib 发送电子邮件.这是我当前正在使用的代码:

I'm trying to send an email using python and smtplib. Here's the code i'm currently using:

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部分中的图片标记.发送图像之前,图像标签的来源属性为:

Now it's sending my email off fine, except the image tag in the html part. The source attribute of the image tag before sending the image is:

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

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

But when I receive the email in an email client, the source of the image tag has been "encrypted/encoded/messed up" and therefore the image doesn't show 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 类有关.

Not sure whthere it has something to do with the SMTP_SSL class that I'm using.

有人能想到任何解决方案吗?任何帮助和/或建议将不胜感激.

Can anyone think of any solution? Any help and/or suggestions would be greatly appreciated.

推荐答案

好吧,好吧,我看到不同的人使用了Google驱动器提供的可共享网址的不同版本.在我看来,人们正在使用不一致的模式来使Google Drive的链接正常工作!有多种解决方案可以使Google驱动器正常工作,但是它们要么过时,要么不一致.

Ok after kicking it around I saw different people using different versions of the sharable url provided by google drive. It seems to me that people are using inconsistent patterns to get the google drive's link to work! There are different solutions to get google drive to work but they're all either obsolete or inconsistent.

我可以使用GOOGLE DRIVE解决方案轻松退出!

作为以后的参考,这是我所做的工作:

For future reference, here's what I've done as a work around:

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

I uploaded all of those images to my website server and simply used their links as the source attributes of the html image tag and they work perfectly fine across all of the standard email clients.

因此,我没有使用 google drive 链接,而是在远程服务器上使用了自己的网站链接,并且它们起作用了!

So instead of using google drive links I used my own website links on a remote server and they worked!

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

这篇关于尝试使用python smtplib发送电子邮件时,图像源属性在html部分中被加密/乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆