-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
42 lines (31 loc) · 785 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
40
41
42
package main
import (
"fmt"
"net/smtp"
"github.com/astaxie/beego/config"
)
// Send an email
func Send(body, title, to string) error {
conf, err := config.NewConfig("ini", "smtp.conf")
if err != nil {
return err
}
mime := "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
from := conf.String("smtp::email")
msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\n%s%s",
from, to, title, mime, body)
port, err := conf.Int("smtp::port")
if err != nil {
return err
}
addr := fmt.Sprintf("%s:%d",
conf.String("smtp::server"), port)
auth := smtp.PlainAuth("",
from, conf.String("smtp::password"),
conf.String("smtp::server"))
err = smtp.SendMail(addr, auth, from, []string{to}, []byte(msg))
if err != nil {
return err
}
return nil
}