简体   繁体   中英

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. 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. 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 . You have to move your logic outside the mailer or use cc or bcc to deliver the mail to multiple recipients.

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.

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:

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. the delayed job gem is available on github: https://github.com/collectiveidea/delayed_job

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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