Module: MtaSettings

Defined in:
lib/mta_settings.rb

Defined Under Namespace

Modules: ActionMailerExtensions

Constant Summary collapse

LOCALHOST =
'localhost.localdomain'

Class Method Summary collapse

Class Method Details

.from_env(env = ENV) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mta_settings.rb', line 64

def self.from_env(env = ENV)
  domain = env['MTA_DOMAIN'] || LOCALHOST
  if url = env[env['MTA_PROVIDER'].presence || 'MTA_URL'].presence
    method, settings = from_url(url)
    if method == :smtp && settings[:domain] == LOCALHOST
      settings[:domain] = domain
    end
    [method, settings]
  elsif env['SENDGRID_USERNAME'].present?
    [:smtp, {
      :address              => "smtp.sendgrid.net",
      :port                 => 587,
      :authentication       => :plain,
      :enable_starttls_auto => true,
      :user_name            => env['SENDGRID_USERNAME'],
      :password             => env['SENDGRID_PASSWORD'],
      :domain               => domain,
    }]
  elsif env['MANDRILL_APIKEY'].present?
    [:smtp, {
      :address              => "smtp.mandrillapp.com",
      :port                 => 587,
      :authentication       => :plain,
      :enable_starttls_auto => true,
      :user_name            => env['MANDRILL_USERNAME'],
      :password             => env['MANDRILL_APIKEY'],
      :domain               => domain,
    }]
  elsif env['POSTMARK_API_TOKEN'].present?
    [:smtp, {
      :address              => env['POSTMARK_SMTP_SERVER'] || 'smtp.postmarkapp.com',
      :port                 => 25,
      :authentication       => :cram_md5,
      :enable_starttls_auto => true,
      :user_name            => env['POSTMARK_API_TOKEN'],
      :password             => env['POSTMARK_API_TOKEN'],
      :domain               => domain,
    }]
  elsif env['MAILGUN_SMTP_LOGIN'].present?
    [:smtp, {
      :address              => env['MAILGUN_SMTP_SERVER'] || 'smtp.mailgun.org',
      :port                 => env['MAILGUN_SMTP_PORT'] || '25',
      :authentication       => :plain,
      :enable_starttls_auto => true,
      :user_name            => env['MAILGUN_SMTP_LOGIN'],
      :password             => env['MAILGUN_SMTP_PASSWORD'],
      :domain               => domain,
    }]
  elsif env['MAILTRAP_API_TOKEN'].present?
    response = Net::HTTP.get(URI.parse("https://mailtrap.io/api/v1/inboxes.json?api_token=#{env['MAILTRAP_API_TOKEN']}"))
    inboxes = JSON.parse(response)
    inbox = inboxes.detect { |i| i['id'] == env['MAILTRAP_INBOX_ID'].to_i } || inboxes.first
    [:smtp, {
      :address              => inbox['domain'],
      :port                 => inbox['smtp_ports'].last,
      :authentication       => :cram_md5,
      :user_name            => inbox['username'],
      :password             => inbox['password'],
      :domain               => inbox['domain'],
      :enable_starttls_auto => true
    }]
  end
end

.from_url(url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mta_settings.rb', line 11

def self.from_url(url)
  return if url.blank?
  # Use MTA_URL=: to short circuit configuration
  return [nil, nil] if url == ':'
  # Use MTA_URL=test: to use the test method without changing settings
  return [$1.tr('+-.', '___').to_sym, nil] if url =~ /\A^([\w+-.]+):\z/
  uri = URI.parse(url.to_s)

  settings = {
    :user_name => (CGI.unescape(uri.user) if uri.user),
    :password => (CGI.unescape(uri.password) if uri.password),
    :address => uri.host,
    :port => uri.port,
    :location => (uri.path if uri.path != '/'),
  }.reject do |k, v|
    v.nil?
  end

  if !settings[:location] && uri.opaque =~ /^[^?]/
    settings[:location] = CGI.unescape(uri.opaque.split('?').first)
  end

  CGI.parse(uri.query || uri.opaque.to_s.split('?')[1].to_s).each do |k, v|
    settings[k.to_sym] = v.join("\n")[/.+/m]
  end

  adapter = uri.scheme.downcase.tr('+-.', '___').to_sym
  case adapter
  when :sendmail, :exim
    settings[:location] ||= "/usr/sbin/#{adapter}"
    settings[:arguments] ||= '-i -t'

  when :file
    settings[:location] ||=
      if defined?(Rails.root)
        "#{Rails.root}/tmp/mails"
      else
        "#{Dir.tmpdir}/mails"
      end

  when :smtp, :smtps
    settings[:ssl] = (adapter == :smtps)
    adapter = :smtp
    settings[:enable_starttls_auto] = true
    settings[:authentication] ||= :plain if settings[:user_name]
    settings[:domain] ||=
      (settings.delete(:location) || LOCALHOST).sub(/^\//, '')

  end

  [adapter, settings]
end