-
Notifications
You must be signed in to change notification settings - Fork 1
/
secmail.go
63 lines (50 loc) · 1.24 KB
/
secmail.go
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
package secmail
import (
"context"
"time"
"github.com/ProtonMail/gopenpgp/v2/helper"
"github.com/mailgun/mailgun-go/v4"
)
// Recipient of the email. This type includes their email address and a public
// key for encryption.
type Recipient struct {
Email string
Key string
}
// NewRecipient creates a new Recipient.
func NewRecipient(email, key string) *Recipient {
return &Recipient{
Email: email,
Key: key,
}
}
// Mailer sends the email.
type Mailer struct {
SenderEmail string
MailgunDomain string
MailgunAPIKey string
}
// NewMailer returns a new Mailer.
func NewMailer(sender, domain, key string) *Mailer {
return &Mailer{
SenderEmail: sender,
MailgunDomain: domain,
MailgunAPIKey: key,
}
}
// Send the recipient an encrypted email.
func (m Mailer) Send(rcpt *Recipient, subject string, message string) (string, error) {
armor, err := helper.EncryptMessageArmored(rcpt.Key, message)
if err != nil {
return "", err
}
mg := mailgun.NewMailgun(m.MailgunDomain, m.MailgunAPIKey)
msg := mg.NewMessage(m.SenderEmail, subject, armor, rcpt.Email)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
_, id, err := mg.Send(ctx, msg)
if err != nil {
return "", err
}
return id, nil
}