我使用Rails 4.2應用程式,我需要將郵件的PDF版本附加到郵件本身。我找到了一種方法來渲染并將渲染后的視圖轉換為 PDF,但在郵件中添加附件會導致無法發送其內容。 發送附件的代碼:
def failed_charge(bill) @monthly_bill = bill @organization = bill.organization @edit_account_url = edit_account_url(organization) I18n.with_locale(organization.locale) do @date = I18n.l Time.now.utc.to_date, format: : medium @display_period = [bill.period_start, bill.period_end]. .map { |t| I18n.l(t.to_date, format: :medium).strip } .join(' - ') 發票內容 = WickedPdf.new.pdf_from_string( render_to_string(template: 'billing_mailer/failed_charge'), { margin: {top: 0, bottom: 0, left: 0, right: 0}。 } ) File.open('invoice.pdf', 'wb') do |file| file.write invoice_content 結束 attachments['invoice.pdf'] = invoice_content mail( to: bill.billing_email, bcc: [INVOICES_EMAIL_FULL, SUPPORT_EMAIL_FULL], 主題。I18n.t('billing_mailer.subject_failed_charge')。 ) 結束 結束結果郵件:
日期。Wed, 08 Sep 2021 19:13:16 0400
來自。GlassFrog Billing
收件人:[email protected]
郵件地址:Message-ID:
主題。在向您的信用卡收取GlassFrog費用時出現了問題。
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="--==_mimepart_6138d30c9a0db_7215116ac5527d";
charset=UTF-8
Content-Transfer-Encoding: 7bit
不發送附件的代碼:
def failed_charge(bill)
@monthly_bill = bill
@organization = bill.organization
@edit_account_url = edit_account_url(organization)
I18n.with_locale(organization.locale) do
@date = I18n.l Time.now.utc.to_date, format: : medium
@display_period = [bill.period_start, bill.period_end].
.map { |t| I18n.l(t.to_date, format: :medium).strip }
.join(' - ')
發票內容 = WickedPdf.new.pdf_from_string(
render_to_string(template: 'billing_mailer/failed_charge'),
{
margin: {top: 0, bottom: 0, left: 0, right: 0}。
}
)
File.open('invoice.pdf', 'wb') do |file|
file.write invoice_content
結束
mail(
to: bill.billing_email,
bcc: [INVOICES_EMAIL_FULL, SUPPORT_EMAIL_FULL],
主題。I18n.t('billing_mailer.subject_failed_charge')。
)
結束
結束
結果郵件:
日期。Wed, 08 Sep 2021 19:22:18 0400
來自。GlassFrog Billing
收件人:[email protected]
郵件地址:Message-ID:
主題。在向您的信用卡收取GlassFrog費用時出現了問題。
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_6138d52a674b5_75fc116ac69560";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_6138d52a674b5_75fc116ac69560
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
玻璃蛙標志
9月8日, 2021年
***********************************************************
您的信用卡在為GlassFrog充電時出現了問題
***********************************************************
親愛的Sally Payer。
您的信用卡在上個月(2015年2月1日=E2=80=93 2015年2月28日)為您的GlassFrog賬戶充值時出現了問題。請
更新您在GlassFrog中的信用卡資訊,請訪問您的
組織的賬單和計劃管理頁面
( http://app.glassfrog.local:16124/accounts/15570897/edit )。我們
我們將在當月10日第二次嘗試從您的卡上扣款。
月的10號從您的卡上扣款。
請注意,逾期10天的賬戶將被從高級版降級為免費版,功能受限。
從高級賬戶降為免費賬戶,但功能有限,直到付款到期為止。
繳費。
如果你有任何問題或需要任何幫助,請不要猶豫。
回復此郵件或聯系我們
( http://glassfrog.com/contact )。
請注意。
玻璃蛙帳務部
致
帳單殼
來自。
HolacracyOne, LLC
12333 Sowden Rd.
Ste B #33583
休斯頓,德克薩斯州 77080-2059
美國
-------
詳情
-------
發票號
104275561
日期
說明
金額
2015年2月1日至
2015年2月28日
高級計劃 8個用戶@9/月每人
$72.00
共計
$72.00
ref. org/15570897
浩然之家有限公司
12333 Sowden Rd. Ste B #33583, Houston, Texas 77080-2059=
----==_mimepart_6138d52a674b5_75fc116ac69560
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
=
=
玻璃蛙
2021年9月8日
您的信用卡在為GlassFrog充電時出現了問題
親愛的Sally Payer。
您的信用卡在為您的GlassFrog賬戶充值時出現了問題 =
為上個月(2015年2月1日=E2=80=93 2015年2月28日)。請更新您在GlassFrog的c=
更新您在GlassFrog的信用卡資訊,請訪問您的組織的B=
賬單和計劃管理頁面。我們將在2015年2月10日進行第二次嘗試
在每月10日從您的卡中扣除。
請注意,逾期10天的賬戶將被從Prem?
ium降級為免費,功能有限,直到付款到期。
如果你有任何問題,或需要任何幫助,請不要猶豫,立即復制
如果你有任何問題或需要任何幫助,請不要猶豫,復制到這個電子郵件或聯系=
我們。
謝謝。
來自。
HolacracyOne, LLC
12333 Sowden Rd.
Ste B #33583
Houston, Texas 77080-2059
美國
詳細內容
發票號
104275561
日期
說明
金額
2015年2月1日 -=
2015年2月28日=
高級計劃 8個用戶 @9/月 每個用戶
浩然正氣有限公司(HolacracyOne, LLC
12333 Sowden Rd. Ste B #33583, Houston, Texas 77080-2059
----==_mimepart_6138d52a674b5_75fc116ac69560--
P.S. 寫入檔案是為了除錯的目的
。uj5u.com熱心網友回復:
問題出在基類中,那里有一行:
問題出在基類中。
after_action :convert_ul_to_table。
def convert_ul_to_table
body = message.body.to_s
message.body = to_table(Rinku.auto_link(body))
end
增加對附件的檢查可以解決這個問題:
def convert_ul_to_table
return nil unless message.attachments.empull?
body = message.body.to_s
message.body = to_table(Rinku.auto_link(body))
end。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/307619.html
標籤:
