Mailer error missing template

2019-04-03 22:20发布

Hello,

I have problem with ActionMailer, when I try to execute action:

rake send_email

I get a error:

    rake aborted!
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in:
  * "user_mailer"

Here's my:

mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "from@example.com"

  def mailer(user)
    @user = user
    mail(to: @user.email, subject: 'Test')
  end

end

views/user_mailer/mailer.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <p>
      Sample mail.
    </p>
  </body>
</html>

views/user_mailer/mailer.text.erb

Sample mail.

lib/tasks/emails_task.rake

desc 'send email'
task send_email: :environment do
  UserMailer.mailer(User.last).deliver!
end

config/environments/development.rb

# I recommend using this line to show error
  config.action_mailer.raise_delivery_errors = true

  # ActionMailer Config
  config.action_mailer.delivery_method = :letter_opener

# config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# config.action_mailer.delivery_method = :smtp
# SMTP settings for gmail
config.action_mailer.smtp_settings = {
 :address              => "smtp.gmail.com",
 :port                 => 587,
 :user_name            => ENV['gmail_username'],
 :password             => ENV['gmail_password'],
 :authentication       => "plain",
:enable_starttls_auto => true
}

# Send email in development mode?
config.action_mailer.perform_deliveries = true

I searched for the solution on stackoverflow and I tried many of the answers for the similar problem but unfortunately none of them worked for me.

I found solution, when I add body to mailer method like:

def mailer(user)
  @user = user
  mail(to: @user.email, subject: 'Test', body: 'something')
end

Then it does work but I would like to have a body in separate files and make it more complex with user name and other things.

If someone have a idea how to solve this problem then I would be very thankful :)

7条回答
叛逆
2楼-- · 2019-04-03 23:27

I had the same problem. Restarting sidekiq solved it for me.

查看更多
登录 后发表回答