From d385ce40cb93fbc5d229de79d7a18e5521a48a4d Mon Sep 17 00:00:00 2001 From: jhs-panda Date: Sun, 29 Sep 2024 13:27:02 -0400 Subject: [PATCH] Catch SMTP Error to flash message if new user email send doesn't work. --- app/controllers/users_controller.rb | 18 +++++++++++++++--- app/models/user.rb | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dbd929dc2..cecfa83dc 100755 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -117,9 +117,21 @@ def create save_worked = false end if save_worked - # @user.send_reset_password_instructions - flash[:success] = "Successfully created user." - redirect_to(users_path) && return + begin + @user.send_reset_password_instructions + flash[:success] = "Successfully created user." + redirect_to(users_path) + rescue StandardError => e + error_message = e.message + if error_message.include?("The from address does not match a verified Sender Identity") + flash[:success] = "Successfully created user but reset password instructions not sent: + Net::SMTPFatalError" + else + flash[:error] = "Failed to create user." + @user.destroy + end + redirect_to(users_path) + end else render action: "new" end diff --git a/app/models/user.rb b/app/models/user.rb index 7619357f1..392b925d6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -188,7 +188,7 @@ def self.instructor_create(email, course_name) User.assign_random_password user - # user.send_reset_password_instructions + user.send_reset_password_instructions user end