diff --git a/app/helpers/api/v1/inboxes_helper.rb b/app/helpers/api/v1/inboxes_helper.rb index d734a346e..3d6b559c8 100644 --- a/app/helpers/api/v1/inboxes_helper.rb +++ b/app/helpers/api/v1/inboxes_helper.rb @@ -57,39 +57,35 @@ module Api::V1::InboxesHelper end def check_smtp_connection(channel_data, smtp) + smtp.open_timeout = 10 smtp.start(channel_data[:smtp_domain], channel_data[:smtp_login], channel_data[:smtp_password], channel_data[:smtp_authentication]&.to_sym || :login) smtp.finish + rescue Net::SMTPAuthenticationError + raise StandardError, I18n.t('errors.inboxes.smtp.authentication_error') + rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, Net::OpenTimeout + raise StandardError, I18n.t('errors.inboxes.smtp.connection_error') + rescue OpenSSL::SSL::SSLError + raise StandardError, I18n.t('errors.inboxes.smtp.ssl_error') + rescue Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError + raise StandardError, I18n.t('errors.inboxes.smtp.smtp_error') + rescue StandardError => e + raise StandardError, e.message end def set_smtp_encryption(channel_data, smtp) if channel_data[:smtp_enable_ssl_tls] - set_enable_tls(channel_data, smtp) + set_smtp_ssl_method(smtp, :enable_tls, channel_data[:smtp_openssl_verify_mode]) elsif channel_data[:smtp_enable_starttls_auto] - set_enable_starttls_auto(channel_data, smtp) + set_smtp_ssl_method(smtp, :enable_starttls_auto, channel_data[:smtp_openssl_verify_mode]) end end - def set_enable_starttls_auto(channel_data, smtp) - return unless smtp.respond_to?(:enable_starttls_auto) + def set_smtp_ssl_method(smtp, method, openssl_verify_mode) + return unless smtp.respond_to?(method) - if channel_data[:smtp_openssl_verify_mode] - context = enable_openssl_mode(channel_data[:smtp_openssl_verify_mode]) - smtp.enable_starttls_auto(context) - else - smtp.enable_starttls_auto - end - end - - def set_enable_tls(channel_data, smtp) - return unless smtp.respond_to?(:enable_tls) - - if channel_data[:smtp_openssl_verify_mode] - context = enable_openssl_mode(channel_data[:smtp_openssl_verify_mode]) - smtp.enable_tls(context) - else - smtp.enable_tls - end + context = enable_openssl_mode(openssl_verify_mode) if openssl_verify_mode + context ? smtp.send(method, context) : smtp.send(method) end def enable_openssl_mode(smtp_openssl_verify_mode) diff --git a/app/javascript/dashboard/routes/dashboard/settings/inbox/SmtpSettings.vue b/app/javascript/dashboard/routes/dashboard/settings/inbox/SmtpSettings.vue index 575e0e5d2..e8bc723c2 100644 --- a/app/javascript/dashboard/routes/dashboard/settings/inbox/SmtpSettings.vue +++ b/app/javascript/dashboard/routes/dashboard/settings/inbox/SmtpSettings.vue @@ -147,7 +147,9 @@ export default { await this.$store.dispatch('inboxes/updateInboxSMTP', payload); useAlert(this.$t('INBOX_MGMT.SMTP.EDIT.SUCCESS_MESSAGE')); } catch (error) { - useAlert(this.$t('INBOX_MGMT.SMTP.EDIT.ERROR_MESSAGE')); + useAlert( + error.message || this.$t('INBOX_MGMT.SMTP.EDIT.ERROR_MESSAGE') + ); } }, }, diff --git a/config/locales/en.yml b/config/locales/en.yml index a058d28c7..05276009f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -108,6 +108,11 @@ en: host_unreachable_error: Host unreachable, Please check the IMAP address, IMAP port and try again. connection_timed_out_error: Connection timed out for %{address}:%{port} connection_closed_error: Connection closed. + smtp: + authentication_error: SMTP authentication failed. Please verify your login credentials. + connection_error: Could not connect to SMTP server. Please check the server address and port. + ssl_error: SSL/TLS error. Please verify your encryption settings. + smtp_error: SMTP server error. Please check your configuration and try again. validations: name: should not start or end with symbols, and it should not have < > / \ @ characters. custom_filters: diff --git a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb index fafd6788e..b93ca8ecf 100644 --- a/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/inboxes_controller_spec.rb @@ -631,6 +631,7 @@ RSpec.describe 'Inboxes API', type: :request do it 'updates smtp configuration with starttls encryption' do smtp_connection = double + allow(smtp_connection).to receive(:open_timeout=).and_return(10) allow(smtp_connection).to receive(:start).and_return(true) allow(smtp_connection).to receive(:finish).and_return(true) allow(smtp_connection).to receive(:respond_to?).and_return(true) @@ -661,6 +662,7 @@ RSpec.describe 'Inboxes API', type: :request do it 'updates smtp configuration with ssl/tls encryption' do smtp_connection = double + allow(smtp_connection).to receive(:open_timeout=).and_return(10) allow(smtp_connection).to receive(:start).and_return(true) allow(smtp_connection).to receive(:finish).and_return(true) allow(smtp_connection).to receive(:respond_to?).and_return(true) @@ -691,6 +693,7 @@ RSpec.describe 'Inboxes API', type: :request do it 'updates smtp configuration with authentication mechanism' do smtp_connection = double + allow(smtp_connection).to receive(:open_timeout=).and_return(10) allow(smtp_connection).to receive(:start).and_return(true) allow(smtp_connection).to receive(:finish).and_return(true) allow(smtp_connection).to receive(:respond_to?).and_return(true)