发送电子邮件给其他用户

时间:2019-07-30 18:38:23

标签: ruby-on-rails

我正在建立一个网站,如果这两个网站匹配,interviewer可以向employeecandidate发送电子邮件。

http://localhost:3000/positions/1/candidatures中  我可以访问所有三个电子邮件(intervieweremployeecandidate的电子邮件。

我希望interviewer单击一个按钮并发送自动电子邮件,该电子邮件将发送到candidateemployee

我不确定该怎么做。

我见过的所有建议都建议创建一个application_mailer 并在邮件程序控制器中写类似的内容:

class MyMailer < ApplicationMailer
  def welcome_email(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

先前的代码会将电子邮件发送到@user。我希望将电子邮件发送到candidateinterviewer

此外,如果我在基本目录中创建控制器,它将永远无法访问candidateinterviewer电子邮件。 只能在http://localhost:3000/positions/1/candidatures中检索这两封电子邮件。 (嵌套路线)

我有点困惑,想知道更多建议

其他信息:interviewrcandidate来自Devise。 employee是一个简单的Model.rb

1 个答案:

答案 0 :(得分:1)

如果MyMailer具有方法:

def candidate_email(interviewer, employee, candidate)
    @interviewer = interviewer
    @employee = employee
    @candidate = candidate

    mail(to: @candidate.email, subject: 'candidate subject')
end

def employee_email(interviewer, employee, candidate)
    @interviewer = interviewer
    @employee = employee
    @candidate = candidate

    mail(to: @employee.email, subject: 'employee subject')
end

您可以在positions下嵌套另一个控制器,例如:

resources :positions do
    resources :candidatures …
    resources :candidature_notifications, only: :create
end

然后在控制器操作中可以执行以下操作:

class CandidatureNotificationsController < ApplicationController
    def create
        # this is probably the similar to what you are loading in Candidatures#index
        @position = Position.find(params[:position_id]
        @interviewer = …
        @employee = …
        @candidate = …

        MyMailer.candidate_email(@interviewer, @employee, @candidate).deliver 
        MyMailer.employee_email(@interviewer, @employee, @candidate).deliver
        # other notifications here

        redirect_to position_candidatures_path(@position), notice: 'Sent notifications'
    end
end

您在/positions/1/candidatures页上的链接将为<%= link_to 'Send emails', positions_candidature_emails(@position), method: :post %>