Skip to content

Commit

Permalink
Fix: Better TestMailer with checks for local mail server. (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
pglombardo authored Sep 24, 2024
1 parent c9d1f8b commit 7c6b35e
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions app/mailers/test_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,73 @@ class TestMailer < ApplicationMailer
def send_test_email(email)
puts ""
puts "--> Configured FROM: address: '#{Settings.mail.mailer_sender}'"
raise StandardError, "No SMTP address provided: smtp_address" if Settings.mail.smtp_address.nil?
raise StandardError, "No SMTP port provided: smtp_port" if Settings.mail.smtp_port.nil?
raise StandardError, "No SMTP username provided: smtp_user_name" if Settings.mail.smtp_user_name.nil?
raise StandardError, "No SMTP password provided: smtp_password" if Settings.mail.smtp_password.nil?
raise StandardError, "No host domain provided: host_domain" if Settings.host_domain.nil?
raise StandardError, "No host domain protocol provided: host_protocol" if Settings.host_protocol.nil?

if Settings.mail.smtp_address.nil?
puts "--> No SMTP address provided. Mail subsystem should default to localhost."
end

if Settings.mail.smtp_port.nil?
puts "--> No SMTP port provided. Mail subsystem should default to 25."
else
puts "--> Default port is #{Settings.mail.smtp_port} but will only be used if smtp_address is set."
end

if Settings.mail.mailer_sender.nil?
raise StandardError, "mailer_sender is not set. This is the required 'from' address when sending email."
end

if Settings.mail.smtp_authentication.present?
puts "--> SMTP authentication is requested & enabled."
raise StandardError, "No SMTP username provided: smtp_user_name" if Settings.mail.smtp_user_name.nil?
raise StandardError, "No SMTP password provided: smtp_password" if Settings.mail.smtp_password.nil?
end

if Settings.host_domain.nil?
raise StandardError, "No host domain provided: host_domain. This is required to create fully qualified URLs in emails."
end

if Settings.host_protocol.nil?
raise StandardError, "No host domain protocol provided: host_protocol. This is required to create fully qualified URLs in emails."
end

puts ""
puts "The settings.yml mail configuration is:"
puts "----------"
Settings.mail.each do |key, value|
if key.to_s.include?("password")
puts "#{key}: [HIDDEN]"
else
puts "#{key}: #{value}"
end
end
puts "----------"
puts ""

puts "The Mail subsystem SMTP configuration is:"
puts "----------"
Rails.application.config.action_mailer.smtp_settings.each do |key, value|
if key.to_s.include?("password")
puts "#{key}: [HIDDEN]"
else
puts "#{key}: #{value}"
end
end
puts "----------"
puts ""

if Settings.mail.raise_delivery_errors
puts "--> raise_delivery_errors is set to true in the configuration. This will raise an error if the email fails to send."
else
puts "--> raise_delivery_errors is set to false. Set to true to see errors if the email fails to send."
end

puts ""
puts "--> Attempting to send a test email to #{email}..."
mail(to: email,
subject: "Test Email from Password Pusher",
body: "⭐ If you are reading this, sending email works! ⭐")
body: "⭐ If you are reading this, sending email works! ⭐ ")

puts "--> It seems that the Email sent successfully! Check destination inbox for the test email."
puts "--> It seems that the Email was accepted by the SMTP server! Check destination inbox for the test email."
puts ""

puts "--> If you see an error, please paste this output into a GitHub issue for help."
Expand Down

0 comments on commit 7c6b35e

Please sign in to comment.