Skip to content

Commit cd0bd2a

Browse files
benedikteelcoj
andauthored
Add Userlist as an additional provider (#5)
* Add Userlist as an additional provider * Please standardrb --------- Co-authored-by: eelco <[email protected]>
1 parent 41ed352 commit cd0bd2a

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Courrier supports these transactional email providers:
161161
- [Postmark](https://postmarkapp.com)
162162
- [SendGrid](https://sendgrid.com)
163163
- [SparkPost](https://sparkpost.com)
164+
- [Userlist](https://userlist.com)
164165

165166
⚠️ Some providers still need manual verification of their implementation. If you're using one of these providers, please help verify the implementation by sharing your experience in [this GitHub issue](https://github.com/Rails-Designer/courrier/issues/4). 🙏
166167

lib/courrier/email/provider.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
require "courrier/email/providers/preview"
1111
require "courrier/email/providers/sendgrid"
1212
require "courrier/email/providers/sparkpost"
13+
require "courrier/email/providers/userlist"
1314

1415
module Courrier
1516
class Email
@@ -43,7 +44,8 @@ def deliver
4344
postmark: Courrier::Email::Providers::Postmark,
4445
preview: Courrier::Email::Providers::Preview,
4546
sendgrid: Courrier::Email::Providers::Sendgrid,
46-
sparkpost: Courrier::Email::Providers::Sparkpost
47+
sparkpost: Courrier::Email::Providers::Sparkpost,
48+
userlist: Courrier::Email::Providers::Userlist
4749
}.freeze
4850

4951
def configuration_missing_in_production?
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
module Courrier
4+
class Email
5+
module Providers
6+
class Userlist < Base
7+
ENDPOINT_URL = "https://push.userlist.com/messages"
8+
9+
def body
10+
{
11+
"from" => @options.from,
12+
"to" => @options.to,
13+
"subject" => @options.subject,
14+
"body" => body_document
15+
}.compact.merge(provider_options)
16+
end
17+
18+
private
19+
20+
def headers
21+
{
22+
"Authorization" => "Push #{@api_key}"
23+
}
24+
end
25+
26+
def body_document
27+
if @options.html && @options.text
28+
multipart_document
29+
elsif @options.html
30+
html_document
31+
elsif @options.text
32+
text_document
33+
end
34+
end
35+
36+
def text_document
37+
{
38+
"type" => "text",
39+
"content" => @options.text
40+
}
41+
end
42+
43+
def html_document
44+
{
45+
"type" => "html",
46+
"content" => @options.html
47+
}
48+
end
49+
50+
def multipart_document
51+
{
52+
"type" => "multipart",
53+
"content" => [
54+
html_document,
55+
text_document
56+
]
57+
}
58+
end
59+
60+
def provider_options
61+
{"theme" => nil}.merge(@provider_options)
62+
end
63+
end
64+
end
65+
end
66+
end

0 commit comments

Comments
 (0)