0

I am trying to implement sendgrid into my backend api rails system so that when a user signs up I can send them a welcome email. After making a post request and handling user creation, I get this verification:

UserMailer#send_sign_up_email: processed outbound mail in 43.5ms
Sent mail to *******@gmail.com (185.8ms)
Date: Wed, 28 Feb 2018 16:54:05 -0800
From: *******@gmail.com
To: *******@gmail.com
Message-ID: <5a974f2d39c92_c5b2abcd76769fc423e0@albert-VirtualBox.mail>
Subject: Welcome to BottlesTonight albert jin!
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: quoted-printable

My code looks exactly like in this link https://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.html.

This looks all fine and well, but the email is just not sending (I put stars here for the email but I actually put in my email, and I used another one of emails as the default for sending). There is no email in my outbox or inbox.

However, now that I think about it, I never "logged in" with my email or entered the password, so the application shouldn't have access to send emails with my email. I also never did anything with the api key that I made on my sendgrid account. Furthermore, for the environment.rb file, I wasn't sure what to put in domain, so I put gmail.com. These all seem kinda sketchy to me, I think the tutorial doesn't contain everything. Does anyone know how to configure this? I've been stuck on it for a while.

Edit: I tried doing it on production and it is not working. Here is more info: My production.rb looks like this:

config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method = :smtp

  config.action_mailer.default_url_options = { :host => ENV['DEFAULT_HOST'] }

  ActionMailer::Base.smtp_settings = {
    :address        => 'smtp.sendgrid.net',
    :port           => '587',
    :authentication => :plain,
    :user_name      => ENV['SENDGRID_USERNAME'],
    :password       => ENV['SENDGRID_PASSWORD'],
    :domain         => 'heroku.com',
    :enable_starttls_auto => true
  }

I have a heroku sendgrid add on. I have set the heroku config vars. In my registrations controller I merely added the line:

UserMailer.send_sign_up_email(@current_user).deliver

My mailer class looks like:

def send_sign_up_email(user)
   @user = user
   mail(to: @user.email, subject: "Welcome! #{@user.first_name}")
   end

However, when I sign up on my website, the user gets added to the database but the email is not sending. Does anyone know why, or how I can debug?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Albert Jin
  • 51
  • 1
  • 5
  • Don't write a link to your code. Write the code here. If it is long, then extract the minimum part that is relevant to your question here. – sawa Mar 01 '18 at 04:55
  • post you `development.rb` file where you setup mailer. – Pardeep Saini Mar 01 '18 at 05:10
  • Please post your relevant mailer configuration for the environment in question. Is this a problem in development on your machine or on production on a server? – spickermann Mar 01 '18 at 05:11
  • Hi! I edited my post, is that better? – Albert Jin Mar 01 '18 at 06:50
  • Go to the rails console and set `user = User.last` then run `UserMailer.send_sign_up_email(user).deliver` - does it pop up with the email correctly? – kuwantum Mar 01 '18 at 07:34
  • No it doesn't :(. I think using the console is on the development server though, and I only set up the production.rb file – Albert Jin Mar 01 '18 at 10:18
  • Can you try setting `config.action_mailer.perform_deliveries = true` in your environment file and restarting the server and then try. The faster way to get quick feedback is to test out sending email from rails console as is suggested by @kuwantum. – Jignesh Gohel Mar 01 '18 at 12:47

1 Answers1

0

I would suggest to remove all config for ActionMailer from your environment files (i.e. files under /config/environments), except following ones

  # Don't care if the mailer can't send.
  config.action_mailer.perform_caching = false

  # Reference: http://stackoverflow.com/a/20770131/936494
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

Then create an initializer /config/initializers/mail_config.rb and add following code to it:

TEST_ENVS = %w(test)
FILESYSTEM_ENVS = TEST_ENVS + %w(development)

# ===========  DELIVERY METHOD SETTING
delivery_method = case Rails.env.to_sym
                    when :production, :staging, :experimental
                     :sendmail
                    when :test
                     :test
                    else
                     :smtp
                  end

ActionMailer::Base.delivery_method = delivery_method

# ===========  SMTP SETTINGS
ENVS_TO_USE_GMAIL_CONFIG = %w(development test)

gmail_config = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'gmail.com',
  user_name:            ENV['MAIL_USER_NAME'],
  password:             ENV['MAIL_PASSWORD'],
  authentication:       :plain,
  enable_starttls_auto: true
}

if :smtp == delivery_method
  use_gmail_config = ENVS_TO_USE_GMAIL_CONFIG.include?(Rails.env)

  smtp_settings = ( use_gmail_config ? gmail_config : {} )
  ActionMailer::Base.smtp_settings = smtp_settings
end

# =========== DEFAULT URL OPTIONS

default_url_options_settings = Settings.default_url_options
host = default_url_options_settings.host
port = default_url_options_settings.port
protocol = default_url_options_settings.protocol

default_url_options = {}
default_url_options[:host] = host if host.present?
default_url_options[:port] = port if port.present?
default_url_options[:protocol] = protocol if protocol.present?

ActionMailer::Base.default_url_options = default_url_options

The Settings object is available as part of using config gem. If you do not want to use that gem for configuring env-specific values then you can just hard-code them in the initializer and try it.

My /config/settings.yml looks like

default_url_options:
  host: ''
  port: ''
  protocol: ''

and my /config/settings/development.yml looks like

default_url_options:
  host: 'localhost'
  port: '3000'

Having a centralized mailer config helps in diagnosing mailer settings related issues in quick manner.

First try it for Gmail account and if it works you can be sure that sending email works. Just make sure in your Gmail account Less Secure Apps setting is enabled.

Jignesh Gohel
  • 6,236
  • 6
  • 53
  • 89