msmtp icon indicating copy to clipboard operation
msmtp copied to clipboard

Example call using OAuth2 on Windows and Linux/Mac?

Open Kukulkano opened this issue 11 months ago • 1 comments

Hi. I really like msmtp. Thanks for that!

I currently plan to integrate msmtp into a end user application. It is needed for sending emails based on normal TLS accounts but also on OAuth2 accounts like peoples Microsoft or gmail accounts. The final product should allow people to enter their email address and smtp server (I can store all that information in my app) and then run the OAuth2 procedure for the first sending (or if needed again).

I already have my application registration and can use Client ID and and Client Secret. Now I did some investigation and searched the internet, but there is no complete example about configuring and calling msmtp for OAuth usage. I see the use of Linux tools (secure-tool?) and sometimes some incomplete information or asking people to do some strange process to get tokens. But I still do not get who is opening the web browser for initial authentication, opens a port for getting the token and how to use that with my application. Is msmtp doing that? The official documentation is extemely short on that, mentions OAuth2 just two times.

I can't ask end users to login to their google/MS accounts and create any tokens and stuff. I know that there is a web browser related way of doing this by also opening a local port for confirmation and token receiving. Users do not have to do manual tokens for using Thunderbird, right? So there is a way.

Can you integrate some complete example about OAuth2 usage with msmtp to the documentation? Maybe for Windows and Linux/Mac to be complete? I believe that, after Google and MS now are forcing people to use OAuth2, the demand for such is increasing.

And if a developer needs to implement something like opening a web browser with a dedicated URL and run a listening port on the device to get some information, maybe you can line out this a bit? That would be soooo helpful!

Thanks for reading :)

Kukulkano avatar Feb 06 '25 08:02 Kukulkano

following - similar problem here, but was not able to solve now.. https://github.com/crazy-max/docker-msmtpd/issues/87

yayaasd avatar Feb 16 '25 07:02 yayaasd

Hi @yayaasd @Kukulkano

You can use my git-credential-outlook script here after some modifications: https://github.com/AdityaGarg8/git-credential-outlook.

Eg:

Change print(f"password={token['access_token']}") on Line 40 to print(f"{token['access_token']}")

Change the client id and add a ClientSecret variable

Replace app = PublicClientApplication(ClientId) with app = PublicClientApplication(ClientId, client_credential=ClientSecret)

AdityaGarg8 avatar Apr 28 '25 11:04 AdityaGarg8

My script will be able to easily get the refresh token, and print the access token. The refresh token is stored in your keyring.

AdityaGarg8 avatar Apr 28 '25 11:04 AdityaGarg8

https://github.com/ag91/M365-IMAP

This is an alternate way, I had taken some code from here.

AdityaGarg8 avatar Apr 28 '25 11:04 AdityaGarg8

Since the method of acquiring the OAuth2 token depends on the mail provider and can change at any time, it does not seem to make much sense to put it into the msmtp documentation - it will probably always be necessary to search for a solution on the web anyway.

marlam avatar Nov 29 '25 11:11 marlam