-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
69 lines (59 loc) · 1.08 KB
/
main.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
63
64
65
66
67
68
69
package main
import (
"bytes"
"fmt"
"html/template"
"net/smtp"
"os"
"github.com/joho/godotenv"
)
func Config(key string) string {
err := godotenv.Load(".env")
if err != nil {
fmt.Print("Error loading .env file")
}
return os.Getenv(key)
}
func SendEmail() {
secretPassword := Config("EMAIL_SECRET_KEY")
auth := smtp.PlainAuth(
"",
"agustfricke@gmail.com",
secretPassword,
"smtp.gmail.com",
)
tmpl, err := template.ParseFiles("email_template.html")
if err != nil {
fmt.Println(err)
return
}
data := struct {
Name string
}{
Name: "Usuario",
}
var bodyContent bytes.Buffer
err = tmpl.Execute(&bodyContent, data)
if err != nil {
fmt.Println(err)
return
}
content := fmt.Sprintf("To: agustfricke@gmail.com\r\n"+
"Subject: Email con plantilla HTML\r\n"+
"Content-Type: text/html; charset=utf-8\r\n"+
"\r\n"+
"%s", bodyContent.String())
err = smtp.SendMail(
"smtp.gmail.com:587",
auth,
"agustfricke@gmail.com",
[]string{"agustfricke@gmail.com"},
[]byte(content),
)
if err != nil {
fmt.Println(err)
}
}
func main() {
SendEmail()
}