lib/railgun/mailer.rb in mailgun-ruby-1.1.11 vs lib/railgun/mailer.rb in mailgun-ruby-1.2.0

- old
+ new

@@ -9,11 +9,11 @@ # Railgun::Mailer is an ActionMailer provider for sending mail through # Mailgun. class Mailer # List of the headers that will be ignored when copying headers from `mail.header_fields` - IGNORED_HEADERS = %w[ to from subject ] + IGNORED_HEADERS = %w[ to from subject reply-to ] # [Hash] config -> # Requires *at least* `api_key` and `domain` keys. attr_accessor :config, :domain, :settings @@ -25,11 +25,16 @@ [:api_key, :domain].each do |k| raise Railgun::ConfigurationError.new("Config requires `#{k}` key", @config) unless @config.has_key?(k) end - @mg_client = Mailgun::Client.new(config[:api_key], config[:api_host] || 'api.mailgun.net', config[:api_version] || 'v3', config[:api_ssl].nil? ? true : config[:api_ssl]) + @mg_client = Mailgun::Client.new( + config[:api_key], + config[:api_host] || 'api.mailgun.net', + config[:api_version] || 'v3', + config[:api_ssl].nil? ? true : config[:api_ssl], + ) @domain = @config[:domain] # To avoid exception in mail gem v2.6 @settings = { return_response: true } @@ -60,10 +65,13 @@ # Performs a series of transformations on the `mailgun*` attributes. # After prefixing them with the proper option type, they are added to # the message hash where they will then be sent to the API as JSON. # + # It is important to note that headers set in `mailgun_headers` on the message + # WILL overwrite headers set via `mail.headers()`. + # # @param [Mail::Message] mail message to transform # # @return [Hash] transformed message hash def transform_for_mailgun(mail) message = build_message_object(mail) @@ -83,15 +91,20 @@ # as they are accepted as POST parameters on the message endpoint. msg_headers = Hash.new # h:* attributes (headers) - mail.mailgun_headers.try(:each) do |k, v| - msg_headers[k] = v - end + # Let's set all of these headers on the [Mail::Message] so that + # the are created inside of a [Mail::Header] instance and processed there. + mail.headers(mail.mailgun_headers || {}) mail.header_fields.each do |field| - msg_headers[field.name] = field.value + header = field.name.downcase + if msg_headers.include? header + msg_headers[header] = [msg_headers[header], field.value].flatten + else + msg_headers[header] = field.value + end end msg_headers.each do |k, v| if Railgun::Mailer::IGNORED_HEADERS.include? k.downcase Rails.logger.debug("[railgun] ignoring header (using envelope instead): #{k}")