smtp.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Channel::Driver::Smtp
  3. =begin
  4. instance = Channel::Driver::Smtp.new
  5. instance.send(
  6. {
  7. host: 'some.host',
  8. port: 25,
  9. enable_starttls_auto: true, # optional
  10. openssl_verify_mode: 'none', # optional
  11. user: 'someuser',
  12. password: 'somepass'
  13. authentication: nil, # nil, autodetection - to use certain schema use 'plain', 'login' or 'cram_md5'
  14. },
  15. mail_attributes,
  16. notification
  17. )
  18. =end
  19. def send(options, attr, notification = false)
  20. # return if we run import mode
  21. return if Setting.get('import_mode')
  22. # set smtp defaults
  23. if !options.key?(:port) || options[:port].empty?
  24. options[:port] = 25
  25. end
  26. if !options.key?(:domain)
  27. # set fqdn, if local fqdn - use domain of sender
  28. fqdn = Setting.get('fqdn')
  29. if fqdn =~ /(localhost|\.local^|\.loc^)/i && (attr['from'] || attr[:from])
  30. domain = Mail::Address.new(attr['from'] || attr[:from]).domain
  31. if domain
  32. fqdn = domain
  33. end
  34. end
  35. options[:domain] = fqdn
  36. end
  37. if !options.key?(:enable_starttls_auto)
  38. options[:enable_starttls_auto] = true
  39. end
  40. if !options.key?(:openssl_verify_mode)
  41. options[:openssl_verify_mode] = 'none'
  42. end
  43. mail = Channel::EmailBuild.build(attr, notification)
  44. mail.delivery_method :smtp, {
  45. openssl_verify_mode: options[:openssl_verify_mode],
  46. address: options[:host],
  47. port: options[:port],
  48. domain: options[:domain],
  49. user_name: options[:user],
  50. password: options[:password],
  51. enable_starttls_auto: options[:enable_starttls_auto],
  52. authentication: options[:authentication],
  53. }
  54. mail.deliver
  55. end
  56. end