-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparkpost.coffee
executable file
·62 lines (56 loc) · 1.89 KB
/
sparkpost.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class SparkPostClass
sendTemplate: (id, message) ->
recipients = []
data = message.data or {}
if _.isArray(message.to)
recipients = _.map message.to, (to) ->
address: to
else
recipients.push address: message.to
@api 'POST', 'transmissions?num_rcpt_errors=3',
data:
recipients: recipients
content:
template_id: id
substitution_data: data
options:
inline_css: true
renderTemplate: (id, data = {}) ->
@api 'POST', "templates/#{id}/preview?draft=true",
data:
substitution_data: data
config: (options) ->
@options = _.defaults options,
username: "SMTP_Injection"
port: "587"
host: "smtp.sparkpostmail.com"
baseUrl: "https://api.sparkpost.com/api/v1/"
enableSMTP: true
process.env.MAIL_URL = "smtp://#{@options.username}:#{@options.key}@#{@options.host}:#{@options.port}" if @options.enableSMTP
@headers =
'Content-Type': 'application/json'
'Accept': 'application/json'
'Authorization': @options.key or ''
api: (method, path, options = {}, callback) ->
url = @options.baseUrl + path
options = _.defaults options,
headers: @headers
try
res =
if !!callback
if method in ['DEL','del']
HTTP.del url, options, callback
else
HTTP.call method, url, options, callback
else
if method in ['DEL','del']
HTTP.del url, options
else
HTTP.call method, url, options
if res.data?.results then res.data.results else res.data
catch e
if e.response.data?.errors
throw new Meteor.Error e.response.data.errors[0].message, e.response.data.errors[0].description
else
throw new Meteor.Error 'email-error', 'Unknow error'
@SparkPost = new SparkPostClass