1
votes

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: "[email protected]"
    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 protected]
DEBUG: Date: 16 Feb 

From: [email protected]
Reply-To: [email protected]
To: [email protected]
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?

1

1 Answers

0
votes

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