2017-09-13 176 views
0

我正在使用Rails 5並試圖使用Gmail作爲中繼從我的開發計算機發送一些電子郵件。我有這樣的郵件文件,應用程序/郵寄/ user_notifier.rb如何使用Gmail從Rails發送電子郵件?

class UserNotifier < ActionMailer::Base 
    default from: RAILS_FROM_EMAIL 

    # send notification email to user about the price 
    def send_notification(user_notification, crypto_price) 
    puts "user notification: #{user_notification.id}" 
    @user = user_notification.user 
    @crypto_price = crypto_price 
    threshhold = user_notification.buy ? 'above' : 'below' 
    puts "user: #{@user.email} currency: #{@user.currency}" 
    mail(:to => @user.email, 
    :subject => sprintf(Constants::USER_NOTIFICATION_SUBJECT, crypto_price.crypto_currency.name, threshhold, PriceHelper.format_price(user_notification.price, @user.currency)) ) 
    end 

然後我從Sidekiq工人發送電子郵件,像這樣

UserNotifier.send_notification(user_notification, price).deliver 

雖然我不看我的日誌中的任何錯誤,電子郵件從未交付(我檢查了我的垃圾郵件文件夾以驗證此)。以下是我的config/environments/development.rb文件。

# ActionMailer Config 
    config.action_mailer.smtp_settings = { 
    address:    'smtp.gmail.com', 
    port:     587, 
    domain:    'mybox.devbox.com', 
    user_name:   'myusertest1', 
    password:    'myuser99999', 
    authentication:  'plain', 
    enable_starttls_auto: true 
    } 
    config.action_mailer.delivery_method = :smtp 
    # change to true to allow email to be sent during development 
    config.action_mailer.perform_deliveries = true 
    config.action_mailer.raise_delivery_errors = true 
    config.action_mailer.default :charset => "utf-8" 

任何想法什麼可能會出錯或如何我可以進一步排除故障?

回答

1

我相信Rails的5,正確的語法爲

UserNotifier.send_notification(user_notification, price).deliver_now

...並使用完整的電子郵件作爲用戶名。

+0

給了一個旋轉,但沒有做'。仍然不會收到任何電子郵件。 – Dave

+0

添加了編輯。我使用Gmail作爲中繼,並使用完整的電子郵件作爲用戶名。值得一試。 –

+0

謝謝,我沒有想到這一點。我仍然沒有收到電子郵件(即使是垃圾郵件) - 有什麼方法可以瞭解發生了什麼?日誌文件似乎沒有透露太多。 – Dave

相關問題