发送消息所需的发件人(Return-Path,Sender或From)

时间:2013-01-10 07:52:55

标签: ruby-on-rails-3 actionmailer delayed-job

我正在尝试使用delayed_job gem在rails中从动作邮件发送电子邮件。 我从after_create回调中调用动作邮件方法

在演示模型中

after_create :send_mail
def send_mail
  DemoMailer.to_client( self).deliver
end

在demo_mailer中

def to_client( demo)
  @demo = demo
  mail(:to => demo.email, :subject => "Confirmation")
end
handle_asynchronously :to_client

显示错误

ArgumentError in DemosController#create

A sender (Return-Path, Sender or From) required to send a message

然后我改变了

def send_mail
  DemoMailer.to_client( self).deliver
end

def send_mail
  DemoMailer.delay.to_client( self)
end

它没有显示任何错误,但我也没有收到任何邮件。

2 个答案:

答案 0 :(得分:2)

如错误所示:您缺少return-pathsenderfrom字段。设置from字段并不困难:

mail(:to => demo.email, :from => 'webmaster@your-domain.com', :subject => "Confirmation")

有关详细信息,请参阅ActionMailer#mail documentation

答案 1 :(得分:1)

如果您在顶部设置from

class FoobarMailer < ActionMailer::Base
  default from: SomeClass.email_variable
  #...

您可以尝试一下:

class FoobarMailer < ActionMailer::Base
  default from: -> { SomeClass.email_variable }
  #...

或者发送它就像@Veger提到的那样:

mail(:to => demo.email, :from => 'webmaster@your-domain.com', :subject => "Confirmation")