我不確定這是否是我的代碼ActionMailer、Mail甚至icalendar gem 的問題?
用戶注冊一個活動,他們會收到一封帶有 ical 附件的電子郵件:
# app/mailers/registration_mailer.rb
class RegistrationMailer < ApplicationMailer
helper MailerHelper
def created(registration)
...
cal = Icalendar::Calendar.new
cal.event do |e|
e.dtstart = @event.start_time
e.dtend = @event.end_time
e.organizer = 'mailto:[email protected]'
e.attendee = @recipient
e.location = @location.addr_one_liner
e.summary = @summary
e.description = @description
end
cal.append_custom_property('METHOD', 'REQUEST')
mail.attachments[@attachment_title] = { mime_type: 'text/calendar', content: cal.to_ical }
mail(to: @recipient.email, subject: "[20 Liters] You registered for a filter build on #{@event.mailer_time}")
end
...
end
我有文本和 HTML 視圖:
app/views/registration_mailer/created.text.erbapp/views/registration_mailer/created.html.erb
當我省略附件時,電子郵件的結構如下:
Header stuff...
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary="--==_mimepart_63358693571_1146901122e"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_63358693571_1146901122e
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email here]
----==_mimepart_63358693571_1146901122e
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email here]
----==_mimepart_63358693571_1146901122e--
當附件存在時,電子郵件的結構如下:
Header stuff...
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="--==_mimepart_6335c3388b140_114924286ed"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email here]
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email here]
----==_mimepart_6335c3388b140_114924286ed
Content-Type: multipart/alternative; boundary="--==_mimepart_6335c3389bc30_114924287a3"; charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_6335c3389bc30_114924287a3
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the text version of the email AGAIN]
----==_mimepart_6335c3389bc30_114924287a3
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit
[the HTML version of the email AGAIN]
----==_mimepart_6335c3389bc30_114924287a3--
----==_mimepart_6335c3388b140_114924286ed
Content-Type: text/calendar; charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=20Liters_filterbuild_20221011T0900.ical
Content-ID: <6335[email protected]>
[numbers and letters]
----==_mimepart_6335c3388b140_114924286ed--
突然是一棵奇怪的樹:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain
B. Content-Type: text/html
C. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar
Rails 的郵件預覽不會重現此問題,使用 Litmus 的電子郵件客戶端預覽也不會(因為它似乎洗掉了文本部分和附件),但我假設內容型別的變形結構不僅僅是特定于客戶端的渲染問題。
我認為這是來自 ActionMailer 下面的 Mail gem 奇怪地構造了內容型別,但我在這里有點超出我的深度。可能是ActionMailer,我真的不知道該怎么說。
我不是很精通這個,但我想我想要這個結構:
1. Content-Type: multipart/mixed
A. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
B. Content-Type: text/calendar
所以,兩個問題:
1.如果是我的代碼,我做錯了什么?
2.如果不是我的代碼,我可以強制我想要的結構嗎?
我一直在梳理 ActionMailer 和 Mail 代碼庫,但還沒有找到一種方法將我的電子郵件手動形成到這個級別。
uj5u.com熱心網友回復:
經過更多的挖掘,我責怪ActionMailer,盡管我仍然不確定為什么 text 和 html 部分被添加了兩次。
我特定用途的猴子補丁是讓 ActionMailer 和 Mail 構建mail物件,然后手動洗掉不需要的部分:
# app/mailers/registration_mailer.rb
...
mail.attachments[@attachment_title] = { mime_type: 'text/calendar', content: cal.to_ical }
mail(to: @recipient.email, subject: "[20 Liters] You registered for a filter build on #{@event.mailer_time}")
# PATCH: text and html parts are getting inserted in multipart/mixed (top level) as well as multipart/alternative (2nd level)
mail.parts.reject! { |part| !part.attachment? && !part.multipart? }
這僅適用于我的具體情況。如果mail為您創建的嵌套不同,則您的reject!陳述句將需要不同。
在我的例子中,mail構建這個結構:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain
B. Content-Type: text/html
C. Content-Type: multipart/alternative
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar
所以我進入第一級(A. - D.)并拒絕任何不是多部分且不是附件的部分:
1. Content-Type: multipart/mixed
A. Content-Type: text/plain <-- not multipart, not attachment = rejected
B. Content-Type: text/html <-- not multipart, not attachment = rejected
C. Content-Type: multipart/alternative <-- is multipart, not attachment = kept
i. Content-Type: text/plain
ii. Content-Type: text/html
D. Content-Type: text/calendar <-- not multipart, is attachment = kept
如果您遇到此問題,我建議您使用除錯器來檢查您的mail物件,特別關注parts. 請記住,它parts可以是深度嵌套的。
Mail::Part繼承Mail::Message其中有一些有用的方法可以用來確定訊息的“形狀”:
multipart?attachment?attachmentshas_attachments?boundarypartsall_parts
還有祝你好運。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/513313.html
標籤:轨道上的红宝石电子邮件日历动作邮件ruby-on-rails-6.1
