-
Notifications
You must be signed in to change notification settings - Fork 5
/
mail.go
62 lines (51 loc) · 1.16 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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"gopkg.in/gomail.v1"
)
type mail struct {
fromAddr string
pass string
toAddr string
msg []byte
subject string
body string
}
func sendByGmail(m *mail) (err error) {
const (
gmailSMTPAddr = "smtp.gmail.com"
)
tempfile, err := ioutil.TempFile("", "secret-")
if err != nil {
return err
}
defer tempfile.Close()
tempfileName := tempfile.Name()
err = ioutil.WriteFile(tempfileName, m.msg, 0644)
if err != nil {
return err
}
subject := m.subject
if subject == "" {
subject = "Secret message"
}
body := m.body
if body == "" {
body = fmt.Sprintf("Please execute with attachment file to read: `openssl rsautl -decrypt -oaep -inkey <YOUR SECRET KEY> -in %s`", filepath.Base(tempfileName))
}
newMsg := gomail.NewMessage()
newMsg.SetHeader("From", m.fromAddr)
newMsg.SetHeader("To", m.toAddr)
newMsg.SetHeader("Subject", subject)
newMsg.SetBody("text/plain", body)
f, err := gomail.OpenFile(tempfile.Name())
if err != nil {
return err
}
newMsg.Attach(f)
smtpPort := 587
mailer := gomail.NewMailer(gmailSMTPAddr, m.fromAddr, m.pass, smtpPort)
return mailer.Send(newMsg)
}