Rails Mailer - 从'回退到默认值'

时间:2014-11-30 11:46:36

标签: ruby-on-rails actionmailer

我想根据某些逻辑设置从特定电子邮件地址发送的邮件。

如果逻辑失败,我想回退到默认电子邮件,并保持干燥

class myMailer  < ActionMailer::Base
default from: "somespecial@email.com"


def special_send

if (logic)
 from = "noone special"
else
# set it as the default
end

mail(to: ..., subject: ..., from: from)

end

1 个答案:

答案 0 :(得分:2)

试试这个(未经测试)

def special_send

if (logic)
   mail_hash = {to: ..., subject: ..., from: from}
else
   mail_hash = {to: ..., subject: ...}
end

mail(mail_hash)

end

另一种解决方案

def special_send
   mail_hash = {to: ..., subject: ..., from: from}    
   mail_hash.delete(:from) unless (logic)
   mail(mail_hash)
end