Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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")
}
@nedimf
Copy link

nedimf commented Jun 10, 2020

You can test it with https://mailtrap.io

@KaiserWerk
Copy link

KaiserWerk commented Aug 23, 2020

I got this error while using golang,
2015/11/26 15:04:45 smtp error: x509: certificate signed by unknown authority
testing in python show gmail works fine
any suggestion?

That just means the CA that signed the certificate isn't recognized. This is usually the case with selfsigned certificates.

@alex-hunter3
Copy link

alex-hunter3 commented Sep 3, 2020

My god you have literally no idea how long I have been trying to solve this one tiny little issue with sending the actual email and this just solved it for me. Thank you so much!

@ashishtiwari1993
Copy link

ashishtiwari1993 commented Nov 7, 2020

package main

import (
	"log"
	"net/smtp"
	"errors"
)

type loginAuth struct {
  username, password string
}

func LoginAuth(username, password string) smtp.Auth {
	return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
	return "LOGIN", []byte{}, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
	if more {
		switch string(fromServer) {
		case "Username:":
			return []byte(a.username), nil
		case "Password:":
			return []byte(a.password), nil
		default:
			return nil, errors.New("Unkown fromServer")
		}
	}
	return nil, nil
}

func main() {
	// Choose auth method and set it up
	auth := LoginAuth("user","pass")

	// Here we do it all: connect to our server, set up a message and send it
	to := []string{"to@example.com"}
	msg := []byte("To: to@example.com\r\n" +
		"Subject: New Hack\r\n" +
		"\r\n" +
		"Wonderful solution\r\n")
	err := smtp.SendMail("smtp.gmail.com:587", auth, "from@example.com", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

Wonderful hack by https://gist.github.com/andelf/5118732

I don't know why Standard library not implementing LOGIN Mechanism.

@gocs
Copy link

gocs commented May 21, 2021

gmail users: if you've seen this error: "https://support.google.com/mail/?p=InvalidSecondFactor", it means make a App Password

@elimisteve
Copy link

elimisteve commented Jul 5, 2021

@ashishtiwari1993 That worked great for me, thanks!

@kunal-pahwa
Copy link

kunal-pahwa commented Nov 11, 2021

can anyone help me to change the display name of the sender i.e if I am sending mail from xyz@gmail.com so I am seeing xyz@gmail.com in the mail list of receiver inbox but I want my custom sender name instead of xyz@gmail.com,
I am using SMTP plain auth

from:= xyz@gmail.com
auth := smtp.PlainAuth("", from, password, smtpHost)
smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, body.Bytes())

@whoiscarlo
Copy link

whoiscarlo commented Jul 7, 2022

package main

import (
	"log"
	"net/smtp"
	"errors"
)

type loginAuth struct {
  username, password string
}

func LoginAuth(username, password string) smtp.Auth {
	return &loginAuth{username, password}
}

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
	return "LOGIN", []byte{}, nil
}

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
	if more {
		switch string(fromServer) {
		case "Username:":
			return []byte(a.username), nil
		case "Password:":
			return []byte(a.password), nil
		default:
			return nil, errors.New("Unkown fromServer")
		}
	}
	return nil, nil
}

func main() {
	// Choose auth method and set it up
	auth := LoginAuth("user","pass")

	// Here we do it all: connect to our server, set up a message and send it
	to := []string{"to@example.com"}
	msg := []byte("To: to@example.com\r\n" +
		"Subject: New Hack\r\n" +
		"\r\n" +
		"Wonderful solution\r\n")
	err := smtp.SendMail("smtp.gmail.com:587", auth, "from@example.com", to, msg)
	if err != nil {
		log.Fatal(err)
	}
}

Wonderful hack by https://gist.github.com/andelf/5118732

I don't know why Standard library not implementing LOGIN Mechanism.

@ashishtiwari1993 Thank you so much for this! I was going crazy trying to figure it out!!

@luizuatanabe
Copy link

luizuatanabe commented Jul 8, 2022

Enabling "less secure apps" didn't work for me, I kept getting a Error when sending mail: 535 5.7.8 Username and Password not accepted. Learn more at 5.7.8 https://support.google.com/mail/?p=BadCredentials y19sm6477535pfe.9 - gsmtp even though that was definitely my username and password.

What did was turning turning on 2FA and generating an app password instead. The app password can be used in lieu of your actual gmail password anywhere where your password is required.

Thanks brother, this worked for me (replacing password for app password).

@parkare
Copy link

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?

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