· 7 years ago · Mar 05, 2019, 06:12 AM
1package main
2
3import (
4 "fmt"
5 "net/smtp"
6)
7
8func main() {
9 err := send("hello there", "This email is from a Go application")
10 if err != nil {
11 panic(err.Error())
12 }
13}
14
15func send(to, subject, body string) error {
16 from := "...@gmail.com"
17 pass := "..."
18
19 msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\n\n%s",from,to,subject,body)
20
21 return smtp.SendMail("smtp.gmail.com:587",
22 smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
23 from, []string{to}, []byte(msg))
24}