-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.go
39 lines (33 loc) · 969 Bytes
/
email.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
package email
import (
"net/mail"
"net/smtp"
"time"
"github.com/forbole/bookkeeper/types"
"github.com/scorredoira/email"
)
// SendEmail send an an email by Gmail. It attach an zip which compress the provided file
func SendEmail(emailDetails types.EmailDetails, path []string) error {
// compose the message
if len(emailDetails.To)==0{
return nil
}
m := email.NewMessage(emailDetails.Subject, emailDetails.Details)
m.From = mail.Address{Name: emailDetails.From.Name, Address: emailDetails.From.Address}
m.To = emailDetails.To
//zip the files
zipName := "bookkeeper_" + time.Now().String() + ".zip"
if err := ZipFiles(zipName, path); err != nil {
return err
}
// add attachments
if err := m.Attach(zipName); err != nil {
return nil
}
// send it
auth := smtp.PlainAuth("", emailDetails.From.Name, emailDetails.From.Password, "smtp.gmail.com")
if err := email.Send("smtp.gmail.com:587", auth, m); err != nil {
return err
}
return nil
}