From 3333a7baf339f7ff7cada82b78b2f8a44ccb9e2d Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Fri, 28 Apr 2017 17:09:39 +0100 Subject: [PATCH] add script for testing app SMTP connections --- app/mailers/app_mailer.rb | 4 +++ app/views/app_mailer/test_message.text.erb | 3 +++ bin/postal | 4 +++ config/environments/development.rb | 2 +- config/environments/production.rb | 2 +- script/test_app_smtp.rb | 29 ++++++++++++++++++++++ 6 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 app/views/app_mailer/test_message.text.erb create mode 100755 script/test_app_smtp.rb diff --git a/app/mailers/app_mailer.rb b/app/mailers/app_mailer.rb index a64ed32..25e5fb6 100644 --- a/app/mailers/app_mailer.rb +++ b/app/mailers/app_mailer.rb @@ -44,4 +44,8 @@ class AppMailer < ApplicationMailer mail :to => @server.organization.notification_addresses, :subject => "[#{server.full_permalink}] Your mail server has been suspended" end + def test_message(recipient) + mail :to => recipient, :subject => "Postal SMTP Test Message" + end + end diff --git a/app/views/app_mailer/test_message.text.erb b/app/views/app_mailer/test_message.text.erb new file mode 100644 index 0000000..1f9750a --- /dev/null +++ b/app/views/app_mailer/test_message.text.erb @@ -0,0 +1,3 @@ +This is a test message sent by Postal. + +If you have received this message your test has succeeded. diff --git a/bin/postal b/bin/postal index 457dc8c..b85e5bd 100755 --- a/bin/postal +++ b/bin/postal @@ -84,6 +84,10 @@ case "$1" in run "bundle exec ruby script/make_user.rb" ;; + test-app-smtp) + run "bundle exec ruby script/test_app_smtp.rb $2" + ;; + bundle) if [ -n "$2" ]; then run "bundle install --path=$2 --without=development --jobs=4 --clean" diff --git a/config/environments/development.rb b/config/environments/development.rb index e5a88d7..896ef8d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -27,7 +27,7 @@ Rails.application.configure do end # Don't care if the mailer can't send. - config.action_mailer.raise_delivery_errors = false + config.action_mailer.raise_delivery_errors = true config.action_mailer.perform_caching = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 0a479f6..42f2fee 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -55,7 +55,7 @@ Rails.application.configure do # 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. - # 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 # the I18n.default_locale when a translation cannot be found). diff --git a/script/test_app_smtp.rb b/script/test_app_smtp.rb new file mode 100755 index 0000000..1b3cc2a --- /dev/null +++ b/script/test_app_smtp.rb @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +trap("INT") { puts ; exit } + + +if ARGV[0].nil? || !(ARGV[0] =~ /@/) + puts "usage: postal test-app-smtp [email address]" + exit 1 +end + +require_relative '../config/environment' + +begin + Timeout.timeout(10) do + AppMailer.test_message(ARGV[0]).deliver + end + + puts "\e[32mMessage has been sent successfully.\e[0m" +rescue => e + puts "\e[31mMessage was not delivered successfully to SMTP server.\e[0m" + puts "Error: #{e.class} (#{e.message})" + puts + puts " SMTP Host: #{Postal.config.smtp.host}" + puts " SMTP Port: #{Postal.config.smtp.port}" + puts " SMTP Username: #{Postal.config.smtp.username}" + puts " SMTP Password: #{Postal.config.smtp.password}" + puts +rescue Timeout::Error + puts "Sending timed out" +end