Tuesday, December 17, 2019

Embed an image in email Django

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 = ['abc@gmail.com']
    from_email = 'abc@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
    </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")

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0ebc6368-bc16-4d6b-9f4b-77af1f5fe377%40googlegroups.com.

No comments:

Post a Comment