smtp.rb 3.1 KB

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