0
votes

I'm attempting to send a welcome email when a new user is created via Action Mailer. I've followed the guide here: https://guides.rubyonrails.org/action_mailer_basics.html

The email is successful when creating a user on localhost:3000, but when I deploy and test in production environment (Heroku), no success.

Even worse, I'm not seeing errors in the Heroku logs.

I'm not really sure what to check at this point. I've tried moving everything to my application.rb file since the configuration is the same (see: ActionMailer doesn't work in production)

I thought that it may be sending a delayed message, but I've already tried UserMailer.with(user: @user).welcome_email.deliver_later as well as deliver_now

config/application.rb

config.action_mailer.delivery_method = :smtp
    config.action_mailer.smtp_settings = {
      address:              'smtp.gmail.com',
      port:                 587,
      domain:               'gmail.com',
      user_name:            ENV["GMAIL_USERNAME"],
      password:             ENV["GMAIL_PASSWORD"],
      authentication:       'plain',
      enable_starttls_auto: true }

config/environments/production.rb

config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false

mailers/user_mailer

class UserMailer < ApplicationMailer
  default from: '[email protected]'

  def welcome_email
    @user = params[:user]
    @url  = 'https://pickleballsocial.herokuapp.com'
    mail(to: @user.email, subject: 'Welcome Pickleball Social')
  end
end

users_controller

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        session[:user_id] = @user.id
        # Tell the UserMailer to send a welcome email after save
        UserMailer.with(user: @user).welcome_email.deliver_later

        format.html { redirect_to(@user, notice: 'User was successfully created.') }
        format.json { render json: @user, status: :created, location: @user }

        #redirect_to user_path(@user)
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
2
I assume you've confirmed the user name and password from ENV is the same as what you tested on localhost? - lurker
@lurker wow. Thanks, it's nice to be humbled. - Austin Burke

2 Answers

0
votes

I don't see any major issues with your code. First thing I'd check is to make sure the credentials are correct. If that's not the issue, without log information, I'm not sure of the problem.

0
votes

From the guides

Note: As of July 15, 2014, Google increased its security measures and now blocks attempts from apps it deems less secure. You can change your Gmail settings here to allow the attempts. If your Gmail account has 2-factor authentication enabled, then you will need to set an app password and use that instead of your regular password. Alternatively, you can use another ESP to send email by replacing 'smtp.gmail.com' above with the address of your provider.

Can you check your gmail security settings from that article link? I'm wondering if the production host is blocked because it's coming from something that might seem less secure (a hosted server, versus your local computer)