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

眉间皱痕 提交于 2021-01-29 11:12:33

问题


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)

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

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.


回答1:


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.

SO I SIMPLY QUIT USING GOOGLE DRIVE SOLUTION!

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

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.

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


来源:https://stackoverflow.com/questions/65534150/image-source-attribute-is-getting-encrypted-messed-up-in-the-html-part-while-try

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!