-
Notifications
You must be signed in to change notification settings - Fork 2
/
emailer.go
56 lines (45 loc) · 1017 Bytes
/
emailer.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
package main
import (
"crypto/tls"
"fmt"
"gopkg.in/gomail.v2"
"os"
"strconv"
)
var (
config *Config
)
type Config struct {
Host string
Username string
Password string
Port int
}
func InitConfig() {
config = &Config{
Host: os.Getenv("HOST"),
Username: os.Getenv("USERNAME"),
Password: os.Getenv("PASSWORD"),
Port: toInt(os.Getenv("PORT")),
}
}
func SendEmail(u *User) error {
m := gomail.NewMessage()
m.SetHeader("From", "info@betatude.com")
m.SetHeader("To", u.Email)
m.SetHeader("Subject", "Hello!")
m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
d := gomail.NewDialer(config.Host, config.Port, config.Username, config.Password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
fmt.Printf("Sending email to %s...\n", u.Email)
if err := d.DialAndSend(m); err != nil {
fmt.Println(err)
return err
}
fmt.Printf("Sent email to %s\n", u.Email)
return nil
}
func toInt(s string) int {
num, _ := strconv.ParseInt(s, 10, 64)
return int(num)
}