简体   繁体   中英

ActionMailer automatic emails. Ruby on Rails

Building A "Ticketing System" for our support team, so they can create tickets when a customer calls and requests something.

I have quite a lot up and running, now I need each time a ticket is created for the app to send an email to the person the the ticket is assigned to. This info is all in the DB and accessible easily.

Do you guys have any suggestion how I can imolement this or any documentation I can use?

Thanks!

Check out this Railscast to get started on your mailer - http://railscasts.com/episodes/206-action-mailer-in-rails-3

Basically, in your Ticket create method, you want to call the mailer and pass in the email of the assigned person so that every time a Ticket is created, it sends an email.

# Model
class Ticket

  ...
  after_create: :send_email
  ...

  def send_email
    Mailer.delay_for(5.minutes).send_ticket(id, assigned_to.id) # delay sending e-mail
    Mailer.send_ticket(id, assigned_to.id).deliver # or send it now..
  end
end

class Mailer < ActionMailer::Base

  def send_ticket(ticket_id, assigned_to_id)
    @assigned_to = User.find(assigned_to_id)
    @ticket = Ticket.find(ticket_id)
    mail(
      from: 'you<do-not-reply@your_domain.com>',
      to: @assigned_to.email,
      subject: 'new assignment'
    )
  end
end

this is mainly the logic, now for the views add the following file app/views/mailer/send_ticket.html.haml

Hey #{@assigned_to.name},
%p you have been assigned to a new ticket #{@ticket.id}
%p here is the link to the ticket #{@ticket.url}

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