3

招待状を送信するための次の関数(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

HotmailやOutlookと同じように、メールメッセージを開いたときにGmailに画像をすぐに表示させるにはどうすればよいですか。

PS。インライン画像に関する多くのトピックを調べましたが、Gmailではまだ機能しません

4

1 に答える 1

1

セキュリティ上の理由から、Gmail は画像の自動読み込みをサポートしていません。電子メールに含める画像の量を制限することを目指してくださいspam-trapped

電子メール クライアントは通常、画像の表示を許可します (特に、画像が base-64 などでエンコードされている場合)。これは、Gmail などのブラウザ側の電子メールには当てはまりません。画像の表示にかなり関心がある場合は、base64エンコーディングを使用して埋め込むことができます。これにより、電子メールのファイルサイズが大幅に増加します (参照ではなく値で含めます) ので、控えめに使用してください (そうしないと、スパム トラッパーによってフィルター処理されます)。

参考http ://docs.python.org/library/base64.html

楽しんで頑張ってください!

于 2012-07-16T15:50:37.023 に答える