Best way to send photo inside mail Django

Viewed 27

What is the best way to attach my website logo inside activation mail? I tried to do something like this:

@lru_cache()
def logo_data():
    with open('templates/authentication/logo.png', 'rb') as f:
        logo_data = f.read()
    logo = MIMEImage(logo_data)
    logo.add_header('Content-ID', '<logo>')
    return logo

and then:

email=EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_FROM_USER, to=[user.email])
email.attach(logo_data())
email.content_subtype='html'
email.send()

In my html file it looks like this:

<img src="cid:logo" style="width: 242px; height: 88px;"/>

but it doesn't work. The photo is actually added to the mail in attachments, but the <img> tag is empty and looks just like this: enter image description here

What is wrong? I read also that attaching photos to email is not recommended because mail gets cause of that extra spam points. What is the best way to do this? Can I send my png logo without attaching it into attachments?

EDIT

I tried to add url (to my photo on google drive) like this <img scr="url"> but these properties disappear inside mail

1 Answers

You can use a CDN, like S3 or Google Cloud bucket to upload the image and add the url of the image in your <img src="URL"> tag.

Related