首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何自定义Devise以使用PostMark邮件程序发送密码重置电子邮件

如何自定义Devise以使用PostMark邮件程序发送密码重置电子邮件
EN

Stack Overflow用户
提问于 2011-04-16 00:13:32
回答 3查看 23.8K关注 0票数 19

我正在尝试使用PostMarkApp和Rails get (postmark-railspostmark-gemmail)将我系统的所有电子邮件通知整合到一个保护伞下。我已经成功地创建了一个邮件程序,可以处理购物收据的发送,但我还无法收到忘记密码的电子邮件。我的开发日志显示Devise发送了消息,但我的收件箱中没有收到电子邮件,PostMark积分也没有减少。

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

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

代码语言:javascript
代码运行次数:0
运行
复制
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

使用邮戳我的邮件程序

代码语言:javascript
代码运行次数:0
运行
复制
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中使用我的通告程序作为邮件程序,解决了这个问题

来自config/initializers/devise.rb代码片段

代码语言:javascript
代码运行次数:0
运行
复制
# Configure the class responsible to send e-mails.
config.mailer = "Notifier"

我的通告邮件程序now

代码语言:javascript
代码运行次数:0
运行
复制
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
EN

回答 3

Stack Overflow用户

发布于 2011-09-08 01:55:36

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

在config/application.rb中:

代码语言:javascript
代码运行次数:0
运行
复制
config.action_mailer.delivery_method   = :postmark
config.action_mailer.postmark_settings = { :api_key => "your-API-key-here" }

在config/initializers/devise.rb中:

代码语言:javascript
代码运行次数:0
运行
复制
config.mailer = "UserMailer" # UserMailer is my mailer class

在app/mailers/user_mailer.rb中:

代码语言:javascript
代码运行次数:0
运行
复制
class UserMailer < ActionMailer::Base
  include Devise::Mailers::Helpers

  default from: "default@mydomain.com"

  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

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

代码语言:javascript
代码运行次数:0
运行
复制
rails generate devise:views

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

代码语言:javascript
代码运行次数:0
运行
复制
mv app/views/devise/mailer/* app/views/user_mailer/
票数 20
EN

Stack Overflow用户

发布于 2011-12-03 06:56:54

如果您还想在邮戳标题中指定‘tag’,则必须在邮件程序中执行此操作:

代码语言:javascript
代码运行次数:0
运行
复制
  # this override method is from Devise::Mailers::Helpers
  def headers_for(action)
    headers = {
      :subject        => translate(devise_mapping, action),
      :from           => mailer_sender(devise_mapping),
      :to             => resource.email,
      :template_path  => template_paths,
      :tag            => action.dasherize # specify the tag here
    }
    if resource.respond_to?(:headers_for)
      headers.merge!(resource.headers_for(action))
    end
    unless headers.key?(:reply_to)
      headers[:reply_to] = headers[:from]
    end
    headers
  end
票数 1
EN

Stack Overflow用户

发布于 2011-05-17 04:12:21

我还必须为设计生成视图,并将邮件模板复制到我的邮件程序的正确位置。像这样的东西-

代码语言:javascript
代码运行次数:0
运行
复制
rails generate devise:views
cp app/views/devise/mailer/* app/views/notification_mailer/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5679571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档