简体   繁体   English

Rails动作邮件如果声明

[英]Rails action mailer if statements

I'm trying to put some logic into my mailer. 我试图把一些逻辑放进我的邮件程序中。 Basically, I have a dozen boolean fields in one of my models and for each field that is true, I want an email to be sent to a specific address. 基本上,我在我的一个模型中有十几个布尔字段,对于每个真实的字段,我希望将电子邮件发送到特定地址。 The model with the boolean fields is called Ecn. 具有布尔字段的模型称为Ecn。 This is my notifier file: 这是我的通知文件:

class EcnNotifier < ActionMailer::Base
  default from: "engineering_notices@wilfley.com"

  def submitted(ecn)
    @ecn = ecn
    @email_list = EmailList.all
    if @ecn.distribute_engineering?  
        mail to: "abc@gmail.com", subject: 'ECN approvald' 
    else

    end
    if @ecn.distribute_purchasing? 
        mail to: "123@gmail.com", subject: 'ECN approval' 
    else

    end
    mail to: "aaa@test.com", subject: 'ECN approval'
  end
end

And the action I've created in my controller looks like this: 我在控制器中创建的动作如下所示:

  def submit

    @ecn = Ecn.find(params[:id])

    respond_to do |format|
      EcnNotifier.submitted(@ecn).deliver
      format.html { redirect_to ecns_url, alert: "Ecn has been submitted for approval." }
      format.json { render json: @ecns }
    end
  end

An email gets sent automatically to aaa@test.com, but regardless of whether or not the distribute_engineering and distribute_purchasing are true, an email is not sent to the other addresses. 电子邮件会自动发送到aaa@test.com,但无论distribute_engineering和distribute_purchasing是否为true,都不会向其他地址发送电子邮件。 I'm assuming I'm incorrectly accessing my object instance, but I'm not sure where I'm going wrong. 我假设我错误地访问了我的对象实例,但我不确定我哪里出错了。

You can only send one mail with one call of deliver . 您只能发送一个邮件与一个呼叫deliver You have to move your logic outside the mailer or use cc or bcc to deliver the mail to multiple recipients. 您必须将您的逻辑移到邮件程序之外,或使用cc或bcc将邮件传递给多个收件人。

I thought that you only can send one mail in one mailer action, and not multiple mails. 我以为你只能在一个邮件程序中发送一封邮件,而不是多封邮件。 You can send one mail and add a few cc or bcc, but only can send one mail within the scope of one action. 您可以发送一封邮件并添加几个cc或密件抄送,但只能在一个操作范围内发送一封邮件。

I would change the controller to following: 我会将控制器更改为以下内容:

def submit
  @ecn = Ecn.find(params[:id])

  respond_to do |format|
    EcnNotifier.notify_default(@ecn).deliver
    EcnNotifier.distribute_engineering(@ecn).delifer if @ecn.distribute_engineering?
    EcnNotifier.distribute_purchasing(@ecn).deliver if @ecn.distribute_purchasing?
    # and so on...
    format.html { redirect_to ecns_url, alert: "Ecn has been submitted for approval." }
    format.json { render json: @ecns }
  end
end

And your EcnNotifier mailer: 和你的EcnNotifier邮件:

class EcnNotifier < ActionMailer::Base
  default from: "engineering_notices@wilfley.com"

  def notify_default(ecn)
    @ecn = ecn
    @email_list = EmailList.all
    mail to: "aaa@test.com", subject: 'ECN approval'
  end

  def distribute_engineering(ecn)
    @ecn = ecn
    @email_list = EmailList.all
    mail to: "abc@gmail.com", subject: 'ECN approval' 
  end

  def distribute_purchasing(ecn)
    @ecn = ecn
    @email_list = EmailList.all
    mail to: "abc@gmail.com", subject: 'ECN approval' 
  end

end

Btw: if you are sending multiple mails, you should do that via delayed job gem or something like that. 顺便说一句:如果你发送多封邮件,你应该通过延迟工作宝石或类似的东西来做到这一点。 Otherwise the user has to wait quite a long time, because the ruby process will be blocked during sending all the mails. 否则用户必须等待很长时间,因为在发送所有邮件期间将阻止ruby进程。 the delayed job gem is available on github: https://github.com/collectiveidea/delayed_job 延迟的job gem可以在github上找到: https//github.com/collectiveidea/delayed_job

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM