【问题标题】:ActionMailer not delivering mail in developmentActionMailer 未在开发中发送邮件
【发布时间】:2014-12-12 20:21:50
【问题描述】:

我正在尝试构建密码重置电子邮件。我正在使用 Rails 3.2.16 和 Ruby 1.9.3p327,我已经阅读了很多关于这个问题的答案,并且几乎尝试了所有方法。我也经历了action mailer basics guide,据我所知,这应该可以工作,但进展并不顺利。我会逐步指导我如何设置它。

首先,因为我试图让这个在开发中工作,在development.rb注意: 我每次编辑 development.rb 文件时都重置了应用程序。

#this is all the action mailer settings i have defined in development.rb
config.action_mailer.raise_delivery_errors = true # Set to true so that errors would be visible.
config.action_mailer.perform_deliveries = true # I read about this possible fix on SO
config.action_mailer.default_url_options = {
  host: "boogle.dev"
}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address              => "smtp.office365.com",
  :port                 => 587,
  :domain               => "mpowered.co.za",
  :user_name            => "support@mpowered.co.za",
  :password             => "password",
  :authentication       => :login,
  :enable_starttls_auto => true
}

我的通知类继承自 ActionMailer

class Notifier < ActionMailer::Base
  default from: "Mpowered - BEEtoolkit <support@mpowered.co.za>"

  def deliver_password_reset_email(user)
    @edit_password_reset_url = edit_password_reset_url(user.perishable_token)
    @name = user.name

    mail(
      subject:       "Password Reset Instructions",
      to:            user.email,
      date:          Time.now,
      content_type:  "text/html")
  end
end

在我的用户模型中,我设置了发送邮件的方法以及 perishable_token 的设置

def deliver_password_reset_instructions!
  reset_perishable_token!
  Notifier.deliver_password_reset_email(self)
end

密码重置控制器的设置如下:

class PasswordResetsController < ApplicationController
  before_filter :require_no_user
  before_filter :load_user_using_perishable_token, :only => [ :edit, :update ]

  def new
  end

  def create
    @user = User.find_by_email(params[:email])
    if @user
      @user.deliver_password_reset_instructions!
      flash[:notice] = "Instructions to reset your password have been emailed to you"
      render action: :new
    else
      flash.now[:error] = "No user was found with email address #{params[:email]}"
      render action: :new
    end
  end

  def edit
  end

  def update
    @user.password = params[:password]
    # Only if your are using password confirmation
    @user.password_confirmation = params[:password]

    # Use @user.save_without_session_maintenance instead if you
    # don't want the user to be signed in automatically.
    if @user.save
      flash[:success] = "Your password was successfully updated"
      redirect_to @user
    else
      render action: :edit
    end
  end


  private

  def load_user_using_perishable_token
    @user = User.find_using_perishable_token(params[:id])
    unless @user
      flash[:error] = "We're sorry, but we could not locate your account"
      redirect_to root_url
    end
  end
end

我在路线中添加了资源:

resources :password_resets, :only => [ :new, :create, :edit, :update ]

我的观点很简单: 在 app/views/password_resets/new.haml.html

%br
= form_tag password_resets_path, class: 'form-inline' do
  %legend Forgotten Password
  %p Enter your email address and instructions to reset your password will be emailed to you:
  %span.span1
    = label_tag :email, 'Email'
  = text_field_tag :email
  = submit_tag 'Reset my password', class: 'btn'
%br

因此,一旦您提交了有效的电子邮件,这应该会发送邮件。 然后您应该会收到一封包含以下内容的电子邮件:app/views/notifier/password_reset_instructions.html.haml

%h1 Password Reset Instructions

%p
  A request to reset your password has been made. If you did not make
  this request, simply ignore this email. If you did make this
  request, please follow the link below.

= link_to "Reset Password!", @edit_password_reset_url

该链接应将您带到一个表单,然后您可以在其中保存新密码和密码确认。

app/views/password_resets/edit.html.haml

- if @user
  = form_for @user, :url => password_reset_path, :method => :put do |f|
    %legend Change My Password
    %p Please select a new password for your account
    .span8
      = f.field :password, :field_type => :password_field, :label => "New password"
      = f.field :password_confirmation, :field_type => :password_field
    .clearfix
    = f.submit "Update my password", class: 'btn'
- else
  %h3 We couldn't identify your reset code
  %p We're sorry, but we could not locate any accounts with reset codes that matched yours.
  %p If you are having difficulties resetting your password, try copying and pasting the URL from your password reset email into your browser or restarting the password reset process.

您可以在其中保存新密码,然后再次登录。这是我在应用程序中设置的。但是每次我尝试按照系统发送它时,它都会说电子邮件已发送但什么也没收到。我还尝试在控制台中加载用户,然后运行u.deliver_password_reset_instructions!

我明白了:

但我的收件箱中仍然没有电子邮件。我目前已将通知程序中的电子邮件地址设置为我自己的个人电子邮件地址,因此无论请求什么有效的电子邮件地址,电子邮件都应该发送给我。

在过去的 12 个小时里,我一直在碰壁,不知道该往哪里走。我希望我已经做了一个新的眼睛可以捕捉到的球。

【问题讨论】:

  • host: "boogle.dev" ?
  • 那是战俘,我相信。 edgeguides.rubyonrails.org/… 用于在邮件中生成 URL。
  • @TheLegend 如果我没记错的话,您在调用您的邮件功能时忘记写.deliver 更改此设置并尝试一次`Notifier.deliver_password_reset_email(self).deliver`
  • @anusha +1 - 请将其作为答案发布。
  • @TheLegend 你试过anusha 说的吗?

标签: ruby-on-rails ruby email actionmailer


【解决方案1】:

像这样调用Mailer方法时需要加上.deliver

def deliver_password_reset_instructions!
  reset_perishable_token!
  Notifier.deliver_password_reset_email(self).deliver
end

希望对你有帮助

【讨论】:

  • 如果我每次错过它都能得到一分钱。 :)
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 2011-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多