- Author:
- [email protected]
- Posted:
- January 24, 2012
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
The code here will take an EmailMessage from django.core.mail and replace the sourcing of any images served by the application with attached image content. Note: This expects a valid closing tag of /> on img elements, it will not properly handle filenames with ' characters in it, and it does not handle if invalid image sources are listed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import string
import random
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
def localize_html_email_images(message):
"""Replace linked images served locally with attached images"""
import re, os.path
from email.MIMEImage import MIMEImage
from django.conf import settings
image_pattern = """<img\s*.*src=['"](?P<img_src>%s[^'"]*)['"].*\/>""" % settings.MEDIA_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.MEDIA_ROOT, image_match.replace(settings.MEDIA_URL, ''))
img_data = open(on_disk_path, 'rb').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(matchobj.group('img_src'), 'cid:%s' % added_images[matchobj.group('img_src')])
if added_images:
message.body = re.sub(image_pattern, repl, message.body)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Hi there, Can you put or send me an example how to use this code? What do i throw into 'localize_html_email_images(???)'. I guess the rendered html template but i'm not sure.
#
Here is how I have used it in my code:
#
Thanks for helping me out. This helped me allot!
#
Images are blocked in Gmail. Do yo have the same issue. If not, what can I do so that Gmail shows images immediately??
#
Please login first before commenting.