Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git send-email using Gmail
  1. Configure git.
# ~/.config/git/config
[sendemail]
  confirm = auto
  smtpServer = smtp.gmail.com
  smtpServerPort = 587
  smtpEncryption = tls
  smtpUser = <gmail email address>
  1. Configure Authentication.

I believe the simplest setup is to create an application-specific password in Google dedicated to git.

Google Account -> Security -> Signing in to Google : App passwords

This password must be configured in git, but should be kept outside of your main gitconfig (which is likely versioned via a dotfile repo?). To do that, I use git's [include] directive.

# ~/.config/git/config
[include]
  path = config.local # (relative or absolute path to the other config file)
# ~/.config/git/config.local
[sendemail]
  smtpPass = <app-specific-password>

Alternatively, check out https://github.com/google/gmail-oauth2-tools/tree/master/go/sendgmail which supports authenticating to GMail with OAuth2.

  1. Determine your revlist.

To send a single commit, just use the sha. To send a range of commits, you can use start_sha..end_sha. Most likely, you'll want to send the commits made to a branch that are missing in upstream. For that, you would use: upstream/branch_name..branch_name You can also use relative numbers to indicate the "previous X commits". e.g. git send-email -3

  1. Send email:
git send-email <revlist> --to <other@user.com>
  1. If you receive an error like this:
Can't locate Net/SMTP/SSL.pm in @INC (@INC contains: ...

You may need to upgrade/install the Net::SMTP::SSL or IO::Socket::SSL packages:

sudo -H cpan Net::SMTP::SSL
sudo -H cpan IO::Socket::SSL
  1. Advanced: configure multiple identites. You may configure multiple smtp servers and switch between them with identities.
[sendemail "gmail"]
  smtpUser = jason@gmail.com
  smtpServer = smtp.gmail.com

[sendemail "outlook"]
  smtpUser = jason@outlook.com
  smtpServer = smtp.office365.com
  
[sendemail]
  identity = outlook
@atykhyy
Copy link

atykhyy commented Feb 14, 2020

For those who don't want to use their Gmail password for this or enable "less secure apps", I wrote a plug-in for git-send-email that uses OAuth 2 authorization. It requests only the minimum required permissions (send email on your behalf) and stores OAuth token data in Windows Credential Store, like Git Credential Manager does. Google has a plug-in which works in a similar fashion, but keeps credentials in a file, and uses less restricted tokens (all of Gmail vs just send email on your behalf). It works on Linux though.

@baddos1234
Copy link

baddos1234 commented Mar 11, 2021

@ValZapod
Copy link

ValZapod commented May 18, 2021

Sigh, the problem with https://github.com/google/gmail-oauth2-tools/tree/master/go/sendgmail is that it uses outdated instruction. You need to set up the Oath content screen and even publish an app to Google now. Only after that you can download json config, ask for auth ID after sign in for that app. But it does work, which is nice.

Some problems: it shows localhost ip address (even ipv6), while it does not happen with https://mail.google.com Not nice.

On the other hand it has TLS1_3...

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Jul 3, 2021

@johnlinp, it looks like you have to either use 2-factor authentication with an app-specific password, OR allow less secure app access. See here: https://git-scm.com/docs/git-send-email#_use_gmail_as_the_smtp_server. I'd rather do the former, as it seems more-secure. I'm attempting to set this up now...

From: https://git-scm.com/docs/git-send-email#_use_gmail_as_the_smtp_server:

If you have multi-factor authentication set up on your Gmail account, you will need to generate an app-specific password for use with git send-email. Visit https://security.google.com/settings/security/apppasswords to create it.

If you do not have multi-factor authentication set up on your Gmail account, you will need to allow less secure app access. Visit https://myaccount.google.com/lesssecureapps to enable it.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Jul 3, 2021

After a great deal of effort, I got it all working. See my full instructions on Stack Overflow now, here: https://stackoverflow.com/questions/68238912/how-to-configure-and-use-git-send-email-to-work-with-gmail-to-email-patches-to/68238913#68238913

@hitmoon
Copy link

hitmoon commented Jul 16, 2021

After a great deal of effort, I got it all working. See my full instructions on Stack Overflow now, here: https://stackoverflow.com/questions/68238912/how-to-configure-and-use-git-send-email-to-work-with-gmail-to-email-patches-to/68238913#68238913

great work !

@bensuperpc
Copy link

bensuperpc commented Aug 21, 2021

On Manjaro (21.08), git config: ~/.gitconfig
And need these packages: perl-mime-tools perl-authen-sasl

Copy link

ghost commented Mar 7, 2022

I had to turn on "less secure apps" at https://myaccount.google.com/lesssecureapps to make this work.

This will no longer work after May 30, as of Google said to me.

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