Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

ruby on rails - How to configure ActionMailer to use SendGrid's V2 API?

Authenticating with username and password will no longer be supported as of Jan 20, 2021. How can I configure my rails app to authenticate with api key? I am using Heroku.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, login to your SendGrid account and generate a new api key. Give it the appropriate permissions for your app. https://app.sendgrid.com/settings/api_keys

Then edit your config/environments/production.rb file.

From something like this:

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

To this:

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

Notice we change the user_name to 'apikey', and password to the api key stored in your environment variables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...