我有以下功能(Django)来发送邀请:
def send_invitation(self, request):
t = loader.get_template('email/invitation.html')
html_content = t.render(Context(context))
message = EmailMessage('Hi there', html_content, 'to@some.com',
[self.profile_email],
headers={'Reply-To': 'Online <online@online.nl>'})
message.content_subtype = 'html'
localize_html_email_images(message)
message.send()
我正在使用一个函数将本地提供的链接图像替换为附加图像。
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)
一切都很完美。但不知何故,Gmail 并没有像 Hotmail 和 Outlook 那样立即显示图像。
当我检查电子邮件的来源时,它确实添加了正确的标题:
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
如何让 Gmail 在打开电子邮件时立即显示图像,就像 Hotmail 和 Outlook 一样?
PS。我查看了很多关于内联图片的主题,但它仍然无法在 Gmail 中使用