2015-11-23 131 views
0

我目前正在研究我的第一個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 [&quot;[email protected]&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+\-.][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! 
+0

建議您可以發佈設計模型的相關部分以及如何爲用戶創建種子? –

+0

您是否正在嘗試寫一張支票來拯救無效郵件?或者您是否要求通過發送電子郵件的方式向開發環境發送電子郵件? –

+0

@SunnyK事實上,我只是想發送電子郵件,但這個以用戶名作爲電子郵件地址的小錯誤令人討厭。 –

回答

1

的問題是在before_save過濾器,有效地將email字段設置爲數組。可能的替代語法是

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

或使用Ruby的多重分配

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

在一個單獨的說明,如果你想發送設計使用設計和resque在後臺郵件,我建議你去看看devise-async寶石,因爲它是devise guys themselves

+0

什麼最適合後臺作業? 'Sidekiq','Resque'或'ActiveJob'?我喜歡Sidekiq,因爲我已經在GitLab中看到它,但我不知道它是否是最好的。 –

+0

沒有銀彈,它很大程度上取決於您的應用的功能以及您計劃部署的位置。我喜歡Sidekiq,但你必須特別小心,在你的應用程序和你使用的寶石中都是線程安全的。 –

+0

那麼,我需要能夠排隊任務以及執行週期性任務。 –

相關問題