Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added options to send plain or html emails #1325

Merged
merged 6 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion app/mailers/system_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ def send_message(options)

mail_options = { to: options[:to], subject: options[:subject] }
mail_options[:from] = options[:from] if options[:from].present?
mail(mail_options)
if options[:content_type].present?
mail(mail_options) do |format|
format.text if options[:content_type] == "text/plain"
format.html if options[:content_type] == "text/html"
end
else
mail(mail_options)
end
end
end
5 changes: 5 additions & 0 deletions app/models/agents/email_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class EmailAgent < Agent

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']}`).

You can provide a `content_type` for the email and specify `text/plain` or `text/html` to be sent.

If you do not specify `content_type`, then the recipient email server will determine the correct rendering.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be worth adding to the email_digest_agent too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code for email digest is very similar to email agent. I am just going to add it here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this one paragraph (here and below).

You can provide a `content_type` for the email and specify `text/plain` or `text/html` to be sent. If you do not specify `content_type`, then the recipient email server will determine the correct rendering.


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.
MD

Expand All @@ -44,6 +48,7 @@ def receive(incoming_events)
subject: interpolated(event)['subject'],
headline: interpolated(event)['headline'],
body: interpolated(event)['body'],
content_type: interpolated(event)['content_type'],
groups: [present(event.payload)]
).deliver_later
end
Expand Down
14 changes: 14 additions & 0 deletions spec/models/agents/email_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,19 @@ def get_message_part(mail, content_type)
expect(get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip).to match(/\A\s*<strong>rain\!<\/strong>\s*\z/)
expect(get_message_part(ActionMailer::Base.deliveries.last, /html/).strip).to match(/<body>\s*<strong>rain\!<\/strong>\s*<\/body>/)
end
it "can take content type option to set content type of email sent" do
@checker.update_attributes :options => @checker.options.merge({
'content_type' => 'text/plain'
})

event2 = Event.new
event2.agent = agents(:bob_rain_notifier_agent)
event2.payload = { :foo => { :subject => "Something you should know about" }, :some_html => "<strong>rain!</strong>" }
event2.save!

Agents::EmailAgent.async_receive(@checker.id, [event2.id])

expect(ActionMailer::Base.deliveries.last.content_type).to eq("text/plain; charset=UTF-8")
end
end
end