Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Better TestMailer with checks for local mail server. #2554

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading