2012-07-16 2 views
3

표시되지 나는 다음과 같은 기능 (장고) 초대장을 보낼 수 있습니다 내가 연결된 이미지가 첨부 된 이미지 로컬로 제공 대체 기능을 사용하고Gmail은 인라인 이미지

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() 

.

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) 

모두 완벽합니다. 그러나 Hotmail과 Outlook이하는 동안 어떻게 든 Gmail은 이미지를 즉시 표시하지 않습니다.

내가 이메일의 출처를 확인하는 경우가 바로 헤더를 추가 않습니다

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의 쇼 이미지를 만들기 위해 무엇을 할 수 있는가?

추신. 인라인 이미지에 대한 주제를 살펴 보았지만 여전히 Gmail에서는 작동하지 않습니다.

답변

1

Gmail은 보안상의 이유로 자동 이미지로드를 지원하지 않습니다. spam-trapped으로 이어질 수 있으므로 전자 메일에 포함 된 이미지의 양을 제한하십시오.

전자 메일 클라이언트는 일반적으로 이미지를 표시 할 수 있습니다 (특히 base-64와 같이 인코딩 된 경우). Gmail과 같은 브라우저 측 전자 메일에서는 그렇지 않습니다. 이미지 표시와 관련이있는 경우 base64 인코딩을 사용하여 이미지를 퍼갈 수 있습니다. 이렇게하면 전자 메일의 파일 크기가 크게 늘어납니다 (참조가 아닌 값으로 포함 함). 따라서 이것을 보수적으로 사용하십시오 (또는 스팸 트래퍼로 필터링됩니다).

참조 : http://docs.python.org/library/base64.html

이 즐길 행운을 빕니다!

관련 문제