From dbf515ab5a1a7486caa5fb8c986250e73bb87976 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Fri, 12 Mar 2021 15:37:06 +0530 Subject: [PATCH] chore: Make SMTP environment variables configurable (#1868) fixes: #1647 --- .env.example | 19 +++++--- app/mailers/conversation_reply_mailer.rb | 35 ++++++--------- app/models/account.rb | 2 +- config/environments/development.rb | 34 -------------- config/environments/production.rb | 13 +----- config/environments/staging.rb | 22 --------- config/initializers/devise.rb | 2 +- config/initializers/mailer.rb | 45 +++++++++++++++++++ .../mailers/conversation_reply_mailer_spec.rb | 4 +- 9 files changed, 77 insertions(+), 99 deletions(-) create mode 100644 config/initializers/mailer.rb diff --git a/.env.example b/.env.example index 7c5299bad..624a44a7b 100644 --- a/.env.example +++ b/.env.example @@ -39,17 +39,24 @@ POSTGRES_PASSWORD= RAILS_ENV=development RAILS_MAX_THREADS=5 -# Mail outgoing -MAILER_SENDER_EMAIL=accounts@chatwoot.com -SMTP_PORT=1025 +# The email from which all outgoing emails are sent +# could user either `email@yourdomain.com` or `BrandName ` +MAILER_SENDER_EMAIL=Chatwoot + + +#SMTP domain key is set up for HELO checking SMTP_DOMAIN=chatwoot.com -# if you are running docker-compose, set SMTP_ADDRESS value as "mailhog", -# else set the value as "localhost" +# the default value is set "mailhog" and is used by docker-compose for development environments, +# Set the value as "localhost" or your SMTP address in other environments SMTP_ADDRESS=mailhog +SMTP_PORT=1025 SMTP_USERNAME= SMTP_PASSWORD= +# plain,login,cram_md5 SMTP_AUTHENTICATION= -SMTP_ENABLE_STARTTLS_AUTO= +SMTP_ENABLE_STARTTLS_AUTO=true +# Can be: 'none', 'peer', 'client_once', 'fail_if_no_peer_cert', see http://api.rubyonrails.org/classes/ActionMailer/Base.html +SMTP_OPENSSL_VERIFY_MODE=peer # Mail Incoming # This is the domain set for the reply emails when conversation continuity is enabled diff --git a/app/mailers/conversation_reply_mailer.rb b/app/mailers/conversation_reply_mailer.rb index 93fb61668..ea21cbb2d 100644 --- a/app/mailers/conversation_reply_mailer.rb +++ b/app/mailers/conversation_reply_mailer.rb @@ -1,5 +1,5 @@ class ConversationReplyMailer < ApplicationMailer - default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com') + default from: ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot ') layout :choose_layout def reply_with_summary(conversation, message_queued_time) @@ -99,7 +99,7 @@ class ConversationReplyMailer < ApplicationMailer def reply_email if should_use_conversation_email_address? - "#{assignee_name} " + "#{assignee_name} " else @inbox.email_address || @agent&.email end @@ -107,24 +107,28 @@ class ConversationReplyMailer < ApplicationMailer def from_email_with_name if should_use_conversation_email_address? - "#{assignee_name} <#{account_support_email}>" + "#{assignee_name} from #{@inbox.name} <#{parse_email(@account.support_email)}>" else - "#{assignee_name} <#{from_email_address}>" + "#{assignee_name} from #{@inbox.name} <#{parse_email(inbox_from_email_address)}>" end end - def from_email_address + def parse_email(email_string) + Mail::Address.new(email_string).address + end + + def inbox_from_email_address return @inbox.email_address if @inbox.email_address - ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com') + @account.support_email end def custom_message_id - "" + "" end def in_reply_to_email - conversation_reply_email_id || "" + conversation_reply_email_id || "" end def conversation_reply_email_id @@ -138,19 +142,8 @@ class ConversationReplyMailer < ApplicationMailer end def inbound_email_enabled? - @inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && current_domain.present? && account_support_email.present? - end - - def current_domain - @current_domain ||= @account.inbound_email_domain - end - - def account_support_email - @account_support_email ||= begin - @account.support_email || - GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || - ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com') - end + @inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && @account.inbound_email_domain + .present? && @account.support_email.present? end def choose_layout diff --git a/app/models/account.rb b/app/models/account.rb index db163ca36..35a731aba 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -92,7 +92,7 @@ class Account < ApplicationRecord end def support_email - super || GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || ENV.fetch('MAILER_SENDER_EMAIL', nil) + super || GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] || ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot ') end private diff --git a/config/environments/development.rb b/config/environments/development.rb index 3bdc26c99..557000065 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -34,40 +34,6 @@ Rails.application.configure do config.active_job.queue_adapter = :sidekiq - # Email related config - config.action_mailer.perform_caching = false - config.action_mailer.perform_deliveries = true - config.action_mailer.raise_delivery_errors = true - config.action_mailer.default_url_options = { host: ENV['FRONTEND_URL'] } - - smtp_settings = { - port: ENV['SMTP_PORT'] || 25, - domain: ENV['SMTP_DOMAIN'] || 'localhost', - address: ENV['SMTP_ADDRESS'] || 'chatwoot.com' - } - - if ENV['SMTP_AUTHENTICATION'].present? - smtp_settings[:user_name] = ENV['SMTP_USERNAME'] - smtp_settings[:password] = ENV['SMTP_PASSWORD'] - smtp_settings[:authentication] = ENV['SMTP_AUTHENTICATION'] - smtp_settings[:enable_starttls_auto] = ENV['SMTP_ENABLE_STARTTLS_AUTO'] if ENV['SMTP_ENABLE_STARTTLS_AUTO'].present? - end - - if ENV['LETTER_OPENER'] - config.action_mailer.delivery_method = :letter_opener - else - config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = smtp_settings - end - - # Set this to appropriate ingress service for which the options are : - # :relay for Exim, Postfix, Qmail - # :mailgun for Mailgun - # :mandrill for Mandrill - # :postmark for Postmark - # :sendgrid for Sendgrid - config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym - Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } # Print deprecation notices to the Rails logger. diff --git a/config/environments/production.rb b/config/environments/production.rb index ced1f7013..f30ee2239 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -76,7 +76,7 @@ Rails.application.configure do # Use a different logger for distributed setups. # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') - + if ActiveModel::Type::Boolean.new.cast(ENV.fetch('RAILS_LOG_TO_STDOUT', true)) logger = ActiveSupport::Logger.new($stdout) logger.formatter = config.log_formatter @@ -98,17 +98,6 @@ Rails.application.configure do # Set this to true and configure the email server for immediate delivery to raise delivery errors. # config.action_mailer.raise_delivery_errors = false - # Chatwoot production settings - config.action_mailer.default_url_options = { host: ENV['FRONTEND_URL'] } - config.action_mailer.smtp_settings = { - address: ENV['SMTP_ADDRESS'], - port: ENV['SMTP_PORT'] || 587, - user_name: ENV['SMTP_USERNAME'], - password: ENV['SMTP_PASSWORD'], - authentication: :login, - enable_starttls_auto: true - } - # Set this to appropriate ingress service for which the options are : # :relay for Exim, Postfix, Qmail # :mailgun for Mailgun diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 3adfe1e0c..36efcc171 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -54,31 +54,9 @@ Rails.application.configure do # Use a real queuing backend for Active Job (and separate queues per environment) # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "chatwoot_#{Rails.env}" - config.action_mailer.perform_caching = false - config.action_mailer.default_url_options = { host: ENV['FRONTEND_URL'] } - config.action_mailer.smtp_settings = { - address: ENV['SMTP_ADDRESS'], - port: 587, - user_name: ENV['SMTP_USERNAME'], # Your SMTP user - password: ENV['SMTP_PASSWORD'], # Your SMTP password - authentication: :login, - enable_starttls_auto: true - } Rails.application.routes.default_url_options = { host: ENV['FRONTEND_URL'] } - # 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 - - # Set this to appropriate ingress service for which the options are : - # :relay for Exim, Postfix, Qmail - # :mailgun for Mailgun - # :mandrill for Mandrill - # :postmark for Postmark - # :sendgrid for Sendgrid - config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym - # Enable locale fallbacks for I18n (makes lookups for any locale fall back to # the I18n.default_locale when a translation cannot be found). config.i18n.fallbacks = [I18n.default_locale] diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 80eb40433..134005945 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -12,7 +12,7 @@ Devise.setup do |config| # Configure the e-mail address which will be shown in Devise::Mailer, # note that it will be overwritten if you use your own mailer class # with default "from" parameter. - config.mailer_sender = ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com') + config.mailer_sender = ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot ') # Configure the class responsible to send e-mails. # config.mailer = 'Devise::Mailer' diff --git a/config/initializers/mailer.rb b/config/initializers/mailer.rb new file mode 100644 index 000000000..1be29ee96 --- /dev/null +++ b/config/initializers/mailer.rb @@ -0,0 +1,45 @@ +Rails.application.configure do + ######################################### + # Configuration Related to Action Mailer + ######################################### + + config.action_mailer.delivery_method = :smtp + # We need the application frontend url to be used in our emails + config.action_mailer.default_url_options = { host: ENV['FRONTEND_URL'] } if ENV['FRONTEND_URL'].present? + # We load certain mailer templates from our database. This ensures changes to it is reflected immediately + config.action_mailer.perform_caching = false + config.action_mailer.perform_deliveries = true + config.action_mailer.raise_delivery_errors = true + + # Config related to smtp + smtp_settings = { + domain: ENV.fetch('SMTP_DOMAIN', ''), + address: ENV.fetch('SMTP_ADDRESS', 'localhost'), + port: ENV.fetch('SMTP_PORT', 587) + } + + smtp_settings[:authentication] = ENV.fetch('SMTP_AUTHENTICATION', 'login').to_sym if ENV.fetch('SMTP_AUTHENTICATION', '').present? + smtp_settings[:user_name] = ENV.fetch('SMTP_USERNAME', '') + smtp_settings[:password] = ENV.fetch('SMTP_PASSWORD', '') + smtp_settings[:enable_starttls_auto] = ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_ENABLE_STARTTLS_AUTO', true)) + smtp_settings[:openssl_verify_mode] = ENV.fetch('SMTP_OPENSSL_VERIFY_MODE', 'peer') + smtp_settings[:ssl] = ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_SSL', true)) if ENV['SMTP_SSL'] + smtp_settings[:tls] = ActiveModel::Type::Boolean.new.cast(ENV.fetch('SMTP_TLS', true)) if ENV['SMTP_TLS'] + + config.action_mailer.delivery_method = :smtp + config.action_mailer.smtp_settings = smtp_settings + # You can use letter opener for your local development by setting the environment variable + config.action_mailer.delivery_method = :letter_opener if Rails.env.development? && ENV['LETTER_OPENER'] + + ######################################### + # Configuration Related to Action MailBox + ######################################### + + # Set this to appropriate ingress service for which the options are : + # :relay for Exim, Postfix, Qmail + # :mailgun for Mailgun + # :mandrill for Mandrill + # :postmark for Postmark + # :sendgrid for Sendgrid + config.action_mailbox.ingress = ENV.fetch('RAILS_INBOUND_EMAIL_SERVICE', 'relay').to_sym +end diff --git a/spec/mailers/conversation_reply_mailer_spec.rb b/spec/mailers/conversation_reply_mailer_spec.rb index 8ddd11523..598549708 100644 --- a/spec/mailers/conversation_reply_mailer_spec.rb +++ b/spec/mailers/conversation_reply_mailer_spec.rb @@ -45,7 +45,7 @@ RSpec.describe ConversationReplyMailer, type: :mailer do let(:mail) { described_class.reply_with_summary(message.conversation, Time.zone.now).deliver_now } it 'has correct name' do - expect(mail[:from].display_names).to eq(['Notifications']) + expect(mail[:from].display_names).to eq(['Notifications from Inbox']) end end @@ -148,7 +148,7 @@ RSpec.describe ConversationReplyMailer, type: :mailer do end it 'sets the from email to be the support email' do - expect(mail['FROM'].value).to eq("#{agent.available_name} <#{conversation.account.support_email}>") + expect(mail['FROM'].value).to eq("#{agent.available_name} from Inbox <#{conversation.account.support_email}>") expect(mail.from).to eq([conversation.account.support_email]) end