Make email settings configurable, add custom mailer for one-off emails #107

Merged
greg merged 3 commits from feature/custom_mailer into master 2023-03-23 15:52:43 +00:00
7 changed files with 78 additions and 20 deletions

View File

@ -1,3 +1,12 @@
SMTP_SERVER=smtp.example.com
SMTP_PORT=587
SMTP_LOGIN=accounts
SMTP_PASSWORD=123abc
SMTP_FROM_ADDRESS=accounts@example.com
SMTP_DOMAIN=example.com
SMTP_AUTH_METHOD=plain
SMTP_ENABLE_STARTTLS=auto
LDAP_HOST=localhost LDAP_HOST=localhost
LDAP_PORT=389 LDAP_PORT=389
LDAP_ADMIN_PASSWORD=passthebutter LDAP_ADMIN_PASSWORD=passthebutter
@ -24,4 +33,3 @@ LNDHUB_PG_PORT=5432
LNDHUB_PG_DATABASE=lndhub LNDHUB_PG_DATABASE=lndhub
LNDHUB_PG_USERNAME=lndhub LNDHUB_PG_USERNAME=lndhub
LNDHUB_PG_PASSWORD='' LNDHUB_PG_PASSWORD=''

View File

@ -1,4 +1,3 @@
class ApplicationMailer < ActionMailer::Base class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer' layout 'mailer'
end end

View File

@ -0,0 +1,23 @@
# A custom mailer that can be used from the Rails console for one-off emails
# today, and later connected from an admin panel mailing page.
#
# Assign any template variables you want to use:
#
# user = User.first
#
# Create the email body from a custom email template file:
#
# body = ERB.new(File.read('./tmp/mailer-1.txt.erb')).result binding
#
# Send email via Sidekiq:
#
# CustomMailer.with(user: u, subject: "Important announcement", body: body).custom_message.deliver_later
#
class CustomMailer < ApplicationMailer
def custom_message
@user = params[:user]
@subject = params[:subject]
@body = params[:body]
mail(to: @user.email, subject: @subject)
end
end

View File

@ -0,0 +1 @@
<%= @body %>

View File

@ -2,9 +2,7 @@
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style> <style></style>
/* Email styles need to be inline */
</style>
</head> </head>
<body> <body>

View File

@ -57,10 +57,14 @@ Rails.application.configure do
# routes, locales, etc. This feature depends on the listen gem. # routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.action_mailer.default_options = {
from: "accounts@localhost"
}
# Don't actually send emails, cache them for viewing via letter opener # Don't actually send emails, cache them for viewing via letter opener
config.action_mailer.delivery_method = :letter_opener config.action_mailer.delivery_method = :letter_opener
# Don't care if the mailer can't send # Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false config.action_mailer.raise_delivery_errors = true
# Base URL to be used by email template link helpers # Base URL to be used by email template link helpers
config.action_mailer.default_url_options = { host: "localhost:3000", protocol: "http" } config.action_mailer.default_url_options = { host: "localhost:3000", protocol: "http" }

View File

@ -57,28 +57,53 @@ Rails.application.configure do
# config.active_job.queue_adapter = :resque # config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "akkounts_production" # config.active_job.queue_name_prefix = "akkounts_production"
config.action_mailer.perform_caching = false # E-mail settings, adapted from https://github.com/mastodon/mastodon
config.action_mailer.delivery_method = :smtp outgoing_email_address = ENV.fetch('SMTP_FROM_ADDRESS', 'accounts@localhost')
outgoing_email_domain = Mail::Address.new(outgoing_email_address).domain
config.action_mailer.default_options = {
from: outgoing_email_address,
message_id: -> { "<#{Mail.random_tag}@#{outgoing_email_domain}>" },
}
config.action_mailer.default_options[:reply_to] = ENV['SMTP_REPLY_TO'] if ENV['SMTP_REPLY_TO'].present?
config.action_mailer.default_options[:return_path] = ENV['SMTP_RETURN_PATH'] if ENV['SMTP_RETURN_PATH'].present?
enable_starttls = nil
enable_starttls_auto = nil
case ENV['SMTP_ENABLE_STARTTLS']
when 'always'
enable_starttls = true
when 'never'
enable_starttls = false
when 'auto'
enable_starttls_auto = true
else
enable_starttls_auto = ENV['SMTP_ENABLE_STARTTLS_AUTO'] != 'false'
end
config.action_mailer.smtp_settings = { config.action_mailer.smtp_settings = {
address: "mail.gandi.net", port: ENV['SMTP_PORT'],
port: "587", address: ENV['SMTP_SERVER'],
authentication: "plain", user_name: ENV['SMTP_LOGIN'].presence,
enable_starttls_auto: true, password: ENV['SMTP_PASSWORD'].presence,
user_name: Rails.application.credentials.smtp[:username], domain: ENV['SMTP_DOMAIN'] || ENV['LOCAL_DOMAIN'],
password: Rails.application.credentials.smtp[:password] authentication: ENV['SMTP_AUTH_METHOD'] == 'none' ? nil : ENV['SMTP_AUTH_METHOD'] || :plain,
ca_file: ENV['SMTP_CA_FILE'].presence || '/etc/ssl/certs/ca-certificates.crt',
openssl_verify_mode: ENV['SMTP_OPENSSL_VERIFY_MODE'],
enable_starttls: enable_starttls,
enable_starttls_auto: enable_starttls_auto,
tls: ENV['SMTP_TLS'].presence && ENV['SMTP_TLS'] == 'true',
ssl: ENV['SMTP_SSL'].presence && ENV['SMTP_SSL'] == 'true',
} }
config.action_mailer.default_url_options = { config.action_mailer.delivery_method = ENV.fetch('SMTP_DELIVERY_METHOD', 'smtp').to_sym
host: "accounts.kosmos.org",
protocol: "https",
from: "accounts@kosmos.org"
}
# Ignore bad email addresses and do not raise email delivery errors. # 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. # Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false config.action_mailer.raise_delivery_errors = true
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found). # the I18n.default_locale when a translation cannot be found).