Search code examples
ruby-on-rails-4mailgunmailer

How to tell actionmailer to use html.erb or text.erb file format in rails


I'm following this guide here: https://launchschool.com/blog/handling-emails-in-rails to set up a mailer for my ruby-on-rails project.

This code works: example_mailer.rb

class ExampleMailer < ApplicationMailer
  default from: "[email protected]"

  def sample_email(user)
    @user = user


    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {:from    => ENV['gmail_username'],
                      :to      => @user.email,
                      :subject => 'Sample Mail using Mailgun API',
                      :text    => 'This mail is sent using Mailgun API via mailgun-ruby'}
    mg_client.send_message ENV['domain'], message_params

  end
end

When I call ExampleMailer.sample_email(@user).deliver in my controller, I get an eMail with the text: 'This mail is sent using Mailgun API via mailgun-ruby' as it is saved in :text.

Now, I want, that this html.erb or this text.erb file is send, not just :text:

sample_email.html.erb

<!DOCTYPE html>
<html>
<head>
  <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<h1>Hi <%= @user.username %></h1>
<p>
 Sie haben folgende Tags ausgewählt:
  <% @user.tag_list.each do |tag| %>
    <%= tag %> <br>
  <% end %>

  <br><br>
  <% @infosall.each do |info| %>
      <%= info.name %><br>
  <% end %><br>
</p>
</body>
</html>

sample_email.text.erb

Hi <%= @user.username %>

Sie haben folgende Tags ausgewählt:
<% @user.tag_list.each do |tag| %>
    <%= tag %>
<% end %>

<% @infosall.each do |info| %>
    <%= info.name %>
<% end %>

What do I need to do, so these files are sent, rather than just the :text?

sample_email.text.erb and sample_email.html.erb are found in: app/views/example_mailer

The file example_mailer.rb is in: app/mailers/example_mailer.rb

Thanks in advance!

Edit: I deleted the :text line and the text.erb-file and now I have this:

mg_client = Mailgun::Client.new ENV['api_key']
message_params = {:from    => ENV['gmail_username'],
                  :to      => @user.email,
                  :subject => 'Sample Mail using Mailgun API'}
mg_client.send_message ENV['domain'], message_params

But now, I get this error:

2016-02-14T13:28:12.483443+00:00 app[web.1]: ExampleMailer#sample_email: processed outbound mail in 150.3ms
2016-02-14T13:28:12.483718+00:00 app[web.1]: Completed 500 Internal Server Error in 495ms (ActiveRecord: 30.2ms)

2016-02-14T13:28:12.485057+00:00 app[web.1]: 
2016-02-14T13:28:12.485054+00:00 app[web.1]: Mailgun::CommunicationError (400 Bad Request):
2016-02-14T13:28:12.485055+00:00 app[web.1]:   app/mailers/example_mailer.rb:19:in `sample_email'
2016-02-14T13:28:12.485056+00:00 app[web.1]:   app/controllers/events_controller.rb:26:in `create'
2016-02-14T13:28:12.485057+00:00 app[web.1]: 

In example_mailer.rb:19:     `mg_client.send_message ENV['domain'], message_params`
In events_controller.rb:26: `ExampleMailer.sample_email(@user).deliver`

Where did I go wrong? Thanks!

Edit2: My smtp settings:

  config.action_mailer.delivery_method = :smtp
  # SMTP settings for gmail
  ActionMailer::Base.smtp_settings = {
      :port           => 587,
      :address        => "smtp.mailgun.org",
      :domain         => ENV['domain'],
      :user_name      => ENV['username'],
      :password       => ENV['password'],
      :authentication => :plain,
  }

Solution

  • Change your simple_email funtion like the one below

    def sample_email(user)
        @user = user
        mail(to: @user.email, subject: 'Sample Email')
    end
    

    You can also define the format by changing above function like this

    def sample_email(user)
        @user = user
        mail(to: @user.email, subject: 'Sample Email') do |format|
            format.text
            # or
            format.html
        end
    end