Skip to content

Commit

Permalink
Send emails using standard ActionMailer, with Sendgrid only accessed …
Browse files Browse the repository at this point in the history
…over SMTP.
  • Loading branch information
DanielJackson-Oslo committed Feb 17, 2024
1 parent aad97e2 commit 684f666
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 60 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ MAPBOX_API_KEY=

# Sendgrid
SENDGRID_API_KEY=SG.*****.*****
SENDGRID_CONFIRMATION_TEMPLATE_ID=d-******
SENDGRID_RESET_TEMPLATE_ID=d-*****
SENDGRID_FROM_EMAIL=****@****-com

# Scout
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ group :development do
gem "guard"
gem "guard-livereload", require: false
gem "guard-rspec", require: false
gem "letter_opener"
gem "listen"
gem "overcommit"
gem "rack-livereload", require: false
Expand Down Expand Up @@ -92,7 +93,6 @@ gem "kaminari"
gem "paper_trail"
gem "phonelib"
gem "rails-i18n"
gem "sendgrid-ruby"
gem "simple_form"
gem "simple_form-tailwind"
gem "stimulus-rails"
Expand Down
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ GEM
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
letter_opener (1.9.0)
launchy (>= 2.2, < 3)
listen (3.8.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -453,7 +457,6 @@ GEM
ruby-vips (2.2.0)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
ruby_http_client (3.5.5)
rubyzip (2.3.2)
scout_apm (5.3.5)
parser
Expand All @@ -462,8 +465,6 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sendgrid-ruby (6.7.0)
ruby_http_client (~> 3.4)
shellany (0.0.1)
simple_form (5.3.0)
actionpack (>= 5.2)
Expand Down Expand Up @@ -552,6 +553,7 @@ DEPENDENCIES
jbuilder (~> 2.7)
jsbundling-rails (~> 1.2)
kaminari
letter_opener
listen
newrelic_rpm
omniauth
Expand All @@ -577,7 +579,6 @@ DEPENDENCIES
rubocop-rspec
scout_apm
selenium-webdriver
sendgrid-ruby
simple_form
simple_form-tailwind
sprockets-rails
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ You can also set the ENV variable ["SEED_FILE"](https://github.com/lnu-norge/lok

## Sendgrid setup

You need to set the right sendgrip enviroment variables, and to get sendgrid to work you will also need to set the ENV variable ["HOST"] to equal to the domain you are using, example: `ENV["HOST"] = "app.herokuapp.com`
You need to set the right sendgrid environment variables, and to get sendgrid to work you will also need to set the ENV variable ["HOST"] to equal to the domain you are using, example: `ENV["HOST"] = "app.herokuapp.com`

# Want to learn to code or learn Rails?
---
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: "[email protected]"
default from: ENV["SENDGRID_FROM_EMAIL"] || "[email protected]"
layout "mailer"
end
12 changes: 0 additions & 12 deletions app/mailers/devise_mailer.rb

This file was deleted.

29 changes: 0 additions & 29 deletions app/mailers/sendgrid_mailer.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */

</style>
</head>

Expand Down
8 changes: 5 additions & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
# Store uploaded files on the local file system (see config/storage.yml for options).
config.active_storage.service = :local

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

# This will open all sent emails in a new tab in the browser
config.action_mailer.delivery_method = :letter_opener
config.action_mailer.default_url_options = { host: "localhost:3000" }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
Expand Down
23 changes: 19 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "active_support/core_ext/integer/time"

Rails.application.configure do
Rails.application.configure do # rubocop:disable Metrics/BlockLength
# Settings specified here will take precedence over those in config/application.rb.

# No need to compile in prod, as all assets are precompiled on deploy:
Expand Down Expand Up @@ -64,7 +64,25 @@
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "lokaler_lnu_no_production"

# Set up URL
Rails.application.routes.default_url_options[:host] =
ENV["DEFAULT_HOST"] || "#{ENV.fetch('HEROKU_APP_NAME')}.herokuapp.com"

# ActionMailer setup: Sendgrid over SMTP
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { host: Rails.application.routes.default_url_options[:host] }
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_caching = false
ActionMailer::Base.smtp_settings = {
user_name: "apikey", # NB: This is the string literal 'apikey', NOT the ID of your API key. Do not change this.
password: ENV.fetch(SENDGRID_API_KEY),
domain: "lnu.no", # We should probably have our own sub domain for transactional emails eventually
address: "smtp.sendgrid.net",
port: 587,
authentication: :plain,
enable_starttls_auto: true
}

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
Expand Down Expand Up @@ -99,9 +117,6 @@
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

Rails.application.routes.default_url_options[:host] =
ENV["DEFAULT_HOST"] || "#{ENV.fetch('HEROKU_APP_NAME')}.herokuapp.com"

# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
# Configure the e-mail address which will be shown in Devise::Mailer,
# note that it will be overwritten if you use your own mailer class
# with default "from" parameter.
config.mailer_sender = 'no-reply@lnu.no'
config.mailer_sender = ENV["SENDGRID_FROM_EMAIL"] || "lokaler@lnu.no"

# Configure the class responsible to send e-mails.
config.mailer = 'DeviseMailer'
# config.mailer = 'Devise::Mailer'

# Configure the parent class responsible to send e-mails.
# config.parent_mailer = 'ActionMailer::Base'
Expand Down

0 comments on commit 684f666

Please sign in to comment.