RoR send email with html template

520 Views Asked by At

I would like send email with html.erb template and I have some code. Like this:

class Mailer < ActionMailer::Base
  def notification(to_email)
    begin
      mail(to: '[email protected]', subject: "Test message")
      rescue Exception => e
      return
    end
  end
end

And some html.erb file:

Hello, <%= @name %>.

But I don't know how to attach this template for my method. Help me please with this issue, because i can't fount normally manual about it.

1

There are 1 best solutions below

0
On BEST ANSWER

You can create a page named notification.html.erb in app/views/mailer directory. You will get all instance variable in notification.html.erb that is defined in your def notification(to_email)

def notification(to_email)
  @name = #pass name here
  #your code goes here 
end

Now @name will be available in notification.html.erb like

Hello, <%= @name %>