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")
}
@goldalworming

This comment has been minimized.

Copy link

@goldalworming goldalworming commented Nov 26, 2015

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?

@0x7b1

This comment has been minimized.

Copy link

@0x7b1 0x7b1 commented Jan 26, 2016

Golang rocks!

@tylerjin

This comment has been minimized.

Copy link

@tylerjin tylerjin commented Apr 14, 2016

Cool! It's working!
Gmail need to set allowed unsafe app

@tangingw

This comment has been minimized.

Copy link

@tangingw tangingw commented Oct 18, 2016

Just modify a little bit based on the core source code over here: https://github.com/tangingw/go_smtp

@warent

This comment has been minimized.

Copy link

@warent warent commented Dec 3, 2016

Nice one :)

@kienbui1995

This comment has been minimized.

Copy link

@kienbui1995 kienbui1995 commented Apr 14, 2017

Why do we use pass. I don't like this. Maybe key or token instead of pass.

@k0fi

This comment has been minimized.

Copy link

@k0fi k0fi commented Sep 18, 2017

This does not work for me despite the fact that I have disabled secure login. I get this error:

smtp error: 534

Any ideas?

@JoshPattman

This comment has been minimized.

Copy link

@JoshPattman JoshPattman commented Jan 3, 2018

Awesome, works perfectly

@eoinahern

This comment has been minimized.

Copy link

@eoinahern eoinahern commented Feb 11, 2018

check it out. the answer for me is in here. turn on "less secure apps" on the email account.
https://serverfault.com/questions/635139/how-to-fix-send-mail-authorization-failed-534-5-7-14
thank you please

@arumugaguru

This comment has been minimized.

Copy link

@arumugaguru arumugaguru commented Jul 11, 2018

This works great without a proxy server, but in our case, we want to send a mail via a proxy server, please let me know if this is possible and how we can do it in here.

Thanks

@TrOwX99

This comment has been minimized.

Copy link

@TrOwX99 TrOwX99 commented Oct 15, 2018

Great work! thanks for your codes

@Mooninghnk

This comment has been minimized.

Copy link

@Mooninghnk Mooninghnk commented Jun 11, 2019

Much love !! gonna make a package for gmail

@cyberience

This comment has been minimized.

Copy link

@cyberience cyberience commented Jul 24, 2019

This is deprecated, since Plain Auth is no longer available with gmail.

@mosleim

This comment has been minimized.

Copy link

@mosleim mosleim commented Nov 12, 2019

@cyberience any other ideas?

@tomVlt

This comment has been minimized.

Copy link

@tomVlt tomVlt commented Nov 19, 2019

This isn't deprecated at all. I sent email to my gmail smtp relay this morning using AUTH (RFC 2554) extension. Of course gmail is expecting a client implementing the STARTTLS extension as well, like in this gist. The only problem here is that gmail won't accept clients sending the default EHLO command set with "localhost" (it's written in their docs), which is what the smtp.SendMail() func is doing by default. In order to send a custom EHLO cmd, one need to manually instantiate a new smtp.Client and call the Hello method on it, while supplying it's own valid domain name. Otherwise one will get an EOF error.

@dilip2048

This comment has been minimized.

Copy link

@dilip2048 dilip2048 commented Nov 29, 2019

why sending mail using net/smtp goes to spam?

@MiLeung

This comment has been minimized.

Copy link

@MiLeung MiLeung commented Feb 4, 2020

I get the log error :

smtp error: 535 5.7.8 Username and Password not accepted. Learn more at
5.7.8  https://support.google.com/mail/?p=BadCredentials q15sm14168864pgm.47 - gsmtp

and an email saying: "Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access."

@shadywattay

This comment has been minimized.

Copy link

@shadywattay shadywattay commented Feb 4, 2020

@bokwoon95

This comment has been minimized.

Copy link

@bokwoon95 bokwoon95 commented Apr 3, 2020

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.

@sheetalbhusari

This comment has been minimized.

Copy link

@sheetalbhusari sheetalbhusari commented Apr 26, 2020

Although the email is getting sent. Immediately post that program exits with below error. Please suggest solution

Only one usage of each socket address (protocol/network address/port) is normally permitted.

@nedimf

This comment has been minimized.

Copy link

@nedimf nedimf commented Jun 10, 2020

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

@KaiserWerk

This comment has been minimized.

Copy link

@KaiserWerk 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

This comment has been minimized.

Copy link

@alex-hunter3 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

This comment has been minimized.

Copy link

@ashishtiwari1993 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

This comment has been minimized.

Copy link

@gocs 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

This comment has been minimized.

Copy link

@elimisteve elimisteve commented Jul 5, 2021

@ashishtiwari1993 That worked great for me, thanks!

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