简体   繁体   中英

Sendgrid not sending email on Heroku production Rails App

I'm using Devise for user authentication, SendGrid to send emails, and Heroku to host my Rails app.

The problem is that SendGrid should be sending a confirmation email when a user signs up, and a welcome email after the user has confirmed, but SendGrid is not sending these emails out although I can see that Heroku is dispatching the email through the Heroku logs.

mailers/welcome_email.rb

class WelcomeEmail < ApplicationMailer

    def welcome_email(user)
        @user = user
        mail to: @user.email, subject: "Thanks for joining!", from: "info@app.com"
    end

end

models/user.rb

class User < ApplicationRecord
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable, :confirmable

    def after_confirmation
        WelcomeEmail.welcome_email(self).deliver
    end

end

views/welcome_email.html.erb

<h1>This is the welcome email!</h1>

config/environments/development.rb

Rails.application.configure do

  config.action_mailer.perform_deliveries = true
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

end

config/environments/production.rb

Rails.application.configure do
  # for emails to go out
  config.action_mailer.default_url_options = { host: 'appname.herokuapp.com', protocol: 'https' }
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.perform_deliveries = true
  config.action_mailer.smtp_settings = {
    :user_name => ENV['SENDGRID_USERNAME'],
    :password => ENV['SENDGRID_PASSWORD'],
    :domain => 'herokuapp.com',
    :address => 'smtp.sendgrid.net',
    :port => 587,
    :authentication => :plain,
    :enable_starttls_auto => true
  }
end

Heroku logs

INFO: Rendering devise/mailer/reset_password_instructions.html.erb
INFO: Rendered devise/mailer/reset_password_instructions.html.erb
DEBUG: Devise::Mailer#reset_password_instructions: processed outbound mail
INFO: Sent mail to email@gmail.com
DEBUG: Date: 16 Feb 

From: info@app.com
Reply-To: info@app.com
To: email@gmail.com
Subject: Reset password
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
...
Hello user!
...

INFO: Redirected to https://app.herokuapp.com
Completed 302 Found in 267ms

I have looked around SO, blog posts, videos, and guides without any success. Can someone help me out please?

Do you want an email to be sent right away? Try deliver.now instead of deliver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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