Home>

In Chapter 11 of the Rails tutorial, an error was received that emails were not sent from SendGrid.

After registering a new user in Cloud 9,Please check your email to activate your accountis displayed properly on the screen, but in the Heroku production environment,We're Sorry, but something went wrong.

Can someone give me advice? I've been struggling with this error for two days,
I tried a lot and tried, but I don't know the solution.

Error message

$heroku logs

2018-08-27T00: 47: 30.841589 + 00: 00 app [web.1]: I, [2018-08-27T00: 47: 30.841521 # 6] INFO-: [a6255093-4c52- 4e32-9cd6-8a4216580976] Completed 500 Internal Server Error in 702ms (ActiveRecord: 11.7ms)
2018-08-27T00: 47: 30.842539 + 00: 00 app [web.1]: F, [2018-08-27T00: 47: 30.842467 # 6] FATAL-: [a6255093-4c52-4e32-9cd6-8a4216580976]
2018-08-27T00: 47: 30.842630 + 00: 00 app [web.1]: F, [2018-08-27T00: 47: 30.842569 # 6] FATAL-: [a6255093-4c52-4e32-9cd6-8a4216580976] Net :: SMTPFatalError (550 Unauthenticated senders not allowed
2018-08-27T00: 47: 30.842632 + 00: 00 app [web.1]:):
2018-08-27T00: 47: 30.842744 + 00: 00 app [web.1]: F, [2018-08-27T00: 47: 30.842663 # 6] FATAL-: [a6255093-4c52-4e32-9cd6-8a4216580976]
2018-08-27T00: 47: 30.842840 + 00: 00 app [web.1]: F, [2018-08-27T00: 47: 30.842782 # 6] FATAL-: [a6255093-4c52-4e32-9cd6-8a4216580976] app/models/user.rb: 48: in `send_activation_email '
2018-08-27T00: 47: 30.842842 + 00: 00 app [web.1]: [a6255093-4c52-4e32-9cd6-8a4216580976] app/controllers/users_controller.rb: 22: in `create '
Applicable source code

production.rb

(Omitted)
  config.active_record.dump_schema_after_migration = false
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.delivery_method =: smtp
  host = 'https: // app name.herokuapp.com'
  config.action_mailer.default_url_options = {host: host}
  ActionMailer :: Base.smtp_settings = {
    : address =>'smtp.sendgrid.net',
    : port =>'587',
    : authentication =>: plain,
    : user_name =>ENV ['[email protected] '],
    : password =>ENV ['password'],
    : domain =>'heroku.com',
    : enable_starttls_auto =>true
  }
end


app/models/user.rb

(Omitted)
  # Remember user in database for persistent sessions
  def remember
    self.remember_token = User.new_token
    update_attribute (: remember_digest, User.digest (remember_token))
  end
  def forget
    update_attribute (: remember_digest, nil)
  end
  #Returns true if the token matches the digest
  def authenticated? (attribute, token)
    digest = send ("# {attribute} _digest")
    return false if digest.nil?
    BCrypt :: Password.new (digest) .is_password? (Token)
  end
  #Enable account
  def activate
    update_columns (activated: true, activated_at: Time.zone.now)
  end
  #Send activation email
  def send_activation_email
   UserMailer.account_activation (self) .deliver_now
  end
  private
  #Create and assign an activation token and digest
  def create_activation_digest
    self.activation_token = User.new_token
    self.activation_digest = User.digest (activation_token)
  end


users_controller.rb

def create
    @user = User.new (user_params)
    if @ user.save
      @ user.send_activation_email
      flash [: info] = "Please check your email to activate your account"
      redirect_to root_url
    else
      render 'new'
    end
  end
Tried

1, Register credit card information on Heroku

2,
$heroku config: get SENDGRID_USERNAME
$heroku config: get SENDGRID_PASSWORD
The user name and password are now displayed.

3,
$heroku config: add SENDGRID_PASSWORD = password --app app name
$heroku config: add [email protected] --app app name
I also tried this

4, reboot

Supplemental information (FW/tool version etc.)

Rails5.1.4
AWS Cloud 9

  • Answer # 1

    Since it's just one round of the Ruby on Rails tutorial, I'm sorry if it's not the same.
    At the same time, I experienced an error that shows create in the HEROKU log.

    I think the source code can be completely compared with the one in the tutorial, or you can try copying and pasting it again for verification. It was the following point.
    I couldn't understand the meaning and touched various things, but it didn't go away. Finally,

    List 11.41:

    config.action_mailer.raise_delivery_errors = true
      config.action_mailer.delivery_method =: smtp
      host = '<your heroku app>.herokuapp.com'
      config.action_mailer.default_url_options = {host: 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
      }

    ,

    host = '<your heroku app>.herokuapp.com'

    just

    host = 'https: // actual content.herokuapp.com'
    Change to

    .
    At the terminal

    $heroku config: get SENDGRID_USERNAME
    $heroku config: get SENDGRID_PASSWORD

    Get it at the terminal and to the right of the thing you got at the terminal

    heroku config: set SENDGRID_USERNAME = (SENDGRID_USERNAME checked)
    heroku config: set SENDGRID_PASSWORD = (SENDGRID_PASSWORD checked)
    Use

    to set the value. The value on the right side of = does not need to be enclosed in&or ", and there was an error if there was a space before and after =.

    heroku run rails db: migrate


    In my case, the error has been resolved. I hope it will be helpful.

Related articles