Skip to content

A platform for community discussion. Free, open, simple.

Rake mail test debugging (#6171)

Jay Pfaffmanb55d9e63a09f
diff --git a/lib/tasks/emails.rake b/lib/tasks/emails.rake
index c4258bd9d32..45e8dd9c256 100644
--- a/lib/tasks/emails.rake
+++ b/lib/tasks/emails.rake
@@ -55,9 +55,132 @@ task "emails:import" => :environment do
end
end
-desc 'Send email test message'
+desc "Check if SMTP connection is successful and send test message"
task 'emails:test', [:email] => [:environment] do |_, args|
email = args[:email]
+ message = "OK"
+ begin
+ smtp=Discourse::Application.config.action_mailer.smtp_settings
+
+ if smtp[:address].match(/smtp\.gmail\.com/)
+ puts "
+
+#{smtp}
+============================== WARNING ==============================
+
+Sending mail with Gmail is a violation of their terms of service.
+
+Sending with G Suite might work, but it is not recommended. For information see:
+https://meta.discourse.org/t/dscourse-aws-ec2-g-suite-troubleshoting/62931?u=pfaffman
+
+========================= CONTINUING TEST ============================
+"
+ end
+
+ puts "Testing sending to #{email} using #{smtp[:user_name]}:#{smtp[:password]}@#{smtp[:address]}:#{smtp[:port]}."
+
+ Net::SMTP.start(smtp[:address], smtp[:port])
+ .auth_login(smtp[:user_name], smtp[:password])
+ rescue Exception => e
+
+
+
+
+
+ if e.to_s.match(/execution expired/)
+ message = "
+
+======================================== ERROR ========================================
+Connection to port #{ENV["DISCOURSE_SMTP_PORT"]} failed.
+====================================== SOLUTION =======================================
+The most likely problem is that your server has outgoing SMTP traffic blocked.
+If you are using a service like Mailgun or Sendgrid, try using port 2525.
+=======================================================================================
+
+"
+ elsif e.to_s.match(/535/)
+ message = "
+
+======================================== ERROR ========================================
+ AUTHENTICATION FAILED
+
+#{e}
+
+====================================== SOLUTION =======================================
+The most likely problem is that your SMTP username and/or Password is incorrect.
+Check them and try again.
+=======================================================================================
+
+"
+ elsif e.to_s.match(/Connection refused/)
+ message = "
+
+======================================== ERROR ========================================
+ CONNECTION REFUSED
+
+#{e}
+
+====================================== SOLUTION =======================================
+The most likely problem is that you have chosen the wrong port or a network problem is
+blocking access from the Docker container.
- Email::Sender.new(TestMailer.send_test(email), :test_message).send
+Check the port and your networking configuration.
+=======================================================================================
+
+"
+ elsif e.to_s.match(/service not known/)
+ message = "
+
+======================================== ERROR ========================================
+ SMTP SERVER NOT FOUND
+
+#{e}
+
+====================================== SOLUTION =======================================
+The most likely problem is that the host name of your SMTP server is incorrect.
+Check it and try again.
+=======================================================================================
+
+"
+ else
+ message = "
+
+======================================== ERROR ========================================
+ UNEXPECTED ERROR
+
+#{e}
+
+====================================== SOLUTION =======================================
+This is not a common error. No recommended solution exists!
+
+Please report the exact error message above. (And a solution, if you find one!)
+=======================================================================================
+
+"
+ end
+ end
+ if message == "OK"
+ puts "SMTP server connection successful."
+ else
+ puts message
+ exit
+ end
+ begin
+ puts "Sending to #{email}. . . "
+ Email::Sender.new(TestMailer.send_test(email), :test_message).send
+ rescue
+ puts "Sending mail failed."
+ else
+ puts "
+Mail accepted by SMTP server.
+
+If you do not receive the message, check your SPAM folder
+or test again using a service like http://www.mail-tester.com/.
+
+If the message is not delivered it is not a problem with Discourse.
+
+Check the SMTP server logs to see why it failed to deliver the message.
+
+"
+ end
end