Skip to content

Commit

Permalink
Add support of additional parameters of Mandrill's send_template me…
Browse files Browse the repository at this point in the history
…thod
  • Loading branch information
Sergey Tikhomirov committed Aug 1, 2013
1 parent 4c36ee8 commit 8b40e1c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.gem
Gemfile.lock
.DS_Store
.ruby-version
.ruby-gemset
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ Creating a new Mandrill Mailer is similar to a normal Rails mailer:
* `filename:` The name of the file
* `mimetype:` This is the mimetype of the file. Ex. png = image/png, pdf = application/pdf, txt = text/plain etc

* `:async` - Whether or not this message should be sent asynchronously

* `:ip_pool` - The name of the dedicated ip pool that should be used to send the message

* `:send_at` - When this message should be sent

## Sending an email

You can send the email by using the familiar syntax:
Expand Down
25 changes: 23 additions & 2 deletions lib/mandrill_mailer/template_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def self.test(mailer_method, options={})
# Public: Other information on the message to send
attr_accessor :message

# Public: Enable background sending mode
attr_accessor :async

# Public: Name of the dedicated IP pool that should be used to send the message
attr_accessor :ip_pool

# Public: When message should be sent
attr_accessor :send_at

# Public: Triggers the stored Mandril params to be sent to the Mandrill api
#
# text - The String to be duplicated.
Expand All @@ -181,7 +190,7 @@ def self.test(mailer_method, options={})
# Returns the duplicated String.
def deliver
mandrill = Mandrill::API.new(api_key)
mandrill.messages.send_template(template_name, template_content, message)
mandrill.messages.send_template(template_name, template_content, message, async, ip_pool, send_at)
end

# Public: Build the hash needed to send to the mandrill api
Expand All @@ -198,6 +207,9 @@ def deliver
# :google_analytics_campaign - Google analytics campaign
# :inline_css - whether or not to automatically inline all CSS styles provided in the message HTML
# :important - whether or not this message is important
# :async - whether or not this message should be sent asynchronously
# :ip_pool - name of the dedicated IP pool that should be used to send the message
# :send_at - when this message should be sent
#
# Examples
#
Expand Down Expand Up @@ -227,6 +239,12 @@ def mandrill_mail(args)
# Set the template content
self.template_content = mandrill_args(args.delete(:template_content))

self.async = args.delete(:async)
self.ip_pool = args.delete(:ip_pool)
if args.has_key?(:send_at)
self.send_at = args.delete(:send_at).getutc.strftime('%Y-%m-%d %H:%M:%S')
end

# Construct message hash
self.message = {
"subject" => args[:subject],
Expand Down Expand Up @@ -261,7 +279,10 @@ def data
"key" => api_key,
"template_name" => template_name,
"template_content" => template_content,
"message" => message
"message" => message,
"async" => async,
"ip_pool" => ip_pool,
"send_at" => send_at
}
end

Expand Down
11 changes: 10 additions & 1 deletion spec/template_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
let(:attachment_file) { File.read(File.expand_path('spec/support/test_image.png')) }
let(:attachment_filename) { 'test_image.png' }
let(:attachment_mimetype) { 'image/png' }
let(:send_at) { Time.utc(2020, 1, 1, 8, 0) }

let(:args) do
{
Expand All @@ -150,7 +151,8 @@
google_analytics_campaign: '1237423474',
attachments: [{file: attachment_file, filename: attachment_filename, mimetype: attachment_mimetype}],
inline_css: true,
important: true
important: true,
send_at: send_at
}
end

Expand Down Expand Up @@ -202,8 +204,15 @@
"template_name" => subject.template_name,
"template_content" => subject.template_content,
"message" => subject.message,
"async" => subject.async,
"ip_pool" => subject.ip_pool,
"send_at" => subject.send_at
})
end

it 'should set send_at option' do
subject.send_at.should eq('2020-01-01 08:00:00')
end
end

describe 'url helpers in mailer' do
Expand Down

0 comments on commit 8b40e1c

Please sign in to comment.