smtp.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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].blank?
  24. options[:port] = 25
  25. end
  26. if !options.key?(:ssl)
  27. if options[:port].to_i == 465
  28. options[:ssl] = true
  29. end
  30. end
  31. if !options.key?(:domain)
  32. # set fqdn, if local fqdn - use domain of sender
  33. fqdn = Setting.get('fqdn')
  34. if fqdn =~ /(localhost|\.local^|\.loc^)/i && (attr['from'] || attr[:from])
  35. domain = Mail::Address.new(attr['from'] || attr[:from]).domain
  36. if domain
  37. fqdn = domain
  38. end
  39. end
  40. options[:domain] = fqdn
  41. end
  42. if !options.key?(:enable_starttls_auto)
  43. options[:enable_starttls_auto] = true
  44. end
  45. if !options.key?(:openssl_verify_mode)
  46. options[:openssl_verify_mode] = 'none'
  47. end
  48. mail = Channel::EmailBuild.build(attr, notification)
  49. smtp_params = {
  50. openssl_verify_mode: options[:openssl_verify_mode],
  51. address: options[:host],
  52. port: options[:port],
  53. domain: options[:domain],
  54. enable_starttls_auto: options[:enable_starttls_auto],
  55. }
  56. # set ssl if needed
  57. if options[:ssl].present?
  58. smtp_params[:ssl] = options[:ssl]
  59. end
  60. # add authentication only if needed
  61. if options[:user].present?
  62. smtp_params[:user_name] = options[:user]
  63. smtp_params[:password] = options[:password]
  64. smtp_params[:authentication] = options[:authentication]
  65. end
  66. mail.delivery_method :smtp, smtp_params
  67. mail.deliver
  68. end
  69. end