Embed an image in email Django

411 views
Skip to first unread message

Santhosh sridhar

unread,
Dec 17, 2019, 6:36:29 AM12/17/19
to Django users
Hi,
I am using Django's Email message module to trigger an email with an embed image from my static folder. Could someone help? Below is the code that I have used.

Code :

    recipient_list = ['a...@gmail.com']
    from_email = 'a...@gmail.com'
    message = ''
    path = "{% static 'images/Welcome_image.jpg' %}" //Using this image in the below html
    message += "<table border='1' cellpadding='1' cellspacing='0' width='800'>\
    <tbody>\
    <tr>\
    <td height='506'>\
    <table border='0' cellpadding='0' cellspacing='0' width='600'>\
    <tbody>\
    <tr>\
    <td valign='top'>\
    <img height='190' src=%s width='800'  tabindex='0'>\    //Using this image here but it is not rendered properly.
    </td>\
    </tr>\
    <tr>\
    <td height='306' valign='top'>\
    <table cellpadding='0' cellspacing='20' width='800'>\
    <tbody>\
    <tr>\
    <td align='left' height='804' style='font-family:arial,helvetica,sans-serif;font-size:13px' valign='top'>Hi User,<br><br>\
    Welcome to the world.\
    </td>\
    </tr>\
    </tbody>\
    </table>\
    </td>\
    </tr>\
    </tbody>\
    </table>\
    </td>\
    </tr>\
    </tbody>\
    </table>"%(path)

    subject = "Welcome to the place!"
    try:
        msg = EmailMessage(subject, message, from_email, recipient_list)
        msg.content_subtype = "html"  # Main content is now text/html
        if any(recipient_list):
            msg.send()
    except Exception:
        print("Exception while sending mail")

Santhosh sridhar

unread,
Dec 17, 2019, 6:37:04 AM12/17/19
to Django users
Hi,
I am using Django's EmailMessage module to trigger an email with embedded image from the static directory path. Somehow the image is not rendering properly in the triggered email. Please find the below code and appreciating your help.

Thanks,
Santhosh

Code:
    recipient_list = ['a...@gmail.com']
    from_email = 'a...@gmail.com'
    message = ''
    path = "{% static 'images/Welcome_image.jpg' %}" //Using this image in the below html
    message += "<table border='1' cellpadding='1' cellspacing='0' width='800'>\
    <tbody>\
    <tr>\
    <td height='506'>\
    <table border='0' cellpadding='0' cellspacing='0' width='600'>\
    <tbody>\
    <tr>\
    <td valign='top'>\
    <img height='190' src=%s width='800'  tabindex='0'>\  #Using this static path here

Giovanni Natale

unread,
Dec 17, 2019, 8:21:58 AM12/17/19
to Django users
The problem with that is that the url of the image does not contain the domain of your website. This is ok when you are using a browser to render a webpage but there is no way for an email client to understand the domain of the url if you do not include it explicitly.

So, the image url should be something like "http://www.yourwebsite.com/images/Welcome_image.png".

I suggest you pass both the domain and the protocol ('http' or 'https') as a context variables to the template so the url will work both in development and production.

It also looks you are including the html within the python code. Better putting that in a template html file....

El dimarts, 17 desembre de 2019 12:36:29 UTC+1, Santhosh sridhar va escriure:
Reply all
Reply to author
Forward
0 new messages