当新用户使用设计

时间:2017-06-19 02:41:33

标签: ruby-on-rails devise

我有用户设计的用户模型,我添加了邮件程序,当用户注册时向自己发送电子邮件。当我运行我的代码时,我没有得到任何错误。在命令行中,它表示电子邮件已发送但我没有得到任何结果。你认为这是什么原因。

我的welcome_mailer:

class WelcomeMailer < ApplicationMailer

    default :from => "jasonbig@gmail.com"

    def welcome_send(user)
        @user = user
        mail to: "jasonbig@gmail.com", subject: "New user added", from: "jasonbig@gmail.com"
    end

end

应用程序邮件:

class ApplicationMailer < ActionMailer::Base
  default from: "jasonbig@gmail.com"
  layout 'mailer'
end

我的用户模型:

class User < ActiveRecord::Base

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  after_create :welcome_send

  def welcome_send
    WelcomeMailer.welcome_send(self).deliver
  end

end

Welcome_mailer视图(welcome_send.html.erb):

<h1>Hello, <%= @user.email %> has been added</h1>
配置中的

development.rb(可能错误在smtp设置中):

Rails.application.configure do


  config.cache_classes = false

  config.eager_load = false


  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false
  config.action_mailer.delivery_method = :smtp

  # these options are only needed if you choose smtp delivery
  config.action_mailer.smtp_settings = {
    :address        => 'smtp.gmail.com',
    :port           => 25,
    :domain         => 'gmail.com',
    :authentication => :login,
    :user_name      => 'jasonbig',
    :password       => 'I have entered my actual password here'
  }

  config.action_mailer.raise_delivery_errors = true

  config.action_mailer.perform_deliveries = true

  config.active_support.deprecation = :log

  config.active_record.migration_error = :page_load

  config.assets.debug = true

  config.assets.digest = true

  config.assets.raise_runtime_errors = true

end

这些是我与ActionMailer相关的唯一文件。如果我必须添加更多文件或更改内容,请告诉我。

我的命令行电子邮件已发送确认:

3 个答案:

答案 0 :(得分:0)

尝试:

config.action_mailer.smtp_settings = {  
  :address        => 'smtp.gmail.com',  
  :port           => 587,
  :domain         => 'gmail.com',  
  :authentication => :login,  
  :user_name      => 'jasonbig',  
  :password       => 'I have entered my actual password here'  
  :enable_starttls_auto => true
}

答案 1 :(得分:0)

关注Rails Guide

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'example.com',
  user_name:            '<username>',
  password:             '<password>',
  authentication:       'plain',
  enable_starttls_auto: true  
}

我会添加这个配置:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true

看看你是否在gmail中启用了2F身份验证

答案 2 :(得分:0)

尝试

:authentication => :plain,

此外,如果您的Gmail帐户具有双因素身份验证,则需要按照本文档中的说明操作: https://support.google.com/accounts/answer/6010255?hl=en 并替换

:password => LESS_SECURE_APP_GMAIL_PASSWORD,