README
¶
sendgmail
sendgmail is a tool that enables git send-email
to send patches from a Gmail account without using SMTP.
This tool has been modified from its original version. This new version, inspired by siketyan/sendgmail, uses the Gmail users.messages.send API to send the email instead of using SMTP. This has the following advantages:
-
The scope of the OAuth token can be limited to only sending emails. This avoids the potential danger created by generating a token with full Gmail permissions, which is necessary to send emails through Gmail using SMTP (when not using an app password, which grants even more permissions).
-
Sent emails will always appear in the "Sent" folder in Gmail regardless of whether or not you CC yourself. In contrast, when a message is sent via SMTP, the message will not appear in the "Sent" folder unless you CC yourself.
Getting Started
Step 1: Install sendgmail
go install github.com/cyrozap/sendgmail@latest
This will install sendgmail
to $GOPATH/bin/sendgmail
.
Step 2: Create Google OAuth2 Client and Credentials
-
Create a Google Cloud project
- Visit the Google Cloud console.
- Open the project picker (to the right of the Google Cloud logo, or press Ctrl+O).
- Create a new project (name it e.g.
sendgmail
). - In the Notifications window, wait for the project to finish being created, then click Select Project when the button appears.
-
Enable the Gmail API
- Click this link to begin the process to enable the Gmail API for your
sendgmail
project. - Click Next, then click Enable.
- Click this link to begin the process to enable the Gmail API for your
-
Create the OAuth configuration
- Open Navigation menu (≡) > APIs & Services > OAuth consent screen.
- Under where it says "Google Auth Platform not configured yet", click Get started.
- Set the App name to
sendgmail
. - Set the User support email to your own email.
- Click Next.
- Set Audience to External, then click Next.
- Set the Contact Information email address to your own email, then click Next.
- Read and agree to the Google API Services: User Data Policy, then click Continue and then Create.
-
Create the OAuth client and download credentials
-
Click Create OAuth client.
-
Set Application type to Web application
-
Set the Name to
sendgmail
, or leave it as-is. -
Under Authorized redirect URIs, click Add URI.
- If you're using
sendgmail -setup
on the same computer that your browser is logged in to, set the URI tohttp://localhost:8080/oauth2callback
.- This will configure
sendgmail -setup
to listen on that port for your browser to send it the authorization code. - You can change the port to another number if
sendgmail -setup
is not able to listen on port8080
on your computer. - You can also set a loopback IP (like
127.0.0.1
or[::1]
) as the host instead oflocalhost
, if you prefer.
- This will configure
- If you need to run
sendgmail -setup
on a different computer from the one that your browser is on, set the URI tohttps://google.github.io/gmail-oauth2-tools/html/oauth2.dance.html
.- This will cause the authorization code to be displayed in your browser, and from there you'll have to copy and paste or manually enter the code into
sendgmail -setup
.
- This will cause the authorization code to be displayed in your browser, and from there you'll have to copy and paste or manually enter the code into
- If you're using
-
Click Create.
-
In the window that appears, click Download JSON to download the OAuth client credentials.
-
Move the credentials JSON into the
sendgmail
configuration directory:CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/sendgmail" mkdir -p "$CONFIG_DIR" chmod 700 "$CONFIG_DIR" # Move the credentials file (must be named "config.json") mv path/to/credentials.json "$CONFIG_DIR"/config.json chmod 600 "$CONFIG_DIR"/config.json
-
-
Enable the
gmail.send
API scope- Click Data Access, then click Add or remove scopes.
- Where it says Enter property name or value, type
gmail.send
and press the Enter/Return key. - Check the checkbox just to the left of where it says "Gmail API" in the single row that should be visible.
- Click Update, then click Save.
-
Add yourself as a test user
- Click Audience.
- Under Test users, click Add users.
- Enter your own email, then click Save.
Step 3: Generate OAuth2 Token
Run the following command to perform the OAuth2 authorization:
# Replace with your Gmail address
$GOPATH/bin/sendgmail -sender=USERNAME@gmail.com -setup
Follow the remaining steps in your browser.
If you set the Authorized redirect URI to a local URL, then at the end of the process the token will automatically be sent to sendgmail
.
If you instead set it to .../oauth2.dance.html
, then you'll have to copy the code and paste it where prompted by sendgmail
.
Usage with Git
[!NOTE] This assumes you've completed the OAuth2 setup.
Add the following section to .gitconfig
in your home directory:
[sendemail]
smtpServer = /full/path/to/sendgmail # From $GOPATH/bin/sendgmail or custom path
smtpServerOption = -sender=USERNAME@gmail.com