邮件程序发送密码重置电子邮件

邮件程序发送密码重置电子邮件

本文介绍了如何自定义设计以使用 PostMark 邮件程序发送密码重置电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PostMarkApp 并利用 Rails gems (postmark-railspostmark-gem、和 邮件).我已经成功创建了一个邮件程序来处理发送购买收据,但我无法接收忘记密码的电子邮件.我的开发日志显示 Devise 发送了消息,但我的收件箱中没有收到电子邮件,并且 PostMark 积分没有减少.

I'm trying to get all of my system's email notifications under one umbrella using PostMarkApp and utilizing the Rails gems (postmark-rails, postmark-gem, and mail). I have successfully created a mailer that handles sending receipts for purchases but I haven't been able to receive emails for forgotten passwords. My development logs show that Devise sent the message but no email is received in my inbox and the PostMark credits are not decremented.

让 Devise 的邮件通过我的 PostMark 帐户发送的最佳或最简单的方法是什么?

What's the best or easiest way to have Devise's mailers send through my PostMark account?

来自 config/environments/development.rb 的片段

config.action_mailer.delivery_method      = :postmark
config.action_mailer.postmark_settings    = { :api_key => "VALID_API_KEY_WAS_HERE" }
config.postmark_signature                 = VALID_POSTMARK_SIGNATURE_WAS_HERE

使用邮戳的我的邮件

class Notifier < ActionMailer::Base
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end
end

我的问题的解决方案如下

通过让我的 Notifier 邮件程序扩展 Devise::Mailer 并指定 Devise 使用我的通知程序作为 config/initializers/devise.rb

SOLUTION to my question is below

Solved it by having my Notifier mailer extend Devise::Mailer and specifying Devise to use my Notifier as the mailer within config/initializers/devise.rb

来自 config/initializers/devise.rb 的片段

# Configure the class responsible to send e-mails.
config.mailer = "Notifier"

现在是我的通知邮件程序

class Notifier < Devise::Mailer
  # set some sensible defaults
  default :from => MyApp::Application.config.postmark_signature

  # send a receipt of the Member's purchase
  def receipt_message(order)
    @order = order
    @billing_address = order.convert_billing_address_to_hash(order.billing_address)

    mail(:to => @order.user.email, :subject => "Your Order Receipt", :tag => 'order-receipt', :content_type => "text/html") do |format|
      format.html
    end
  end

  # send password reset instructions
  def reset_password_instructions(user)
     @resource = user
     mail(:to => @resource.email, :subject => "Reset password instructions", :tag => 'password-reset', :content_type => "text/html") do |format|
       format.html { render "devise/mailer/reset_password_instructions" }
     end
   end
end

推荐答案

使用最新版本的 Devise,上述方法对我没有帮助.这是我的解决方案.

Using the latest version of Devise, the methods above didn't help me. This is my solution.

在 config/application.rb 中:

In config/application.rb:

config.action_mailer.delivery_method   = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }

在 config/initializers/devise.rb:

In config/initializers/devise.rb:

config.mailer = "UserMailer" # UserMailer is my mailer class

在 app/mailers/user_mailer.rb 中:

In app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  default from: "[email protected]"

  def confirmation_instructions(record)
    devise_mail(record, :confirmation_instructions)
  end

  def reset_password_instructions(record)
    devise_mail(record, :reset_password_instructions)
  end

  def unlock_instructions(record)
    devise_mail(record, :unlock_instructions)
  end

  # you can then put any of your own methods here
end

最后,确保您生成了自定义设计视图

Finally, make sure you have generated custom devise views

rails generate devise:views

并将电子邮件模板从 app/views/devise/mailer/移动到 app/views/user_mailer/

and move the email templates from app/views/devise/mailer/ to app/views/user_mailer/

mv app/views/devise/mailer/* app/views/user_mailer/

这篇关于如何自定义设计以使用 PostMark 邮件程序发送密码重置电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:27