Home > database >  Policy Attribute error when sending email by SMTP
Policy Attribute error when sending email by SMTP

Time:07-15

I am trying to send an email with images in the body of the email.

Below is what I have done so far:

from email.encoders import encode_base64
from email.mime.base import MIMEBase
from PIL import Image
from io import BytesIO
from smtplib import SMTPException
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib


msg = MIMEMultipart("related")
msg['Subject'] = subject #time
msg['From'] = from_addr
msg['To'] = to_addr

html_output = "your html here"

msg.attach(MIMEText(html_output, "html"))

#plots is a dictionary of images
if plots:
    for image_name, image_location in plots.items():
        img = Image.open(BytesIO(image_location))
        msg.attach(img)
        
        
smtplib.SMTP(host).sendmail(from_addr, to_addr, msg.as_string())

I get an error saying ```enter image description here ````

What am I doing incorrectly?

CodePudding user response:

You can customize your email in the html body already importing your image in <img src="image.jpg">

CodePudding user response:

You can send beautiful messages with html and css, changing color and placing fonts. I hope this little example has clarified your doubts.

import smtplib, socket, email.message


msg_body = """
<div dir="ltr">
    <h1>Image</h1>
    <img src="image.jpeg">
</div>
"""


def send_email(server_name, passwd):
    try:
        msg = email.message.Message()
        msg['Subject'] = f"           
  • Related