2016-11-02 71 views
0

創建HTML郵件我想用Python語言編寫/ Django中包含這些零部件一個HTML郵件:與內嵌圖像和PDF附件

  • HTML鏈接到logo.png
  • logo.png應內嵌顯示(不作爲附件)郵件用戶代理
  • info.pdf應該顯示爲附件
  • 如果郵件用戶代理無法顯示HTML,應顯示的文本。

我跟着this博客文章。

結果:

  • 的HTML和內嵌形象工程
  • 但訪問3D文件被像內聯logo.png處理,有些郵件用戶代理沒有表現出來:-(?

如何在一個郵件在Python創建兩種方式(下載(訪問3D)和內聯(logo.png))

+0

這是否幫助? :http://stackoverflow.com/a/20717538/2286762 – Kishan

+0

@soupboy不,它沒有幫助。您引用的問題只能解決附加文件的一種方法。 – guettli

+0

但你可以實現的內嵌圖像部分是正確的?你可以在你面臨問題的地方顯示你的代碼嗎? – Kishan

回答

7

我反向設計,這種結構被用於實踐:

+-------------------------------------------------------+ 
| multipart/mixed          | 
|              | 
| +-------------------------------------------------+ | 
| | multipart/related        | | 
| |             | | 
| | +-------------------------------------------+ | | 
| | | multipart/alternative      | | | 
| | |           | | | 
| | | +-------------------------------------+ | | | 
| | | | text can contain [cid:logo.png]  | | | | 
| | | +-------------------------------------+ | | | 
| | |           | | | 
| | | +-------------------------------------+ | | | 
| | | | html can contain src="cid:logo.png" | | | | 
| | | +-------------------------------------+ | | | 
| | |           | | | 
| | +-------------------------------------------+ | | 
| |             | | 
| | +-------------------------------------------+ | | 
| | | image logo.png "inline" attachement  | | | 
| | +-------------------------------------------+ | | 
| |             | | 
| +-------------------------------------------------+ | 
|              | 
| +-------------------------------------------------+ | 
| | pdf ("download" attachment, not inline)   | | 
| +-------------------------------------------------+ | 
|              | 
+-------------------------------------------------------+ 

不幸的是我只找到這個複雜的解決方案:

from django.core.mail.message import EmailMessage 


def create_email(subject='', body='', from_email=None, to=None, bcc=None, 
       connection=None, attachments=[], headers=None, 
       cc=None, reply_to=None, html_body='', html_inline_attachments=[]): 
    message = _create_email(subject=subject, body=body, from_email=from_email, to=to, bcc=bcc, 
          connection=connection, headers=headers, cc=cc, reply_to=reply_to, 
          html_body=html_body, html_inline_attachments=html_inline_attachments) 

    for attachment in attachments: 
     if isinstance(attachment, basestring): 
      message.attach_file(attachment) 
      continue 
     message.attach(attachment) 

    return message 


def _create_email(subject='', body='', from_email=None, to=None, bcc=None, 
        connection=None, headers=None, 
        cc=None, reply_to=None, html_body='', html_inline_attachments=[]): 
    if not (body or html_body): 
     raise ValueError('Missing body or html_body!') 

    for address, type, name in [ 
     (from_email, basestring, 'from_email'), 
     (to, list, 'to'), 
     (cc, list, 'cc'), 
     (bcc, list, 'bcc')]: 
     if address and not isinstance(address, type): 
      raise ValueError('"{}" must be a list! ({})'.format(name, address)) 

    if body and not html_body: 
     if html_inline_attachments: 
      raise ValueError('"html_body" is missing!') 
     return EmailMessage(subject=subject, body=body, from_email=from_email, to=to, bcc=bcc, 
          connection=connection, headers=headers, cc=cc, 
          reply_to=reply_to) 

    if not body: 
     body = html_to_text(html_body) 
    msg = EmailMessage(subject=subject, from_email=from_email, to=to, bcc=bcc, 
         connection=connection, headers=headers, cc=cc, reply_to=reply_to) 
    alternative = MIMEMultipart('alternative') 
    alternative.attach(MIMEText(body.encode('utf8'), 'plain', 'utf8')) 
    alternative.attach(MIMEText(html_body.encode('utf8'), 'html', 'utf8')) 
    related = MIMEMultipart('related') 
    related.attach(alternative) 
    for inline in html_inline_attachments: 
     inline_attachment = msg._create_attachment(os.path.basename(inline), open(inline).read()) 
     inline_attachment.add_header('Content-Disposition', 'inline') 
     inline_attachment.add_header('Content-ID', os.path.basename(inline)) 
     related.attach(inline_attachment) 
    msg.attach(related) 
    return msg 

如果有人有一個簡單的解決方案,請讓我知道:-)

+0

這種圖形表示幫助我更好地理解結構。感謝那。當使用SMIME時,我發現_multipart/mixed_後面跟着_application/pkcs7-signature_ part和_multipart/signed_這兩個參數。 類似於:_multipart/signed [multipart/mixed,application/pkcs7-signature] _ – ramtech