簡體   English   中英

郵件錯誤缺少模板

[英]Mailer error missing template

你好,

當我嘗試執行操作時,我遇到了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: "from@example.com"

  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 上搜索了解決方案,並嘗試了許多類似問題的答案,但不幸的是,沒有一個對我有用。

我找到了解決方案,當我將 body 添加到 mailer 方法時,例如:

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

然后它確實有效,但我希望在單獨的文件中有一個正文,並使用用戶名和其他內容使其更加復雜。

如果有人知道如何解決這個問題,那么我將非常感激:)

嘗試添加布局

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

  layout "mailer"

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

end

你能發布一個極簡的 Github 存儲庫來重現你的錯誤嗎?

您可以嘗試生成郵件以檢查您是否忘記了某些內容:

bundle exec rails generate mailer mailer_classname mailer_viewname

在你的情況下:

bundle exec rails generate mailer user_mailer mailer

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

  layout "mailer"

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

end

在 layout 下添加一個 html 布局#

郵件程序.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>

在布局下添加文本布局#

郵件程序文本文件

<%= yield %>

使用預覽根據您的測試框架檢查您的電子郵件。

Rails 很可能沒有找到您新創建的文件。 如果您正在運行spring gem,那可能就是問題所在。 (更多關於春天: 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)

終於對我有用了,希望它會幫助你們中的一些人!

(其中一些步驟可能是多余的。)

我重命名了我的方法並且它起作用了。 也許方法的名稱不能是mail或mailer...

我有同樣的問題。 重新啟動 sidekiq 為我解決了這個問題。

我遇到了同樣的問題,我的解決方案是刪除郵件模板文件,在您的情況下為views/user_mailer/mailer.html.erb並重新創建它。 似乎我創建了具有正確名稱的文件,但在某處有一些奇怪的空白,而 ActionMailer 無法識別它。

我有一個類似的問題。 我錯過了在我的 Gemfile 中添加 gem 'haml-rails'。 檢查您是否缺少寶石。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM