module MtaSettings
Constants
- LOCALHOST
Public Class Methods
from_env(env = ENV)
click to toggle source
# 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']}")) first_inbox = JSON.parse(response)[0] [:smtp, { :address => first_inbox['domain'], :port => first_inbox['smtp_ports'][0], :authentication => :plain, :user_name => first_inbox['username'], :password => first_inbox['password'], :domain => first_inbox['domain'] }] end end
from_url(url)
click to toggle source
# 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