繁体   English   中英

Rails + Devise-将电子邮件发送到电子邮件+数组中的用户名

[英]Rails + Devise - Sending email to email + username in array

我目前正在开发我的第一个Ruby on Rails应用程序,但发现了一个不知道是我导致它还是已经存在的错误。

我已经安装并配置了Devise,使用Rake为用户设置了种子,并且正确设置了我的视图。 问题是,当我尝试发送确认电子邮件时,它尝试将电子邮件发送至: ['admin@domain.com', 'admin']

您可能会想,它崩溃是因为SMTP服务器拒绝发送带有无效地址的电子邮件。

有办法防止这种情况发生吗?

这是错误时生成的日志:

Devise::Mailer#confirmation_instructions: processed outbound mail in 26.0ms

Sent mail to ["admin@domain.com", "admin"] (5054.3ms)
Date: Mon, 23 Nov 2015 14:45:01 -0500
From: support@domain.com
Reply-To: support@domain.com
To: ["admin@domain.com", "admin"]
Message-ID: <56536cbda3561_55446c9740595c9@freud.mail>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Welcome [&quot;admin@domain.com&quot;, &quot;admin&quot;]!</p>

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

<p><a href="http://localhost:3000/users/confirmation?confirmation_token=z4pUucoe1GKD-mCbTkji">Confirm my account</a></p>

   (3.0ms)  ROLLBACK
Completed 500 Internal Server Error in 5092ms (ActiveRecord: 6.0ms)

Net::SMTPSyntaxError - 501 5.1.3 Invalid address

这是User类:

class User < ActiveRecord::Base
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  acts_as_paranoid

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :confirmable, :lockable, :timeoutable

  before_save { self.email = email.downcase, self.username = username.downcase }

  after_update :send_password_change_email, if: :needs_password_change_email?

  validates :username, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
  validates :role, presence: true, inclusion: { in: %w(user manager admin) }

  def is_admin?
    self.role == 'admin'
  end

  private
    def send_devise_notification(notification, *args)
      ## It crashes here!!!
      devise_mailer.send(notification, self, *args).deliver_now
    end

    def needs_password_change_email?
      encrypted_password_changed? && persisted?
    end

    def send_password_change_email
      UserMailer.password_changed(id).deliver_now
    end
end

和种子:

admin = User.new(username: 'admin', password: 'admin1234', email: 'admin@freudtech.com', role: :admin)
admin.save!

问题出在before_save过滤器中,该过滤器有效地将email字段设置为数组。 可能的替代语法是

self.email = email.downcase; self.username = username.downcase

或使用Ruby的多重分配

self.email, self.username = email.downcase, username.downcase

另外,如果您想使用devise和resque在后台发送devise电子邮件,我建议您看一下devise-async gem,这是由devise伙计们自己建议的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM