-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
50 lines (39 loc) · 1.04 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
package email
import (
"fmt"
"log"
"net/smtp"
"os"
"strings"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}
func sendEmail(addr string, auth smtp.Auth, from string, to []string, message []byte) string {
err := smtp.SendMail(addr, auth, from, to, message)
if err != nil {
log.Printf("Error!: %s", err)
return "Error! sending mail"
}
return "email sent successfully"
}
func getAddressSMTP() string {
return os.Getenv("address")
}
func getEmailFrom() string {
return os.Getenv("username")
}
func plainAuth() smtp.Auth {
return smtp.PlainAuth(os.Getenv("identity"), os.Getenv("username"), os.Getenv("password"), os.Getenv("host"))
}
func joinMessageStructure(emailList []string, subject string, body string) []byte {
concatenate := strings.Join(emailList, ", ")
toMsg := fmt.Sprintf("To: %v\r\n", concatenate)
subjectMsg := fmt.Sprintf("Subject: %v\r\n", subject)
bodyMsg := fmt.Sprintf("%v\r\n", body)
return []byte(toMsg + subjectMsg + "\r\n" + bodyMsg)
}