簡體   English   中英

在Rails 3中附加文件 - mimepart Mailer

[英]Attaching file in Rails 3 - mimepart Mailer

我正在使用Rails發送電子郵件,如下所示:

attachments["ticket.pdf"] = WickedPdf.new.pdf_from_string(render_to_body(pdf: "ticket", template: 'template' }))

這適用於Gmail,但在其他幾個客戶端中,附加的pdf不會出現。

看一下源代碼,我意識到這是mimeparts的一個問題。

我很難在雅虎和其他幾個電子郵件客戶端(例如Thunderbird)制作附件文件,如果我分析發送的電子郵件的源代碼我會發現:mimepart對於文件的每個部分都是相同的..層次結構(據我所知)必須是這樣的:

multipart/mixed
multipart/alternative
multipart/related
text/html
image/png (e.g. for an inline attachment; pdf would be another good example)
text/plain
application/zip (e.g for an attachment--not inline)

這是我從https://github.com/jcoleman/mail_alternatives_with_attachments中提取的

在這里,是電子郵件源(只是內容的塊,以便更好地理解):

Delivered-To: someone@yahoo.com
Received: by 10.194.54.135 with SMTP id j7csp144091wjp;
        Fri, 28 Sep 2012 10:55:03 -0700 (PDT)
Received: by 10.224.187.146 with SMTP id cw18mr18768052qab.35.1348854902751;
        Fri, 28 Sep 2012 10:55:02 -0700 (PDT)
Return-Path: <somewhare@company.com>
Received: from mail-qa0-f46.company.com (mail-qa0-f46.company.com [209.85.216.46])
        by mx.company.com with ESMTPS id c6si13006455qao.34.2012.09.28.10.55.02
        (version=TLSv1/SSLv3 cipher=OTHER);
        Fri, 28 Sep 2012 10:55:02 -0700 (PDT)
Received-SPF: pass (company.com: domain of somewhere@company.com designates 209.85.216.46 as permitted sender) client-ip=xx.xxx.xxx.xxx;
Authentication-Results: mx.company.com; spf=pass (company.com: domain of somwhere@company.com designates 209.85.216.46 as permitted sender) smtp.mail=somewhere@company.com; dkim=pass header.i=@company.com
Received: by mail-qa0-f46.company.com with SMTP id c26so105866qad.5
        for <someone@company.com>; Fri, 28 Sep 2012 10:55:02 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=company.com; s=20120113;
        h=date:from:reply-to:to:message-id:subject:mime-version:content-type
         :content-transfer-encoding;
        bh=nETVpMomUq0PkrNf9ScGjdaBPc5tYF0m1Rphx5FNS4c=;
        b=I1ICSRRK10cdrqXxkNWmK5cLvXOqFpxXmzxNMlIVa8jh+4U1GwqwI1GZF7BRXh5FAU
         wCyTzXfSkBrmn2cVGwUUiNEh956KT2ssWx37cQIb915lSp6rdIqflgQcF9yKwmtJoxDL
         qO+nEVse7+Azi828zC7D6VV7ebAbmnr006KmppkH9bxILk+syiPKLvp4rZXAMIp0IFW8
         nYZ2jY+n/ryduTSl0qDaBDCzVrky2eNwpqfJYizoYWIhxFuDmUWwaPpKzfWoxzT4b0+P
         Mp/Ugv6iN1QyETJWbOfX4lpGezFzcaV7reZaBXLzcqUe1dc8elxFfWncsE0vozmFaj8m
         z37Q==
Received: by 10.224.58.134 with SMTP id g6mr18690371qah.40.1348854902269;
        Fri, 28 Sep 2012 10:55:02 -0700 (PDT)
Return-Path: <somwhere@company.com>
Received: from localhost.localdomain (company.com. [xx.xxx.xxx.xx])
        by mx.company.com with ESMTPS id ck11sm13357478qab.17.2012.09.28.10.54.56
        (version=TLSv1/SSLv3 cipher=OTHER);
        Fri, 28 Sep 2012 10:55:00 -0700 (PDT)
Return-Path: <bounces1@somewhere.com>
Date: Fri, 28 Sep 2012 15:09:56 -0300
From: somewhere <somewhere@company.com>
Reply-To: someone@company.com
To: someone@company.com
Message-ID: <5065e7f4b41bf_a9e43fdbdd45a7601574c@megara.local.mail>
Subject: algun subject
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9
Date: Fri, 28 Sep 2012 15:09:56 -0300
Mime-Version: 1.0
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Content-ID: <5065e7f4b0ead_a9e43fdbdd45a7601546@megara.local.mail>

********
texto cualquiera
********



----==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9
Date: Fri, 28 Sep 2012 15:09:56 -0300
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Content-ID: <5065e7f4b1d7d_a9e43fdbdd45a76015576@megara.local.mail>

