2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
class Channel::Driver::Smtp
|
2015-08-30 11:58:05 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
instance = Channel::Driver::Smtp.new
|
|
|
|
instance.send(
|
2015-12-14 09:23:14 +00:00
|
|
|
{
|
|
|
|
host: 'some.host',
|
|
|
|
port: 25,
|
|
|
|
enable_starttls_auto: true, # optional
|
2016-11-06 15:45:38 +00:00
|
|
|
openssl_verify_mode: 'none', # optional
|
2015-12-14 09:23:14 +00:00
|
|
|
user: 'someuser',
|
|
|
|
password: 'somepass'
|
2020-05-28 13:28:07 +00:00
|
|
|
authentication: nil, # nil, autodetection - to use certain schema use 'plain', 'login', 'xoauth2' or 'cram_md5'
|
2015-12-14 09:23:14 +00:00
|
|
|
},
|
|
|
|
mail_attributes,
|
|
|
|
notification
|
2015-08-30 11:58:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
def send(options, attr, notification = false)
|
2012-12-24 13:55:43 +00:00
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
2015-08-30 11:58:05 +00:00
|
|
|
# set smtp defaults
|
2017-11-23 08:09:44 +00:00
|
|
|
if !options.key?(:port) || options[:port].blank?
|
2015-08-30 11:58:05 +00:00
|
|
|
options[:port] = 25
|
|
|
|
end
|
2020-09-30 09:07:01 +00:00
|
|
|
if !options.key?(:ssl) && options[:port].to_i == 465
|
|
|
|
options[:ssl] = true
|
2017-01-19 05:03:19 +00:00
|
|
|
end
|
2016-12-02 11:24:00 +00:00
|
|
|
if !options.key?(:domain)
|
2016-11-07 11:01:55 +00:00
|
|
|
# set fqdn, if local fqdn - use domain of sender
|
|
|
|
fqdn = Setting.get('fqdn')
|
2021-05-12 11:37:44 +00:00
|
|
|
if fqdn =~ %r{(localhost|\.local^|\.loc^)}i && (attr['from'] || attr[:from])
|
2016-11-07 11:01:55 +00:00
|
|
|
domain = Mail::Address.new(attr['from'] || attr[:from]).domain
|
|
|
|
if domain
|
|
|
|
fqdn = domain
|
|
|
|
end
|
|
|
|
end
|
|
|
|
options[:domain] = fqdn
|
2016-11-06 15:45:38 +00:00
|
|
|
end
|
2015-08-30 11:58:05 +00:00
|
|
|
if !options.key?(:enable_starttls_auto)
|
|
|
|
options[:enable_starttls_auto] = true
|
|
|
|
end
|
|
|
|
if !options.key?(:openssl_verify_mode)
|
|
|
|
options[:openssl_verify_mode] = 'none'
|
|
|
|
end
|
2018-09-13 07:09:05 +00:00
|
|
|
|
|
|
|
# set system_bcc of config if defined
|
|
|
|
system_bcc = Setting.get('system_bcc')
|
2019-11-21 15:49:14 +00:00
|
|
|
email_address_validation = EmailAddressValidation.new(system_bcc)
|
|
|
|
if system_bcc.present? && email_address_validation.valid_format?
|
2018-09-13 07:09:05 +00:00
|
|
|
attr[:bcc] ||= ''
|
|
|
|
attr[:bcc] += ', ' if attr[:bcc].present?
|
|
|
|
attr[:bcc] += system_bcc
|
|
|
|
end
|
|
|
|
|
2014-12-29 23:25:57 +00:00
|
|
|
mail = Channel::EmailBuild.build(attr, notification)
|
2016-11-16 14:08:17 +00:00
|
|
|
smtp_params = {
|
2018-12-19 17:31:51 +00:00
|
|
|
openssl_verify_mode: options[:openssl_verify_mode],
|
|
|
|
address: options[:host],
|
|
|
|
port: options[:port],
|
|
|
|
domain: options[:domain],
|
2015-08-30 11:58:05 +00:00
|
|
|
enable_starttls_auto: options[:enable_starttls_auto],
|
2012-04-13 13:51:10 +00:00
|
|
|
}
|
2016-11-16 14:08:17 +00:00
|
|
|
|
2017-01-19 05:03:19 +00:00
|
|
|
# set ssl if needed
|
|
|
|
if options[:ssl].present?
|
|
|
|
smtp_params[:ssl] = options[:ssl]
|
|
|
|
end
|
|
|
|
|
2016-11-16 14:08:17 +00:00
|
|
|
# add authentication only if needed
|
2016-12-02 11:24:00 +00:00
|
|
|
if options[:user].present?
|
2016-11-16 14:08:17 +00:00
|
|
|
smtp_params[:user_name] = options[:user]
|
|
|
|
smtp_params[:password] = options[:password]
|
|
|
|
smtp_params[:authentication] = options[:authentication]
|
|
|
|
end
|
|
|
|
mail.delivery_method :smtp, smtp_params
|
2013-06-12 15:59:58 +00:00
|
|
|
mail.deliver
|
2012-04-13 13:51:10 +00:00
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|