[英]How to use body erb template in Mail gem?
我想用作郵件erb模板中的正文。 當我在Pony gem上設置它時,它可以工作。
post 'test_mailer' do
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body erb(:test_mailer) # this isn't working
end
end
private
fields = [1, 2] # some array
ERB文件
<% fields.each do |f| %>
<%= f %>
<% end %>
假設使用Pony的原始Sinatra路線如下所示:
post 'test_mailer' do
Pony.mail :to => ['test1@me.com', 'test2@me.com'],
:from => 'you@you.com',
:subject => 'testing',
:body => erb(:test_mailer)
end
您可以看到此處的電子郵件屬性是由哈希指定的。 切換到使用Mail gem時,它的屬性由一個塊定義,該塊在特定的上下文中被調用,以便可以使用這些特殊方法。
我認為問題可能與在塊內調用erb
有關。 您可以嘗試以下操作:
嘗試以可以傳遞到塊中的方式生成ERB:
post 'test_mailer' do
email_body = erb :test_mailer, locals: {fields: fields}
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body email_body
end
end
或全局調用ERB而不是使用sinatra幫助器:
post 'test_mailer' do
context = binding
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body ERB.new(File.read('views/test_mailer.erb')).result(context)
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.