smtp.rb 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (C) 2012-2023 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 send(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. if !options.key?(:openssl_verify_mode)
  49. options[:openssl_verify_mode] = 'none'
  50. end
  51. # set system_bcc of config if defined
  52. system_bcc = Setting.get('system_bcc')
  53. email_address_validation = EmailAddressValidation.new(system_bcc)
  54. if system_bcc.present? && email_address_validation.valid?
  55. attr[:bcc] ||= ''
  56. attr[:bcc] += ', ' if attr[:bcc].present?
  57. attr[:bcc] += system_bcc
  58. end
  59. attr = prepare_idn_outbound(attr)
  60. mail = Channel::EmailBuild.build(attr, notification)
  61. smtp_params = {
  62. openssl_verify_mode: options[:openssl_verify_mode],
  63. address: options[:host],
  64. port: options[:port],
  65. domain: options[:domain],
  66. enable_starttls_auto: options[:enable_starttls_auto],
  67. open_timeout: DEFAULT_OPEN_TIMEOUT,
  68. read_timeout: DEFAULT_READ_TIMEOUT,
  69. }
  70. # set ssl if needed
  71. if options[:ssl].present?
  72. smtp_params[:ssl] = options[:ssl]
  73. end
  74. # add authentication only if needed
  75. if options[:user].present?
  76. smtp_params[:user_name] = options[:user]
  77. smtp_params[:password] = options[:password]
  78. smtp_params[:authentication] = options[:authentication]
  79. end
  80. mail.delivery_method :smtp, smtp_params
  81. mail.deliver
  82. end
  83. end