-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmail.go
198 lines (162 loc) · 5 KB
/
mail.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package proton_api_bridge
import (
"context"
"encoding/base64"
"io/ioutil"
"log"
"net/mail"
"path/filepath"
"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/henrybear327/go-proton-api"
)
type MailSendingParameters struct {
TemplateFile string
EmailSubject string
RecipientEmailAddress string
EmailAttachments []string
EmailContentIDs []string
}
func (protonDrive *ProtonDrive) SendEmail(ctx context.Context, i int, errChan chan error, config *MailSendingParameters) {
log.Println("SendEmail in", i)
defer log.Println("SendEmail out", i)
createDraftResp, err := protonDrive.createDraft(ctx, config)
if err != nil {
errChan <- err
}
attachments, err := protonDrive.uploadAttachments(ctx, createDraftResp, config)
if err != nil {
errChan <- err
}
err = protonDrive.sendDraft(ctx, createDraftResp.ID, attachments, config)
if err != nil {
errChan <- err
}
errChan <- nil
}
func (protonDrive *ProtonDrive) getHTMLBody(config *MailSendingParameters) ([]byte, error) {
htmlTemplate, err := ioutil.ReadFile(config.TemplateFile)
if err != nil {
return nil, err
}
return htmlTemplate, nil
}
func (protonDrive *ProtonDrive) createDraft(ctx context.Context, config *MailSendingParameters) (*proton.Message, error) {
htmlTemplate, err := protonDrive.getHTMLBody(config)
if err != nil {
return nil, err
}
createDraftReq := proton.CreateDraftReq{
Message: proton.DraftTemplate{
Subject: config.EmailSubject,
Sender: &mail.Address{
Address: protonDrive.signatureAddress,
},
ToList: []*mail.Address{
{
Address: config.RecipientEmailAddress,
},
},
CCList: []*mail.Address{},
BCCList: []*mail.Address{},
Body: string(htmlTemplate), // NOTE: the body here is for yourself to view it! No sender encryption is done yet
MIMEType: rfc822.TextHTML,
// Unread: false,
// ExternalID: "", // FIXME: what's this
},
}
createDraftResp, err := protonDrive.c.CreateDraft(ctx, protonDrive.DefaultAddrKR, createDraftReq)
if err != nil {
return nil, err
}
return &createDraftResp, nil
}
func (protonDrive *ProtonDrive) getAttachmentSessionKeyMap(attachments []*proton.Attachment) (map[string]*crypto.SessionKey, error) {
ret := make(map[string]*crypto.SessionKey)
for i := range attachments {
keyPacket, err := base64.StdEncoding.DecodeString(attachments[i].KeyPackets)
if err != nil {
return nil, err
}
key, err := protonDrive.DefaultAddrKR.DecryptSessionKey(keyPacket)
if err != nil {
return nil, err
}
ret[attachments[i].ID] = key
}
return ret, nil
}
func (protonDrive *ProtonDrive) uploadAttachments(ctx context.Context, createDraftResp *proton.Message, config *MailSendingParameters) ([]*proton.Attachment, error) {
attachments := make([]*proton.Attachment, 0)
for i := range config.EmailAttachments {
// read out attachment file
fileByteArray, err := ioutil.ReadFile(config.EmailAttachments[i])
if err != nil {
return nil, err
}
req := proton.CreateAttachmentReq{
MessageID: createDraftResp.ID,
Filename: filepath.Base(config.EmailAttachments[i]),
MIMEType: rfc822.MultipartMixed, // FIXME: what is this?
Disposition: proton.InlineDisposition,
ContentID: config.EmailContentIDs[i],
Body: fileByteArray,
}
uploadAttachmentResp, err := protonDrive.c.UploadAttachment(ctx, protonDrive.DefaultAddrKR, req)
if err != nil {
return nil, err
}
// log.Printf("uploadAttachmentResp %#v", uploadAttachmentResp)
attachments = append(attachments, &uploadAttachmentResp)
}
return attachments, nil
}
func (protonDrive *ProtonDrive) sendDraft(ctx context.Context, messageID string, attachents []*proton.Attachment, config *MailSendingParameters) error {
// FIXME: repect all sendPrefs
// FIXME: respect PGPMIMEScheme, etc.
recipientPublicKeys, recipientType, err := protonDrive.c.GetPublicKeys(ctx, config.RecipientEmailAddress)
if err != nil {
return err
}
if recipientType != proton.RecipientTypeInternal {
log.Fatalln("Currently only support internal email sending")
}
recipientKR, err := recipientPublicKeys.GetKeyRing()
if err != nil {
return err
}
htmlTemplate, err := protonDrive.getHTMLBody(config)
if err != nil {
return err
}
atts, err := protonDrive.getAttachmentSessionKeyMap(attachents)
if err != nil {
return err
}
// send email
sendReq := proton.SendDraftReq{
// Packages: []*proton.MessagePackage{},
}
// for each of the recipient, we encrypt body for them
if err = sendReq.AddTextPackage(protonDrive.DefaultAddrKR,
string(htmlTemplate),
rfc822.TextHTML,
map[string]proton.SendPreferences{config.RecipientEmailAddress: {
Encrypt: true,
PubKey: recipientKR,
SignatureType: proton.DetachedSignature,
EncryptionScheme: proton.InternalScheme,
MIMEType: rfc822.TextHTML, // FIXME
}},
atts,
); err != nil {
return err
}
/* msg */
_, err = protonDrive.c.SendDraft(ctx, messageID, sendReq)
if err != nil {
return err
}
// log.Println(msg)
return nil
}