Skip to content

Instantly share code, notes, and snippets.

@nguyendangminh
Created August 4, 2017 08:02
Show Gist options
  • Save nguyendangminh/77eef3815b3a2659144bd2837ff8a7c0 to your computer and use it in GitHub Desktop.
Save nguyendangminh/77eef3815b3a2659144bd2837ff8a7c0 to your computer and use it in GitHub Desktop.
Send SMTP email in Go
package main
import (
"fmt"
"log"
"net/smtp"
)
const (
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 587
)
func send(from, pass, to, subject, body string) error {
msg := fmt.Sprintf("From: %s\nTo: %s\nSubject: %s\n\n%s", from, to, subject, body)
auth := smtp.PlainAuth("", from, pass, SMTP_SERVER)
return smtp.SendMail(fmt.Sprintf("%s:%d", SMTP_SERVER, SMTP_PORT), auth, from, []string{to}, []byte(msg))
}
func main() {
from := "abc@gmail.com"
pass := "secret_password"
to := "xyz@gmail.com"
subject := "Hi there"
body := "This is body"
if err := send(from, pass, to, subject, body); err != nil {
log.Fatal(err)
}
log.Println("Done")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment