smtp.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. class Channel::Driver::Smtp
  3. # we're using the same timeouts like in Net::SMTP gem
  4. # but we would like to have the possibility to mock it for tests
  5. DEFAULT_OPEN_TIMEOUT = 30.seconds
  6. DEFAULT_READ_TIMEOUT = 60.seconds
  7. =begin
  8. instance = Channel::Driver::Smtp.new
  9. instance.send(
  10. {
  11. host: 'some.host',
  12. port: 25,
  13. enable_starttls_auto: true, # optional
  14. openssl_verify_mode: 'none', # optional
  15. user: 'someuser',
  16. password: 'somepass'
  17. authentication: nil, # nil, autodetection - to use certain schema use 'plain', 'login', 'xoauth2' or 'cram_md5'
  18. },
  19. mail_attributes,
  20. notification
  21. )
  22. =end
  23. def send(options, attr, notification = false)
  24. # return if we run import mode
  25. return if Setting.get('import_mode')
  26. # set smtp defaults
  27. if !options.key?(:port) || options[:port].blank?
  28. options[:port] = 25
  29. end
  30. if !options.key?(:ssl) && options[:port].to_i == 465
  31. options[:ssl] = true
  32. end
  33. if !options.key?(:domain)
  34. # set fqdn, if local fqdn - use domain of sender
  35. fqdn = Setting.get('fqdn')
  36. if fqdn =~ %r{(localhost|\.local^|\.loc^)}i && (attr['from'] || attr[:from])
  37. domain = Mail::Address.new(attr['from'] || attr[:from]).domain
  38. if domain
  39. fqdn = domain
  40. end
  41. end
  42. options[:domain] = fqdn
  43. end
  44. if !options.key?(:enable_starttls_auto)
  45. options[:enable_starttls_auto] = true
  46. end
  47. if !options.key?(:openssl_verify_mode)
  48. options[:openssl_verify_mode] = 'none'
  49. end
  50. # set system_bcc of config if defined
  51. system_bcc = Setting.get('system_bcc')
  52. email_address_validation = EmailAddressValidation.new(system_bcc)
  53. if system_bcc.present? && email_address_validation.valid?
  54. attr[:bcc] ||= ''
  55. attr[:bcc] += ', ' if attr[:bcc].present?
  56. attr[:bcc] += system_bcc
  57. end
  58. mail = Channel::EmailBuild.build(attr, notification)
  59. smtp_params = {
  60. openssl_verify_mode: options[:openssl_verify_mode],
  61. address: options[:host],
  62. port: options[:port],
  63. domain: options[:domain],
  64. enable_starttls_auto: options[:enable_starttls_auto],
  65. open_timeout: DEFAULT_OPEN_TIMEOUT,
  66. read_timeout: DEFAULT_READ_TIMEOUT,
  67. }
  68. # set ssl if needed
  69. if options[:ssl].present?
  70. smtp_params[:ssl] = options[:ssl]
  71. end
  72. # add authentication only if needed
  73. if options[:user].present?
  74. smtp_params[:user_name] = options[:user]
  75. smtp_params[:password] = options[:password]
  76. smtp_params[:authentication] = options[:authentication]
  77. end
  78. mail.delivery_method :smtp, smtp_params
  79. mail.deliver
  80. end
  81. end