trabajo-afectivo/app/models/channel/driver/smtp.rb

79 lines
2.2 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2015-08-28 00:53:14 +00:00
class Channel::Driver::Smtp
=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
openssl_verify_mode: 'none', # optional
2015-12-14 09:23:14 +00:00
user: 'someuser',
password: 'somepass'
authentication: nil, # nil, autodetection - to use certain schema use 'plain', 'login' or 'cram_md5'
2015-12-14 09:23:14 +00:00
},
mail_attributes,
notification
)
=end
2015-08-28 00:53:14 +00:00
def send(options, attr, notification = false)
# return if we run import mode
return if Setting.get('import_mode')
# set smtp defaults
2017-11-23 08:09:44 +00:00
if !options.key?(:port) || options[:port].blank?
options[:port] = 25
end
if !options.key?(:ssl)
if options[:port].to_i == 465
options[:ssl] = true
end
end
2016-12-02 11:24:00 +00:00
if !options.key?(:domain)
# set fqdn, if local fqdn - use domain of sender
fqdn = Setting.get('fqdn')
if fqdn =~ /(localhost|\.local^|\.loc^)/i && (attr['from'] || attr[:from])
domain = Mail::Address.new(attr['from'] || attr[:from]).domain
if domain
fqdn = domain
end
end
options[:domain] = fqdn
end
if !options.key?(:enable_starttls_auto)
options[:enable_starttls_auto] = true
end
if !options.key?(:openssl_verify_mode)
options[:openssl_verify_mode] = 'none'
end
mail = Channel::EmailBuild.build(attr, notification)
smtp_params = {
openssl_verify_mode: options[:openssl_verify_mode],
2015-08-28 00:53:14 +00:00
address: options[:host],
port: options[:port],
domain: options[:domain],
enable_starttls_auto: options[:enable_starttls_auto],
}
# set ssl if needed
if options[:ssl].present?
smtp_params[:ssl] = options[:ssl]
end
# add authentication only if needed
2016-12-02 11:24:00 +00:00
if options[:user].present?
smtp_params[:user_name] = options[:user]
smtp_params[:password] = options[:password]
smtp_params[:authentication] = options[:authentication]
end
mail.delivery_method :smtp, smtp_params
mail.deliver
end
end