Skip to content

Commit

Permalink
improve SMTP configs form
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBTurchyn committed Aug 10, 2023
1 parent 1daee82 commit d875477
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ yarn-debug.log*
/coverage
/attachments
/docuseal
/ee
6 changes: 6 additions & 0 deletions app/controllers/email_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ def index; end

def create
if @encrypted_config.update(storage_configs)
SettingsMailer.smtp_successful_setup(@encrypted_config.value['from_email']).deliver_now!

redirect_to settings_email_index_path, notice: 'Changes have been saved'
else
render :index, status: :unprocessable_entity
end
rescue Net::SMTPError, OpenSSL::SSL::SSLError, Net::ReadTimeout => e
flash[:alert] = e.message

render :index, status: :unprocessable_entity
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ button[disabled] .enabled {
.base-radio {
@apply radio bg-white radio-sm no-animation;
}

.base-select {
@apply select base-input w-full font-normal;
}
7 changes: 7 additions & 0 deletions app/mailers/settings_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SettingsMailer < ApplicationMailer
def smtp_successful_setup(email)
mail(to: email, subject: 'SMTP has been configured')
end
end
4 changes: 2 additions & 2 deletions app/views/accounts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<div class="grid md:grid-cols-2 gap-4">
<div class="form-control">
<%= ff.label :timezone, class: 'label' %>
<%= ff.select :timezone, nil, {}, class: 'select base-input w-full font-normal' do %>
<%= ff.select :timezone, nil, {}, class: 'base-select' do %>
<%= time_zone_options_for_select(current_account.timezone) %>
<% end %>
</div>
<div class="form-control">
<%= ff.label :locale, 'Time format', class: 'label' %>
<%= ff.select :locale, options_for_select(controller.class::LOCALE_OPTIONS.invert, current_account.locale), {}, class: 'select base-input w-full font-normal' %>
<%= ff.select :locale, options_for_select(controller.class::LOCALE_OPTIONS.invert, current_account.locale), {}, class: 'base-select' %>
</div>
</div>
<% end %>
Expand Down
21 changes: 21 additions & 0 deletions app/views/email_settings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
<%= ff.password_field :password, value: value['password'], required: true, class: 'base-input' %>
</div>
</div>
<div class="grid md:grid-cols-2 gap-4">
<div class="form-control">
<%= ff.label :domain, 'Domain (optional)', class: 'label' %>
<%= ff.text_field :domain, value: value['domain'], class: 'base-input' %>
</div>
<div class="form-control">
<%= ff.label :authentication, class: 'label' %>
<%= ff.select :authentication, options_for_select([%w[Plain plain], %w[Login login], %w[CRAM-MD5 cram_md5]], value.fetch('authentication', 'plain')), { prompt: true }, required: true, class: 'base-select' %>
</div>
</div>
<div class="form-control">
<%= ff.label :security_label, 'SMTP Security', class: 'label' %>
<div class="flex items-center space-x-6">
<% [%w[None none], %w[SSL ssl], %w[TLS tls]].each do |(label, val)| %>
<%= ff.label :security, value: val, for: "#{val}_radio", class: 'label' do %>
<%= ff.radio_button :security, val, checked: (value['security'].blank? && val == 'none') || value['security'] == val, id: "#{val}_radio", class: 'base-radio mr-2' %>
<%= label %>
<% end %>
<% end %>
</div>
</div>
<div class="form-control">
<%= ff.label :from_email, 'Send from Email', class: 'label' %>
<%= ff.email_field :from_email, value: value['from_email'], required: true, class: 'base-input' %>
Expand Down
1 change: 1 addition & 0 deletions app/views/settings_mailer/smtp_successful_setup.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>SMTP has been successfully configured!</p>
22 changes: 17 additions & 5 deletions lib/action_mailer_configs_interceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ def delivering_email(message)
email_configs = EncryptedConfig.find_by(key: EncryptedConfig::EMAIL_SMTP_KEY)

if email_configs
message.delivery_method(:smtp, user_name: email_configs.value['username'],
password: email_configs.value['password'],
address: email_configs.value['host'],
port: email_configs.value['port'],
tls: email_configs.value['port'].to_s == '465')
message.delivery_method(:smtp, build_smtp_configs_hash(email_configs))

message.from = "#{email_configs.account.name} <#{email_configs.value['from_email']}>"
else
Expand All @@ -32,4 +28,20 @@ def delivering_email(message)

message
end

def build_smtp_configs_hash(email_configs)
value = email_configs.value

{
user_name: value['username'],
password: value['password'],
address: value['host'],
port: value['port'],
domain: value['domain'],
authentication: value.fetch('authentication', 'plain'),
enable_starttls_auto: true,
ssl: value['security'] == 'ssl',
tls: value['security'] == 'tls' || (value['security'].blank? && value['port'].to_s == '465')
}.compact_blank
end
end
7 changes: 7 additions & 0 deletions spec/mailers/previews/settings_mailer_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class SettingsMailerPreview < ActionMailer::Preview
def smtp_successful_setup
SettingsMailer.smtp_successful_setup('[email protected]')
end
end

0 comments on commit d875477

Please sign in to comment.