发送电子邮件但是没有通过电子邮件确认用户的设计方法“确认”到达邮箱

时间:2013-03-27 18:19:29

标签: ruby-on-rails ruby authentication devise

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

user.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

confirmations_controller.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" %>

development.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。我在localhost上。 任何帮助都会得到满足。

此致 Vieenay Siingh

2 个答案:

答案 0 :(得分:1)

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

email configuration for rails

答案 1 :(得分: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

发展:
  :地址:smtp.gmail.com
  :port:587
  :authentication:plain
  :user_name:xxx
  :密码:yyy

生产:
  :地址:smtp.gmail.com
  :port:587
  :authentication:plain
  :user_name:xxx
  :密码:yyy

相关问题