nelsonvarela
nelsonvarela

Reputation: 2370

Gmail not showing inline images

I have the following function (Django) to send out an invitation:

def send_invitation(self, request): 
    t = loader.get_template('email/invitation.html')
    html_content = t.render(Context(context))
    message = EmailMessage('Hi there', html_content, '[email protected]',
            [self.profile_email],
            headers={'Reply-To': 'Online <[email protected]>'})
    message.content_subtype = 'html'
    localize_html_email_images(message)
    message.send()

I'm using a function to replace linked images served locally with attached images.

def localize_html_email_images(message):
    import re, os.path
    from django.conf import settings

    image_pattern = """<IMG\s*.*src=['"](?P<img_src>%s[^'"]*)['"].*\/>""" % settings.STATIC_URL

    image_matches = re.findall(image_pattern, message.body)
    added_images = {}

    for image_match in image_matches:
        if image_match not in added_images:
            img_content_cid = id_generator()
            on_disk_path = os.path.join(settings.STATIC_ROOT, image_match.replace(settings.STATIC_URL, ''))
            img_data = open(on_disk_path, 'r').read()
            img = MIMEImage(img_data)
            img.add_header('Content-ID', '<%s>' % img_content_cid)
            img.add_header('Content-Disposition', 'inline')
            message.attach(img)

            added_images[image_match] = img_content_cid

    def repl(matchobj):
        x = matchobj.group('img_src')
        y = 'cid:%s' % str(added_images[matchobj.group('img_src')])

        return matchobj.group(0).replace(x, y)

    if added_images:
        message.body = re.sub(image_pattern, repl, message.body)

All is working perfect. But somehow Gmail is not showing the images immediately while Hotmail and Outlook does.

When I check the source of the email it does add the right headers:

Content-Type: multipart/mixed; boundary="===============1839307569=="
#stuff
<IMG style="DISPLAY: block" border=0 alt="" src="cid:A023ZF" width=600 height=20 />
#stuff
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <A023ZF>
Content-Disposition: inline

What can I do to make Gmail show images immediately when opening the email message just like Hotmail and Outlook?

PS. I looked at allot of topics about inline images but it still not working in Gmail

Upvotes: 3

Views: 2353

Answers (1)

Daniel Li
Daniel Li

Reputation: 15389

Gmail will not support automatic image loading simply due to security reasons. Aim to limit the amount of images included in your e-mail as they can often lead to being spam-trapped.

E-mail clients will typically allow images to show (especially if they're encoded in something like base-64). This is simply not the case with browser-side e-mail such as Gmail. If you are considerably concerned with displaying an image, you may embed it using base64 encoding. This will increase the filesize of the e-mail dramatically (you are including it by value, not by reference) so use this conservatively (or you'll be filtered by spam trappers).

Reference: http://docs.python.org/library/base64.html

Enjoy and good luck!

Upvotes: 1

Related Questions