Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mailgun/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# It's the version. Yeay!
module Mailgun
VERSION = '1.1.11'
VERSION = '1.2.0'
end
25 changes: 19 additions & 6 deletions lib/railgun/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Railgun
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.
Expand All @@ -27,7 +27,12 @@ def initialize(config)
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
Expand Down Expand Up @@ -62,6 +67,9 @@ def mailgun_client
# 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
Expand All @@ -85,12 +93,17 @@ def transform_for_mailgun(mail)
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|
Expand Down
4 changes: 2 additions & 2 deletions mailgun.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.2.2'

spec.add_development_dependency 'bundler', '~> 1.16.2'
spec.add_development_dependency 'bundler', '>= 1.16.2'
spec.add_development_dependency 'rspec', '~> 3.8.0'
spec.add_development_dependency 'rake', '~> 12.3.1'
spec.add_development_dependency 'rake', '~> 12.3.2'
spec.add_development_dependency 'webmock', '~> 3.4.2'
spec.add_development_dependency 'pry', '~> 0.11.3'
spec.add_development_dependency 'vcr', '~> 3.0.3'
Expand Down
67 changes: 59 additions & 8 deletions spec/unit/railgun/mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ def message_with_attachment(address, subject)
it 'adds headers to message body' do
message = UnitTestMailer.plain_message('test@example.org', '', {})
message.mailgun_headers ||= {
'X-Unit-Test' => 'true',
'x-unit-test' => 'true',
}

body = Railgun.transform_for_mailgun(message)

expect(body).to include('h:X-Unit-Test')
expect(body['h:X-Unit-Test']).to eq('true')
expect(body).to include('h:x-unit-test')
expect(body['h:x-unit-test']).to eq('true')
end

it 'adds headers to message body from mailer' do
message = UnitTestMailer.plain_message('test@example.org', '', {
'X-Unit-Test-2' => 'true',
'x-unit-test-2' => 'true',
})

body = Railgun.transform_for_mailgun(message)

expect(body).to include('h:X-Unit-Test-2')
expect(body['h:X-Unit-Test-2']).to eq('true')
expect(body).to include('h:x-unit-test-2')
expect(body['h:x-unit-test-2']).to eq('true')
end

it 'properly handles headers that are passed as separate POST params' do
Expand All @@ -131,7 +131,7 @@ def message_with_attachment(address, subject)
expect(body).not_to include("h:#{header}")
end

['bcc', 'cc', 'to', 'h:X-Source'].each do |param|
['bcc', 'cc', 'to', 'h:x-source'].each do |param|
expect(body).to include(param)
end

Expand All @@ -142,7 +142,7 @@ def message_with_attachment(address, subject)
expect(body[:html]).to eq(['<p>Test!</p>'.html_safe])
expect(body['bcc']).to eq(['list@example.org'])
expect(body['cc']).to eq(['admin@example.com'])
expect(body['h:X-Source']).to eq('unit tests')
expect(body['h:x-source']).to eq('unit tests')
end

it 'properly adds attachments' do
Expand All @@ -163,4 +163,55 @@ def message_with_attachment(address, subject)
expect(ActionMailer::Base.deliveries).to include(message)
end

it 'ignores `reply-to` in headers' do
message = UnitTestMailer.plain_message('test@example.org', '', {
'reply-to' => 'user@example.com',
})
message.mailgun_headers = {
'Reply-To' => 'administrator@example.org',
}
message.headers({'REPLY-TO' => 'admin@example.net'})
message.reply_to = "dude@example.com.au"

body = Railgun.transform_for_mailgun(message)
expect(body).to include('h:reply-to')
expect(body).not_to include('h:Reply-To')
expect(body['h:reply-to']).to eq('dude@example.com.au')
end

it 'treats `headers()` names as case-insensitve' do
message = UnitTestMailer.plain_message('test@example.org', '', {
'X-BIG-VALUE' => 1,
})

body = Railgun.transform_for_mailgun(message)
expect(body).to include('h:x-big-value')
expect(body['h:x-big-value']).to eq("1")
end

it 'treats `mailgun_headers` names as case-insensitive' do
message = UnitTestMailer.plain_message('test@example.org', '', {})
message.mailgun_headers = {
'X-BIG-VALUE' => 1,
}

body = Railgun.transform_for_mailgun(message)
expect(body).to include('h:x-big-value')
expect(body['h:x-big-value']).to eq("1")
end

it 'handles multi-value, mixed case headers correctly' do
message = UnitTestMailer.plain_message('test@example.org', '', {})
message.headers({
'x-neat-header' => 'foo',
'X-Neat-Header' => 'bar',
'X-NEAT-HEADER' => 'zoop',
})

body = Railgun.transform_for_mailgun(message)
expect(body).to include('h:x-neat-header')
expect(body['h:x-neat-header']).to include('foo')
expect(body['h:x-neat-header']).to include('bar')
expect(body['h:x-neat-header']).to include('zoop')
end
end