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

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

commented Jan 26, 2016

Golang rocks!

@tylerjin

This comment has been minimized.

Copy link

commented Apr 14, 2016

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

@tangingw

This comment has been minimized.

Copy link

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

commented Dec 3, 2016

Nice one :)

@kienbui1995

This comment has been minimized.

Copy link

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

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

commented Jan 3, 2018

Awesome, works perfectly

@eoinahern

This comment has been minimized.

Copy link

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

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

commented Oct 15, 2018

Great work! thanks for your codes

@Mooninghnk

This comment has been minimized.

Copy link

commented Jun 11, 2019

Much love !! gonna make a package for gmail

@cyberience

This comment has been minimized.

Copy link

commented Jul 24, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.