<html xmlns=3D'http://www.w3.org/1999/xhtml'>
  <html xml:lang=3D'en' xmlns=3D'http://www.w3.org/1999/xhtml'>
    <head>
      <meta content=3D'text/html; charset=3Dutf-8' http-equiv=3D'Content-=
Type'>
      <title>somewhere</title>
      <link href=3D'/assets/ev_classic_mailer.css' rel=3D'stylesheet' typ=
e=3D'text/css'>
      <link href=3D'/assets/mailer_colors.css' rel=3D'stylesheet' type=3D=
'text/css'>
    </head>
    <body>
      <table class=3D'mail-container-table'>
        <tr>
          <td class=3D'decoration' colspan=3D'3'></td>
        </tr>
        <tr>
          <th background=3D'https://here.company.com/ass=
ets/mail/ev_classic_hd.png' class=3D'bcolor12 border header-01' colspan=3D=
'3'>
            <h1>cogotudo</h1>
          </th>
        </tr>
      </table>
    </body>
  </html>
</html>

----==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9
Date: Fri, 28 Sep 2012 15:09:56 -0300
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Content-ID: <5065e7f4b2ede_a9e43fdbdd45a76015666@megara.local.mail>

<html xmlns=3D"http://www.w3.org/1999/xhtml" style=3D"outline: 0; font-we=
ight: inherit; font-style: inherit; font-size: 100%; font-family: inherit=
; vertical-align: baseline; margin: 0; padding: 0; border: 0;">
  <html xml:lang=3D"en" xmlns=3D"http://www.w3.org/1999/xhtml" style=3D"o=
utline: 0; font-weight: inherit; font-style: inherit; font-size: 100%; fo=
nt-family: inherit; vertical-align: baseline; margin: 0; padding: 0; bord=
er: 0;">
    <head>
      <meta content=3D'text/html; charset=3Dutf-8' http-equiv=3D'Content-=
Type'>
      <title>somewhere</title>
      <link href=3D'/assets/ev_classic_mailer.css' rel=3D'stylesheet' typ=
e=3D'text/css'>
      <link href=3D'/assets/mailer_colors.css' rel=3D'stylesheet' type=3D=
'text/css'>
    </head>
    <body style=3D"outline: 0; font-weight: inherit; font-style: inherit;=
</body>
  </html>
</html>


----==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9
Date: Fri, 28 Sep 2012 15:09:56 -0300
Mime-Version: 1.0
Content-Type: application/pdf;
 charset=UTF-8;
 filename=ticket.pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename=ticket.pdf
Content-ID: <5065e7f43b81b_a9e43fdbdd45a760150c9@megara.local.mail>

JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/KQovQ3JlYXRvciAo/v8p
Ci9Qcm9kdWNlciAo/v8AUQB0ACAANAAuADgALgAyACAAXCgAQwBcKQAgADIA
MAAxADEAIABOAG8AawBpAGEAIABDAG8AcgBwAG8AcgBhAHQAaQBvAG4AIABh
AG4AZAAvAG8AcgAgAGkAdABzACAAcwB1AGIAcwBpAGQAaQBhAHIAeQBcKAAt
AGkAZQBzAFwpKQovQ3JlYXRpb25EYXRlIChEOjIwMTIwOTI4MTgwOTU1KQo+
PgplbmRvYmoKMiAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMyAw
IFIKPj4KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL0V4dEdTdGF0ZQovU0Eg
dHJ1ZQovU00gMC4wMgovY2EgMS4wCi9DQSAxLjAKL0FJUyBmYWxzZQovU01h
c2sgL05vbmU+PgplbmRvYmoKNSAwIG9iagpbL1BhdHRlcm4gL0RldmljZVJH

----==_mimepart_5065e7f4af345_a9e43fdbdd45a760153b9--

你所有的mimepart邊界都是一樣的......它們都是'替代'邊界。

- == _ mimepart_5065e7f4af345_a9e43fdbdd45a760153b9

對於包含HTML,純文本和附件的電子郵件,您需要通過正確地將邊界放在正確的位置來聲明並正確封裝單獨的部分。 了解其外觀的最簡單方法是在電子郵件客戶端中手動創建包含所需“所有”功能的電子郵件(HTML +純文本+附件),然后查看源代碼並記下mimepart的方式界定了邊界。

電子郵件源的每個“部分”實際上聲明邊界的樣子,然后在每個部分的末尾有一條或多條指示部分結束的邊界線。 零件可以並且應該封裝在其他零件中。 這很復雜,但是在你的例子中,你只有一個mimepart聲明並且當你應該有更多時使用(正如你在層次結構中指出的那樣)。

現在關於何時/何時出錯,我們必須看到您用於從頭到尾生成電子郵件的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM