0

我正在实施设计“可确认”方法,通过在 Rails App 上发送电子邮件来确认用户。我的应用程序“development.log”文件显示电子邮件已发送但未到达目的地。以下是我包含代码的文件:-

用户.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  attr_accessible :title, :body
  def password_match?
    self.errors[:password] << 'must be provided' if password.blank?
    self.errors[:password] << 'and confirmation do not match' if password != password_confirmation
    password == password_confirmation and !password.blank?
  end
end

确认控制器.rb

class ConfirmationsController < Devise::ConfirmationsController
  def show
    @user = User.find_by_confirmation_token(params[:confirmation_token])
  end
ef confirm_user
    @user = User.find_by_confirmation_token(params[:user][:confirmation_token])
    if @user.update_attributes(params[:user]) and @user.password_match?
      @user = User.confirm_by_token(@user.confirmation_token)
      set_flash_message :notice, :confirmed
      sign_in_and_redirect("user", @user)
    else
      render :show
    end
  end

注册页面

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>



  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

发展.rb

Gt::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {:address => "localhost", :port => 1025}

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  ActionMailer::Base.delivery_method = :smtp
  ActionMailer::Base.smtp_settings = {
   :tls => true,
   :address => "smtp.gmail.com",
   :port => 587,
   :domain => "gmail.com",
   :authentication => :login,
   :user_name => "[username]",
   :password => "[password]"
 }

  # Don't care if the mailer can't send
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger
  config.active_support.deprecation = :log

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true
end

日志输出

Sent mail to xy@gmail.com (2027ms)
Date: Wed, 27 Mar 2013 23:29:12 +0530
From: please-change-me-at-config-initializers-devise@example.com
Reply-To: please-change-me-at-config-initializers-devise@example.com
To: xy@gmail.com
Message-ID: <515333707f095_3501ae36c81486e@Vinay-PC.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Welcome xy@gmail.com!</p>

<p>You can confirm your account email through the link below:</p>

问题:正如我从开发日志中看到的那样,电子邮件已发送,但它没有到达给定的电子邮件 ID。我在本地主机上。任何帮助都将不胜感激。

问候, 维奈·辛格

4

2 回答 2

1

您需要在 config/application.rb 文件中设置默认主机和邮件程序设置。

Rails 的电子邮件配置

于 2013-03-27T19:07:45.250 回答
0

我也遇到了这个问题,但通过以下技巧解决了这个问题。

解决方案是在 config/initialisers/setup_mail.rb 中创建一个初始化程序,其中包含以下内容

如果 Rails.env != 'test'
email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
ActionMailer::Base.smtp_settings = email_settings[Rails.env ] 除非 email_settings[Rails.env].nil?
结尾

然后添加 config/email.yml 包含开发和生产电子邮件帐户的详细信息

开发:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy

生产:
:address: smtp.gmail.com
:port: 587
:authentication: plain
:user_name: xxx
:password: yyy

于 2013-03-28T08:43:06.353 回答