Attempting to send an email with HTML content including an image. For some reason I cannot figure out, the email is received with the image as an attachment and not embedded. I am using the following code:
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import globals
from templates.templates import *
class Mailer:
def __init__(self, recordid, lastname, firstname, emailaddress):
self.recordid = recordid
self.lastname = lastname
self.firstname = firstname
self.emailaddress = emailaddress
self.subject = ""
self.smtp_server = smtplib.SMTP('localhost')
self.message = MIMEMultipart('alternative')
def send(self):
self.smtp_server.ehlo()
self.smtp_server.sendmail(globals.MAIL_SENDER, self.emailaddress, self.message.as_string())
self.smtp_server.close()
def get_image_data(self):
with open("./templates/images/sovanotes_email.png", 'rb') as file:
img = MIMEImage(file.read())
img.add_header('Content-ID', '<logo>')
return img
def email_forgot_password(self):
self.subject = "Resetting Your Account Password"
resetlink = "https://zzz.com/support/pwreset/{}".format(self.recordid)
html = """\
<html>
<head></head>
<body>
<img style="display:block;width:100%;" src="cid:logo" alt="Logo"><br>
</body>
</html>
"""
self.message['Subject'] = self.subject
self.message['From'] = "someone@somewhewre.com"
self.message['To'] = self.emailaddress
self.message.attach(MIMEText(html,'html'))
self.message.attach(self.get_image_data())
self.send()
Any help would greatly be appreciated