Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active March 5, 2024 21:26
Show Gist options
  • Save jpillora/cb46d183eca0710d909a to your computer and use it in GitHub Desktop.
Save jpillora/cb46d183eca0710d909a to your computer and use it in GitHub Desktop.
Send email using Go (Golang) via GMail with net/smtp
package main
import (
"log"
"net/smtp"
)
func main() {
send("hello there")
}
func send(body string) {
from := "...@gmail.com"
pass := "..."
to := "foobarbazz@mailinator.com"
msg := "From: " + from + "\n" +
"To: " + to + "\n" +
"Subject: Hello there\n\n" +
body
err := smtp.SendMail("smtp.gmail.com:587",
smtp.PlainAuth("", from, pass, "smtp.gmail.com"),
from, []string{to}, []byte(msg))
if err != nil {
log.Printf("smtp error: %s", err)
return
}
log.Print("sent, visit http://foobarbazz.mailinator.com")
}
@erielmejias99
Copy link

The "less secure apps" is available for google enterprise accounts.

@saikumar1607
Copy link

https://gist.github.com/jpillora/cb46d183eca0710d909a?permalink_comment_id=3519541#gistcomment-3519541

This worked! Instead of password generate a app password and use it as password. Works like charm!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment