-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend.go
More file actions
88 lines (79 loc) · 2.44 KB
/
send.go
File metadata and controls
88 lines (79 loc) · 2.44 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package plunk
import (
"context"
"net/http"
)
// SendRequest is the request for [Client.Send].
type SendRequest struct {
To []Address
From Address
Subject string
Body string
Template string
Name string
Subscribed bool
Data map[string]any
Headers map[string]string
Reply string
Attachments []Attachment
}
// Attachment represents a file attachment for a transactional email. Content
// must be base64-encoded. A maximum of 10 attachments totaling 10 MB is
// allowed per request.
type Attachment struct {
Filename string `json:"filename"`
Content string `json:"content"`
ContentType string `json:"contentType"`
}
// SendResponse is the response from [Client.Send].
type SendResponse struct {
Emails []SendEmail `json:"emails"`
Timestamp string `json:"timestamp"`
}
// SendEmail represents a sent email in the response.
type SendEmail struct {
Contact SendContact `json:"contact"`
Email string `json:"email"`
}
// SendContact represents a contact associated with a sent email.
type SendContact struct {
ID string `json:"id"`
Email string `json:"email"`
}
type sendRequest struct {
To toField `json:"to"`
From Address `json:"from"`
Subject string `json:"subject,omitempty"`
Body string `json:"body,omitempty"`
Template string `json:"template,omitempty"`
Name string `json:"name,omitempty"`
Subscribed bool `json:"subscribed,omitempty"`
Data map[string]any `json:"data,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Reply string `json:"reply,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}
// Send sends a transactional email. It requires a secret API key.
func (c *Client) Send(ctx context.Context, req *SendRequest) (*SendResponse, error) {
jr := sendRequest{
To: toField(req.To),
From: req.From,
Subject: req.Subject,
Body: req.Body,
Template: req.Template,
Name: req.Name,
Subscribed: req.Subscribed,
Data: req.Data,
Headers: req.Headers,
Reply: req.Reply,
Attachments: req.Attachments,
}
var resp struct {
Success bool `json:"success"`
Data SendResponse `json:"data"`
}
if err := c.do(ctx, http.MethodPost, "/v1/send", jr, &resp); err != nil {
return nil, err
}
return &resp.Data, nil
}