我目前正在研究我的第一個Ruby on Rails應用程序,並且發現了一個我不知道是否導致它的錯誤,或者它是否已經存在。Rails + Devise - 發送電子郵件到電子郵件+數組中的用戶名
我有Devise安裝和配置,一個用戶使用Rake和我的視圖種子正確設置。問題是,當我嘗試發送確認電子郵件時,它會嘗試發送電子郵件至:['[email protected]', 'admin']
。
正如您可能認爲的那樣,它會崩潰,因爲SMTP服務器拒絕發送包含無效地址的電子郵件。
有沒有辦法來防止這種情況發生?
這裏有錯誤產生的日誌:
Devise::Mailer#confirmation_instructions: processed outbound mail in 26.0ms
Sent mail to ["[email protected]", "admin"] (5054.3ms)
Date: Mon, 23 Nov 2015 14:45:01 -0500
From: [email protected]
Reply-To: [email protected]
To: ["[email protected]", "admin"]
Message-ID: <[email protected]>
Subject: Confirmation instructions
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<p>Welcome ["[email protected]", "admin"]!</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+\-.][email protected][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: '[email protected]', role: :admin)
admin.save!
建議您可以發佈設計模型的相關部分以及如何爲用戶創建種子? –
您是否正在嘗試寫一張支票來拯救無效郵件?或者您是否要求通過發送電子郵件的方式向開發環境發送電子郵件? –
@SunnyK事實上,我只是想發送電子郵件,但這個以用戶名作爲電子郵件地址的小錯誤令人討厭。 –