mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 02:57:57 +00:00 
			
		
		
		
	chore: Make SMTP environment variables configurable (#1868)
fixes: #1647
This commit is contained in:
		
							
								
								
									
										19
									
								
								.env.example
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								.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 <email@yourdomain.com>` | ||||
| MAILER_SENDER_EMAIL=Chatwoot <accounts@chatwoot.com> | ||||
|  | ||||
|  | ||||
| #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 | ||||
|   | ||||
| @@ -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 <accounts@chatwoot.com>') | ||||
|   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} <reply+#{@conversation.uuid}@#{current_domain}>" | ||||
|       "#{assignee_name} <reply+#{@conversation.uuid}@#{@account.inbound_email_domain}>" | ||||
|     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 | ||||
|     "<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{current_domain}>" | ||||
|     "<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{@account.inbound_email_domain}>" | ||||
|   end | ||||
|  | ||||
|   def in_reply_to_email | ||||
|     conversation_reply_email_id || "<account/#{@account.id}/conversation/#{@conversation.uuid}@#{current_domain}>" | ||||
|     conversation_reply_email_id || "<account/#{@account.id}/conversation/#{@conversation.uuid}@#{@account.inbound_email_domain}>" | ||||
|   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 | ||||
|   | ||||
| @@ -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 <accounts@chatwoot.com>') | ||||
|   end | ||||
|  | ||||
|   private | ||||
|   | ||||
| @@ -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. | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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] | ||||
|   | ||||
| @@ -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 <accounts@chatwoot.com>') | ||||
|  | ||||
|   # Configure the class responsible to send e-mails. | ||||
|   # config.mailer = 'Devise::Mailer' | ||||
|   | ||||
							
								
								
									
										45
									
								
								config/initializers/mailer.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								config/initializers/mailer.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
| @@ -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 | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Sojan Jose
					Sojan Jose