Skip to content
/ email Public

Send email via google smtp server using oauth2

Notifications You must be signed in to change notification settings

bitwitch/email

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Send email via Google's SMTP server using OAuth2

Why this package exists

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.

Setup

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.

  1. In google cloud console create a new App

  2. Create a new OAuth2 Client ID

    • APIs & Services -> Credentials -> Create Credentials -> OAuth2 Client ID
  3. 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 scope https://mail.google.com/
  4. Make sure your app has access to restricted scopes

  5. Grant the app permission to access a gmail account

    • Go through the OAuth2 flow and obtain the required credentials: ClientId,
      ClientSecret, and RefreshToken.

Usage

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)
    }
}

About

Send email via google smtp server using oauth2

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages