Hello,
我有问题行动邮递员,当我尝试执行操作时:
rake send_email
我收到一个错误:
rake aborted!
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in:
* "user_mailer"
这是我的:
邮件程序/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "[email protected] /cdn-cgi/l/email-protection"
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test')
end
end
视图/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>
视图/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
配置/环境/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
我在 stackoverflow 上搜索了解决方案,并尝试了许多类似问题的答案,但不幸的是它们都不适合我。
当我将正文添加到邮件程序方法时,我找到了解决方案,例如:
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test', body: 'something')
end
然后它确实可以工作,但我希望将主体放在单独的文件中,并使其包含用户名和其他内容变得更加复杂。
如果有人知道如何解决这个问题,那么我将非常感激:)