Created
August 4, 2017 08:02
-
-
Save nguyendangminh/77eef3815b3a2659144bd2837ff8a7c0 to your computer and use it in GitHub Desktop.
Send SMTP email in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 := "[email protected]" | |
pass := "secret_password" | |
to := "[email protected]" | |
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