@@ -8,6 +8,13 @@ class SystemMailer < ActionMailer::Base |
||
| 8 | 8 |
|
| 9 | 9 |
mail_options = { to: options[:to], subject: options[:subject] }
|
| 10 | 10 |
mail_options[:from] = options[:from] if options[:from].present? |
| 11 |
- mail(mail_options) |
|
| 11 |
+ if options[:content_type].present? |
|
| 12 |
+ mail(mail_options) do |format| |
|
| 13 |
+ format.text if options[:content_type] == "text/plain" |
|
| 14 |
+ format.html if options[:content_type] == "text/html" |
|
| 15 |
+ end |
|
| 16 |
+ else |
|
| 17 |
+ mail(mail_options) |
|
| 18 |
+ end |
|
| 12 | 19 |
end |
| 13 | 20 |
end |
@@ -23,6 +23,9 @@ module Agents |
||
| 23 | 23 |
|
| 24 | 24 |
You can provide a `from` address for the email, or leave it blank to default to the value of `EMAIL_FROM_ADDRESS` (`#{ENV['EMAIL_FROM_ADDRESS']}`).
|
| 25 | 25 |
|
| 26 |
+ You can provide a `content_type` for the email and specify `text/plain` or `text/html` to be sent. |
|
| 27 |
+ If you do not specify `content_type`, then the recipient email server will determine the correct rendering. |
|
| 28 |
+ |
|
| 26 | 29 |
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent. |
| 27 | 30 |
MD |
| 28 | 31 |
|
@@ -44,6 +47,7 @@ module Agents |
||
| 44 | 47 |
subject: interpolated(event)['subject'], |
| 45 | 48 |
headline: interpolated(event)['headline'], |
| 46 | 49 |
body: interpolated(event)['body'], |
| 50 |
+ content_type: interpolated(event)['content_type'], |
|
| 47 | 51 |
groups: [present(event.payload)] |
| 48 | 52 |
).deliver_later |
| 49 | 53 |
end |
@@ -18,6 +18,10 @@ module Agents |
||
| 18 | 18 |
|
| 19 | 19 |
You can provide a `from` address for the email, or leave it blank to default to the value of `EMAIL_FROM_ADDRESS` (`#{ENV['EMAIL_FROM_ADDRESS']}`).
|
| 20 | 20 |
|
| 21 |
+ You can provide a `content_type` for the email and specify `text/plain` or `text/html` to be sent. |
|
| 22 |
+ |
|
| 23 |
+ If you do not specify `content_type`, then the recipient email server will determine the correct rendering. |
|
| 24 |
+ |
|
| 21 | 25 |
Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent. |
| 22 | 26 |
MD |
| 23 | 27 |
|
@@ -49,6 +53,7 @@ module Agents |
||
| 49 | 53 |
from: interpolated['from'], |
| 50 | 54 |
subject: interpolated['subject'], |
| 51 | 55 |
headline: interpolated['headline'], |
| 56 |
+ content_type: interpolated['content_type'], |
|
| 52 | 57 |
groups: groups |
| 53 | 58 |
).deliver_later |
| 54 | 59 |
end |
@@ -75,5 +75,19 @@ describe Agents::EmailAgent do |
||
| 75 | 75 |
expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to match(/\A\s*<strong>rain\!<\/strong>\s*\z/) |
| 76 | 76 |
expect(get_message_part(ActionMailer::Base.deliveries.last, /html/).strip).to match(/<body>\s*<strong>rain\!<\/strong>\s*<\/body>/) |
| 77 | 77 |
end |
| 78 |
+ it "can take content type option to set content type of email sent" do |
|
| 79 |
+ @checker.update_attributes :options => @checker.options.merge({
|
|
| 80 |
+ 'content_type' => 'text/plain' |
|
| 81 |
+ }) |
|
| 82 |
+ |
|
| 83 |
+ event2 = Event.new |
|
| 84 |
+ event2.agent = agents(:bob_rain_notifier_agent) |
|
| 85 |
+ event2.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<strong>rain!</strong>" }
|
|
| 86 |
+ event2.save! |
|
| 87 |
+ |
|
| 88 |
+ Agents::EmailAgent.async_receive(@checker.id, [event2.id]) |
|
| 89 |
+ |
|
| 90 |
+ expect(ActionMailer::Base.deliveries.last.content_type).to eq("text/plain; charset=UTF-8")
|
|
| 91 |
+ end |
|
| 78 | 92 |
end |
| 79 | 93 |
end |
@@ -11,6 +11,10 @@ describe Agents::EmailDigestAgent do |
||
| 11 | 11 |
@checker = Agents::EmailDigestAgent.new(:name => "something", :options => { :expected_receive_period_in_days => "2", :subject => "something interesting" })
|
| 12 | 12 |
@checker.user = users(:bob) |
| 13 | 13 |
@checker.save! |
| 14 |
+ |
|
| 15 |
+ @checker1 = Agents::EmailDigestAgent.new(:name => "something", :options => { :expected_receive_period_in_days => "2", :subject => "something interesting", :content_type => "text/plain" })
|
|
| 16 |
+ @checker1.user = users(:bob) |
|
| 17 |
+ @checker1.save! |
|
| 14 | 18 |
end |
| 15 | 19 |
|
| 16 | 20 |
after do |
@@ -35,6 +39,7 @@ describe Agents::EmailDigestAgent do |
||
| 35 | 39 |
end |
| 36 | 40 |
|
| 37 | 41 |
describe "#check" do |
| 42 |
+ |
|
| 38 | 43 |
it "should send an email" do |
| 39 | 44 |
Agents::EmailDigestAgent.async_check(@checker.id) |
| 40 | 45 |
expect(ActionMailer::Base.deliveries).to eq([]) |
@@ -47,6 +52,7 @@ describe Agents::EmailDigestAgent do |
||
| 47 | 52 |
@checker.save! |
| 48 | 53 |
|
| 49 | 54 |
Agents::EmailDigestAgent.async_check(@checker.id) |
| 55 |
+ |
|
| 50 | 56 |
expect(ActionMailer::Base.deliveries.last.to).to eq(["bob@example.com"]) |
| 51 | 57 |
expect(ActionMailer::Base.deliveries.last.subject).to eq("something interesting")
|
| 52 | 58 |
expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to eq("Event\n data: Something you should know about\n\nFoo\n bar: 2\n url: http://google.com\n\nhi\n woah: there\n\nEvent\n test: 2")
|
@@ -73,5 +79,20 @@ describe Agents::EmailDigestAgent do |
||
| 73 | 79 |
|
| 74 | 80 |
expect(@checker.reload.memory[:queue]).to be_empty |
| 75 | 81 |
end |
| 82 |
+ |
|
| 83 |
+ it "should send email with correct content type" do |
|
| 84 |
+ Agents::EmailDigestAgent.async_check(@checker1.id) |
|
| 85 |
+ expect(ActionMailer::Base.deliveries).to eq([]) |
|
| 86 |
+ |
|
| 87 |
+ @checker1.memory[:queue] = [{ :data => "Something you should know about" },
|
|
| 88 |
+ { :title => "Foo", :url => "http://google.com", :bar => 2 },
|
|
| 89 |
+ { "message" => "hi", :woah => "there" },
|
|
| 90 |
+ { "test" => 2 }]
|
|
| 91 |
+ @checker1.memory[:events] = [1,2,3,4] |
|
| 92 |
+ @checker1.save! |
|
| 93 |
+ |
|
| 94 |
+ Agents::EmailDigestAgent.async_check(@checker1.id) |
|
| 95 |
+ expect(ActionMailer::Base.deliveries.last.content_type).to eq("text/plain; charset=UTF-8")
|
|
| 96 |
+ end |
|
| 76 | 97 |
end |
| 77 | 98 |
end |