Skip to content

Instantly share code, notes, and snippets.

@jpillora
Last active July 19, 2025 18:39
Show Gist options
  • Select an option

  • Save jpillora/cb46d183eca0710d909a to your computer and use it in GitHub Desktop.

Select an option

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")
}
@parkare
Copy link
Copy Markdown

parkare commented Sep 30, 2022

Hi @luizuatanabe. The same thing happens to me. Due to Google's policy change in May 2022, it is now no longer possible to disable the "less secure apps" option. Do you know if there is a way to perform this validation to send emails?

@gfeyer
Copy link
Copy Markdown

gfeyer commented Oct 23, 2022

@parkare same thing here. Did you figure out a solution?

@erielmejias99
Copy link
Copy Markdown

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

@saikumar1607
Copy link
Copy Markdown

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!!

@lib4u
Copy link
Copy Markdown

lib4u commented Sep 26, 2024

upd: this work for me, need add parametr from in msg object
msg := []byte("From: ***@mail.com\r\n" +
"To: ***@gmail.com\r\n" +
"Subject: New Hack\r\n" +
"\r\n" +
"Wonderful solution\r\n")

@mdutkin
Copy link
Copy Markdown

mdutkin commented Jul 19, 2025

FYI here's a link to the app passwords: https://myaccount.google.com/apppasswords

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