Ric*_*mas 5 email ruby-on-rails actionmailer actionview
我有一个继承自ApplicationMailer的Mailer类,后者继承自ActionMailer :: Base.Ruby版本是ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15].
Mailer类如下所示:
class PurchaseOrderStatusMailer < ApplicationMailer
CONTACTS = {
JC: ['me@example.com'],
RM: ['me@example.com']
}
def daily_report_email(facility)
@facility = facility
ingredient_items = LineItem.ingredient.by_facility(facility)
@delivered_count = ingredient_items.by_date([7.days.ago, 7.days.from_now]).delivered.count
@partial_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).partial.count
@late_count = ingredient_items.by_date([7.days.ago, 1.day.ago]).late.count
@expected_count = ingredient_items.by_date([Date.today, 7.days.from_now]).expected.count
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}")
end
end
Run Code Online (Sandbox Code Playgroud)
ApplicationMailer如下所示:
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'notify@example.com'
def facility_email(facility)
emails = Rails.application.config_for(:emails)
(emails[facility] + emails["DEFAULT"]).flatten
end
end
Run Code Online (Sandbox Code Playgroud)
该视图位于app/views/purchase_order_status_mailer/daily_report_email.html.erb.
当我打开我的Rails控制台并键入时PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver,我看到以下错误:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/me/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/actionview-5.0.4/lib/action_view/lookup_context.rb:215:in `normalize_name'
Run Code Online (Sandbox Code Playgroud)
我尝试使用相同的调用将format块传递给对mail助手的调用,如下所示:
mail(to: CONTACTS[facility.to_sym], subject: "#{facility} Daily Receipt Status - #{Date.today}") do |format|
format.text { render plain: "Hey!" }
end
Run Code Online (Sandbox Code Playgroud)
以上产生了以下响应,似乎代表了成功的电子邮件发送:
Rendering text template
Rendered text template (0.0ms)
Sent mail to me@example.com (8.8ms)
Date: Mon, 25 Sep 2017 12:55:11 -0400
From: notify@example.com
To: me@example.com
Message-ID: <59c934efec401_1115b3fd69cc3f840917be@me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hey!
=> #<Mail::Message:70191029273300, Multipart: false, Headers: <Date: Mon, 25 Sep 2017 12:55:11 -0400>, <From: notify@example.com>, <To: ["me@example.com"]>, <Message-ID: <59c934efec401_1115b3fd69cc3f840917be@me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>
Run Code Online (Sandbox Code Playgroud)
我实际上没有收到电子邮件,我认为这意味着我没有在本地计算机上设置SMTP,但上述响应令人鼓舞.除了上面发布的错误之外没有堆栈跟踪,所以我尝试深入研究Rails源代码,并且我看到normalize_name内部lookup_context.rb从args_for_lookup受保护的方法中调用,而后者又被ViewPaths模块的find_all方法调用.但除此之外,很难跟踪调用堆栈,因为我无法找到调用者find_all是谁.
我的问题是:第一次打电话有mail什么问题?
编辑1:我也试过format.html { render html: "<h1>Hello Mikel!</h1>".html_safe }而不是format.text选项,根据这里的例子,我得到了类似的成功消息.
然后我尝试在里面添加一个byebug语句normalize_name,尝试识别成功的电子邮件发送中参数的值是什么,但看起来这个方法在传递块时没有被调用.这让我更加怀疑这个问题与我的观点有某种关系.但我还不能确认.
问题在这里
PurchaseOrderStatusMailer.new.daily_report_email('JC').deliver
Run Code Online (Sandbox Code Playgroud)
删除新的ie
PurchaseOrderStatusMailer.daily_report_email('JC').deliver
Run Code Online (Sandbox Code Playgroud)
因为它的错误对象无法推断出模板路径/文件
我能够通过将和选项传递给对帮助程序的原始调用来生成success响应(包括我的邮件程序的渲染模板文件),如下所示:template_pathtemplate_namemail
mail(
to: CONTACTS[facility.to_sym],
subject: "#{facility} Daily Receipt Status - #{Date.today}",
template_path: 'purchase_order_status_mailer',
template_name: 'daily_report_email')
Run Code Online (Sandbox Code Playgroud)
这生成了以下成功响应:
Rendering purchase_order_status_mailer/daily_report_email.text.erb
Rendered purchase_order_status_mailer/daily_report_email.text.erb (0.3ms)
Sent mail to me@example.com (12.1ms)
Date: Mon, 25 Sep 2017 15:18:25 -0400
From: notify@example.com
To: me@example.com
Message-ID: <59c9568121cec_11e943fee8503f82823542@me-MBP.mail>
Subject: JC Daily Receipt Status - 2017-09-25
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59c9568120033_11e943fee8503f8282341e
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Please find the receipt status for JC as of 09-25-2017:
=================================================================================================
...<lots more content, including ERB rendered into HTML>...
=> #<Mail::Message:70293683934580, Multipart: true, Headers: <Date: Mon, 25 Sep 2017 15:18:25 -0400>, <From: notify@example.com>, <To: ["me@example.com"]>, <Message-ID: <59c9568121cec_11e943fee8503f82823542@me-MBP.mail>>, <Subject: JC Daily Receipt Status - 2017-09-25>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_59c9568120033_11e943fee8503f8282341e"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>
Run Code Online (Sandbox Code Playgroud)
我对为什么必须添加这些选项感到有点困惑,因为文档没有明确说明这些选项是必需的,并且我的模板和文件夹似乎命名正确,但它有效并且我有截止日期,所以我继续。:-)