Vinsmoke Mau
Vinsmoke Mau

Reputation: 347

Send images by email

I want to send images by email in a html but when I see the mail the image is broken this is part of my code:

data = {
        'image_url': cms.image.url,
       }
subject = 'Recibo de Pago - Shamosh Palombo'
email_body = render_to_string(
    'mails/supplier_receipt_html.html',
    {'data': data, }
)
msg = email_body
headers = {'Reply-To': "[email protected]"}
TO = '[email protected]'
mail = EmailMessage(subject, msg, '[email protected]', [TO], headers=headers)
mail.content_subtype = "html"
mail.send()

And in the template i code like this:

<img src="data.image_url" style="width:200px">

Upvotes: 1

Views: 894

Answers (2)

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11665

To embed the images in the email follow below code. I just added basic code snippet. Please import required imports.

views.py

from email.mime.image import MIMEImage

mail = EmailMessage(subject, msg, '[email protected]', [TO], headers=headers)
image = MIMEImage(cms.image.open().read())
image.add_header('Content-ID', '<embed_image>')
mail.attach(image)

template.html

<img src="cid:embed_image" style="width:200px">

Upvotes: 2

N. Ivanov
N. Ivanov

Reputation: 1823

I believe you would want to have

{{ data.image_url }}

in your template.

Example:

<img src="{{ data.image_url }}" style="width:200px">

I hope this helps!

Upvotes: 1

Related Questions