你好,
当我尝试执行操作时,ActionMailer 遇到问题:
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]"
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
我在 stackoverflow 上搜索了解决方案,并尝试了许多类似问题的答案,但不幸的是它们都不适合我。
当我将正文添加到邮件程序方法时,我找到了解决方案,例如:
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test', body: 'something')
end
然后它确实可以工作,但我希望将主体放在单独的文件中,并使其包含用户名和其他内容变得更加复杂。
如果有人知道如何解决这个问题那么我将非常感激:)
尝试添加布局
class UserMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test')
end
end
我也有同样的问题。重新启动 sidekiq 为我解决了这个问题。
您可以发布一个极简的 Github 存储库来重现您的错误吗?
您可以尝试生成邮件以检查您是否忘记了某些内容:
bundle exec rails generate mailer mailer_classname mailer_viewname
您的情况:
bundle exec rails generate mailer user_mailer mailer
Rails 很可能找不到您新创建的文件。如果您正在运行
spring
gem,这可能就是问题所在。 (更多关于 spring 的信息:https://github.com/rails/spring)
设置终端命令并停止运行
spring
:
$ bundle exec spring binstub --all
$ bin/spring stop
我尝试了这个,然后使用
rails s
重新运行我的应用程序。这不起作用,所以尝试完全关闭终端并重新启动计算机。运行 rails s
并尝试在第二个选项卡中使用 rails c
进行测试:
mail = UserMailer.mailer(User.last)
这终于对我有用,希望对你们有些人有帮助!
(其中一些步骤可能是多余的。)
class UserMailer < ActionMailer::Base
default from: "[email protected]"
layout "mailer"
def mailer(user)
@user = user
mail(to: @user.email, subject: 'Test')
end
end
在layout下添加html布局#
mailer.html.erb
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<%= yield %>
</body>
</html>
在布局#下添加文本布局
mailer.text.erb
<%= yield %>
使用预览根据您的测试框架检查您的电子邮件。
我重命名了我的方法并且它起作用了。也许方法的名称不能是mail或mailer...
我遇到了同样的问题,我的解决方案是删除邮件程序模板文件(在您的情况下)
views/user_mailer/mailer.html.erb
并重新创建它。
看来我创建了具有正确名称的文件,但在某处有一些奇怪的空格,而 ActionMailer 无法识别它。
我也有类似的问题。我错过了在 Gemfile 中添加 gem 'haml-rails'。检查一下您是否丢失了宝石。
好吧,在删除模板并使用相同名称重新创建它之后,我的答案可能是除了我的工作之外最愚蠢的答案😂
如果您想发送 html 但告诉 Rails 不要将其放置在布局中,请执行以下两件事:
html_body
,使用 body
来代替content_type: "text/html"
参数class TestMailer < ApplicationMailer
default from: '[email protected]'
def hello
mail(
subject: 'Hello from me',
to: '[email protected]',
from: '[email protected]',
body: '<strong>Hello</strong> dear user...', # <--- use body instead of html_body
content_type: "text/html" # <--- add this line too
)
end
end
TestMailer.hello.deliver_now