Rails Mailer:根据匹配条件将电子邮件发送给收件人

时间:2018-12-17 22:42:28

标签: ruby-on-rails ruby actionmailer

我已经设法将ActionMailer配置为基于新的“提交”将电子邮件发送给收件人。但是,我的应用程序的工作方式是,它采用提交的“所需位置”字段并将其与另一个称为“代理”的模型中的“公司业务位置”字段进行匹配,以提供根据用户位置匹配的索引视图。也就是说,如果我提交的邮件位于伦敦,那么只有位于伦敦的代理商才能看到它。这将我带到我的电子邮件中,是否仍然可以创建以相同方式工作的邮件程序?因此,仅将电子邮件发送给符合所需提交位置的代理商吗?

邮件程序

class NewSubmissionMailer < ApplicationMailer

  def submission_email(submission)
    @submission = submission
    mail(to: @submission.Email, subject: 'Welcome to Ottom8')
  end

end

提交控制器

respond_to do |format|
  if @submission.save

    # Tell the UserMailer to send a welcome email after save       
    NewSubmissionMailer.submission_email(@submission).deliver_now

匹配两个型号的代码

def index
  @submissions = Submission.where(:Desired_Location => current_agent.Company_Business_Location)
end

谢谢

1 个答案:

答案 0 :(得分:1)

respond_to do |format|
  if @submission.save
    # Tell the UserMailer to send a welcome email after save
    NewSubmissionMailer.submission_email(@submission).deliver_now

    # Send emails to matching agents
    NewSubmissionMailer.matching_agents_email(@submission).deliver_now

,然后在邮件::matching_agents_email中:

def matching_agents_email(submission) 
  @submission = submission
  agents = Agent.where(:Company_Business_Location => @submission.Desired_Location)
  mail(to: agents.pluck(:email) # ... Rest of email logic. )