My use case is I want to send email programatically. Many of my programs that
run daily have code paths that send email to certain members of my team when
errors occur or to notify them of certain events.
In the past you could just use a gmail username and password and very easily
send email via google's SMTP server. However, Google recently deprecated simple
username and password authentication, so now the only real option for
programatically sending email from a gmail account is oauth2. Go has an SMTP
package, but it does not support authenticating with the SMTP server via OAUTH.
So I wrote this little package to allow my programs to send email from a gmail
account that I control.
You must generate a credentials file in order to be able to authenticate with
the SMTP server. This involves setting up an "app" on google, setting the
appropriate permissions scope, and then granting the app permision to send
email via a particular gmail account.
-
In google cloud console create a new App
-
Create a new OAuth2 Client ID
- APIs & Services -> Credentials -> Create Credentials -> OAuth2 Client ID
-
Set up the OAuth consent screen
- Here you can setup the redirect url where you want google to send users
after they grant permission to your app (note that in my case, there
arent really external "users", "users" is just me using the one gmail
account I want to send email from). - You probably want to setup a small site that parses the query parameters
and constructs a credentials file out of it. I made a page on the Otto
Site that does this. - You must also set the scopes that your app needs access to here. Sending
email via SMTP requires the restricted scopehttps://mail.google.com/
- Here you can setup the redirect url where you want google to send users
-
Make sure your app has access to restricted scopes
- In order to use restricted scopes, the administrator of the google
workspace must grant internal apps permission to use restricted google
workspace apis. (This implies that you want to configure the app to be an
internal app that is only used by an internal team and not meant for
public users).
- In order to use restricted scopes, the administrator of the google
-
Grant the app permission to access a gmail account
- Go through the OAuth2 flow and obtain the required credentials: ClientId,
ClientSecret, and RefreshToken.
- Go through the OAuth2 flow and obtain the required credentials: ClientId,
Remember that you must first make a single call to Init and pass in the from
address and a path to the credentials file. This initializes the email client.
Then you can simply call Send to send emails. You can send attachments as well
by passing in an array of filepaths as the 'attachments' argument to Send.
import "github.com/bitwitch/email"
func main() {
err := email.Init("[email protected]", "path_to_credentials_file.json")
if err != nil {
panic(err)
}
subject := "Subject"
messages := []string{"message number one", "message number two"}
attachments := []string{}
recipients := []string{"[email protected]"}
err = email.Send(subject, messages, attachments, recipients)
if err != nil {
panic(err)
}
}