diff --git a/.env_sample b/.env_sample index 0645a5ca0..4ed89fac4 100644 --- a/.env_sample +++ b/.env_sample @@ -155,9 +155,13 @@ STACK_EXCHANGE_APP_KEY="" GITHUB_KEY="Optional" GITHUB_SECRET="Optional" -# For Sendgrid email -# https://sendgrid.com/docs/for-developers/sending-email/upgrade-your-authentication-method-to-api-keys/#upgrade-to-api-keys-for-your-smtp-integration -SENDGRID_API_KEY="Optional, Production only" +# For SMTP configuration +SMTP_ADDRESS= +SMTP_PORT= +SMTP_DOMAIN= +SMTP_USER_NAME= +SMTP_PASSWORD= +SMTP_AUTHENTICATION= # Disable simplecov coverage testing when running rspec locally diff --git a/config/environments/development.rb b/config/environments/development.rb index 26ff83cb1..407afbc72 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -14,8 +14,7 @@ Rails.application.configure do # Show full error reports. config.consider_all_requests_local = true - DEFAULT_EXPIRATION = 1.hour.to_i.freeze - config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"], expires_in: DEFAULT_EXPIRATION } + config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"], expires_in: 1.hour.to_i } # Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. @@ -90,13 +89,12 @@ Rails.application.configure do config.action_mailer.perform_deliveries = true config.action_mailer.default_url_options = { host: config.app_domain } config.action_mailer.smtp_settings = { - address: "smtp.gmail.com", - port: "587", - enable_starttls_auto: true, - user_name: '<%= ENV["DEVELOPMENT_EMAIL_USERNAME"] %>', - password: '<%= ENV["DEVELOPMENT_EMAIL_PASSWORD"] %>', - authentication: :plain, - domain: config.app_domain + address: ENV["SMTP_ADDRESS"], + port: ENV["SMTP_PORT"], + authentication: ENV["SMTP_AUTHENTICATION"].presence || :plain, + user_name: ENV["SMTP_USER_NAME"], + password: ENV["SMTP_PASSWORD"], + domain: ENV["SMTP_DOMAIN"].presence || config.app_domain } config.action_mailer.preview_path = Rails.root.join("spec/mailers/previews") diff --git a/config/environments/production.rb b/config/environments/production.rb index 9a26dbf5c..efa428577 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -129,15 +129,25 @@ Rails.application.configure do config.action_mailer.delivery_method = :smtp config.action_mailer.perform_deliveries = true config.action_mailer.default_url_options = { host: protocol + ENV["APP_DOMAIN"].to_s } - ActionMailer::Base.smtp_settings = { - address: "smtp.sendgrid.net", - port: "587", - authentication: :plain, - user_name: "apikey", - password: ENV["SENDGRID_API_KEY"], - domain: ENV["APP_DOMAIN"], - enable_starttls_auto: true - } + ActionMailer::Base.smtp_settings = if ENV["SENDGRID_API_KEY"].present? + { + address: "smtp.sendgrid.net", + port: 587, + authentication: :plain, + user_name: "apikey", + password: ENV["SENDGRID_API_KEY"], + domain: ENV["APP_DOMAIN"] + } + else + { + address: ENV["SMTP_ADDRESS"], + port: ENV["SMTP_PORT"], + authentication: ENV["SMTP_AUTHENTICATION"], + user_name: ENV["SMTP_USER_NAME"], + password: ENV["SMTP_PASSWORD"], + domain: ENV["SMTP_DOMAIN"] + } + end if ENV["HEROKU_APP_URL"].present? && ENV["HEROKU_APP_URL"] != ENV["APP_DOMAIN"] config.middleware.use Rack::HostRedirect, diff --git a/docs/backend/emails.md b/docs/backend/emails.md index c9c2a5aa8..6ef137614 100644 --- a/docs/backend/emails.md +++ b/docs/backend/emails.md @@ -2,6 +2,24 @@ title: Emails --- +# Setting up email + +If you would like to enable transactional email using services like SendGrid or +Mailgun, you can configure it by using the following environment variables: + +```shell +SMTP_ADDRESS= # ie. "smtp.sendgrid.net" +SMTP_PORT= # ie. 587 +SMTP_DOMAIN= +SMTP_USER_NAME= +SMTP_PASSWORD= +SMTP_AUTHENTICATION= # defaults to :plain +``` + +We follow the standard `ActionMailer` configuration. For more info, please check out +Rails' +[official documentation](https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-configuration). + # Previewing emails in development You can view email previews at . @@ -10,4 +28,7 @@ Previews are setup in the directory `spec/mailers/previews`. # Overriding mailer templates -To update the contents of emails that the app sends, edit the views under `app/views/mailers`. Note that this app uses the [`devise_invitable` gem](https://github.com/scambra/devise_invitable) for invitations. The views for this gem are stored under `app/views/devise/mailer`. +To update the contents of emails that the app sends, edit the views under +`app/views/mailers`. Note that this app uses the +[`devise_invitable` gem](https://github.com/scambra/devise_invitable) for +invitations. The views for this gem are stored under `app/views/devise/mailer`.