简体   繁体   中英

Send email to different users

I am building a website where a interviewer can send email to employee and candidate if these two match.

In http://localhost:3000/positions/1/candidatures I have access) to the email of all the three ( interviewer , employee and candidate .

I want the interviewer to click a button and send an automatic email is sent to candidate and employee .

I am not sure how to do so.

All the turorials I have seen suggest to create a application_mailer and write in the mailer controller something like:

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

The previous code will send the email to the @user . I want the email to be sent to candidate and interviewer .

In addition, if I create the controller in the base directory, it will never have access to the candidate and interviewer email. These two emails are retrievable only in http://localhost:3000/positions/1/candidatures . (Nested routes)

I am a bit stuck, and would like to know more suggestions

Additional info: interviewr and candidate are from Devise. While the employee is a simple Model.rb

If MyMailer has methods:

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

You can nest an additional controller under positions , something like:

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

Then in your controller action you can do something like:

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

And your link on the /positions/1/candidatures page will be <%= link_to 'Send emails', positions_candidature_emails(@position), method: :post %>

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