Skip to content

Commit

Permalink
Add support to send HTML-only emails
Browse files Browse the repository at this point in the history
  • Loading branch information
dgoerlich committed Apr 25, 2016
1 parent 570b9d6 commit 379364a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ data = { description: "My Important Message" }
mail(to: to_email, subject: "Test", body: "test", sparkpost_data: data)
```

By default, content from single-part messages is sent at plain-text. If you are only intending to send HTML email, with no plain-text part, you can specify this
as shown below:

```
data = { html_content_only: true }
mail(to: to_email, subject: "Test", body: "<h1>test</h1>", sparkpost_data: data)
```

### Subaccounts
SparkPostRails supports sending messages via subaccounts in two ways. The default API key set in the configuration can be overriden on a message-by-message basis with a subaccount API key.

Expand Down Expand Up @@ -209,21 +218,3 @@ headers["Sensitivity"] = "private"
mail(to: to_email, subject: "Test", body: "test")
```

Update Note!
============

If you have been using Version 0.0.5 or earlier of this gem, when you upgrade, you'll need to
change your initalizer as follows:

```
SparkpostRails.configure do |c|
```

becomes:

```
SparkPostRails.configure do |c|
```

We have changed the module name to align with the official SparkPost gem's naming convention.
2 changes: 2 additions & 0 deletions lib/sparkpost_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Configuration
attr_accessor :transactional
attr_accessor :ip_pool
attr_accessor :inline_css
attr_accessor :html_content_only

attr_accessor :subaccount

Expand All @@ -50,6 +51,7 @@ def set_defaults
@transactional = false
@ip_pool = nil
@inline_css = false
@html_content_only = false

@subaccount = nil
end
Expand Down
11 changes: 7 additions & 4 deletions lib/sparkpost_rails/delivery_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def deliver!(mail)

prepare_subject_from mail
prepare_cc_headers_from mail, sparkpost_data
prepare_inline_content_from mail
prepare_inline_content_from mail, sparkpost_data
prepare_attachments_from mail
end

Expand Down Expand Up @@ -139,8 +139,7 @@ def prepare_cc_headers_from mail, sparkpost_data
end
end


def prepare_inline_content_from mail
def prepare_inline_content_from mail, sparkpost_data
if mail.multipart?
if mail.html_part
@data[:content][:html] = cleanse_encoding(mail.html_part.body.to_s)
Expand All @@ -150,7 +149,11 @@ def prepare_inline_content_from mail
@data[:content][:text] = cleanse_encoding(mail.text_part.body.to_s)
end
else
@data[:content][:text] = cleanse_encoding(mail.body.to_s)
if SparkPostRails.configuration.html_content_only || sparkpost_data[:html_content_only]
@data[:content][:html] = cleanse_encoding(mail.body.to_s)
else
@data[:content][:text] = cleanse_encoding(mail.body.to_s)
end
end
end

Expand Down
21 changes: 21 additions & 0 deletions spec/inline_content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
describe SparkPostRails::DeliveryMethod do

before(:each) do
SparkPostRails.configuration.set_defaults
@delivery_method = SparkPostRails::DeliveryMethod.new
end

Expand Down Expand Up @@ -30,6 +31,26 @@
expect(@delivery_method.data[:content][:html]).to eq("<h1>Hello, Testing!</h1>")
end

it "supports HTML-only content as a configuration setting" do
SparkPostRails.configure do |c|
c.html_content_only = true
end

test_email = Mailer.test_email
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:content].has_key?(:text)).to eq(false)
expect(@delivery_method.data[:content][:html]).to eq("Hello, Testing!")
end

it "supports HTML-only content as an option on an individual transmission" do
test_email = Mailer.test_email sparkpost_data: {html_content_only: true}
@delivery_method.deliver!(test_email)

expect(@delivery_method.data[:content].has_key?(:text)).to eq(false)
expect(@delivery_method.data[:content][:html]).to eq("Hello, Testing!")
end

it "should not include template details" do
test_email = Mailer.test_email
@delivery_method.deliver!(test_email)
Expand Down

0 comments on commit 379364a

Please sign in to comment.