Gmail Oauth with Script

lurker

Cadet
Joined
Mar 19, 2013
Messages
8
Hi All,

I have a freebsd jail that runs a script and then sends an email to a gmail account.
This has worked well for many years using SSMTP but Google have disabled IMAP and require OAuth2 authentication.
I have updated my System > Email settings to send with Gmail Oauth which works well for system alerts but I'm wondering if I can use that configuration to send emails from my script.
Anyone been able to do something like this or advise on how to send email to gmail from a script?

Thanks
 

Nick2253

Wizard
Joined
Apr 21, 2014
Messages
1,633
When I need to programmatically send email, I use Python with the Google API: https://developers.google.com/gmail/api/quickstart/python

Over the last few years, I've almost entirely replaced my shell scripts with Python. It's a first-class citizen on nearly every *nix system out there, and pip makes module management quite easy.
 

lurker

Cadet
Joined
Mar 19, 2013
Messages
8
When I need to programmatically send email, I use Python with the Google API: https://developers.google.com/gmail/api/quickstart/python

Over the last few years, I've almost entirely replaced my shell scripts with Python. It's a first-class citizen on nearly every *nix system out there, and pip makes module management quite easy.
Thanks Nick, appreciate you taking the time to reply.
Yes looks like it is time to move my scripts to python, thanks for the link.

Cheers
 

Richardszu

Dabbler
Joined
Jan 21, 2022
Messages
49
When I need to programmatically send email, I use Python with the Google API: https://developers.google.com/gmail/api/quickstart/python

Over the last few years, I've almost entirely replaced my shell scripts with Python. It's a first-class citizen on nearly every *nix system out there, and pip makes module management quite easy.
Hello @Nick2253,
I came across your post by chance looking for how to send an email with an attachment. Could you give me an example of a pyton script to do this simply STP! Richard have a good day
 

Nick2253

Wizard
Joined
Apr 21, 2014
Messages
1,633

Richardszu

Dabbler
Joined
Jan 21, 2022
Messages
49
The Google API docs do a great job of walking you through everything:

Thanks @Nick2253 for your reply,
You also made me discover "Gmail for developers":), thank you for your script. I'm a beginner and I started taking lessons with Python by chance too, a little thanks to you!
My current work environment:
Windows 10 with Python 3.10.8
Vscode with Microsoft's Python Plugin
Windows Terminal WSL Debian, Ubuntu
I created a Nextcloud backup Bash script under FreeBSD with TrueNas Core 12 for my DIY NAS. Now I would like to include the Python file in the script to send an email with a log attachment.
I copied your code in vscode but, I have an error message at line 6 import google.auth
An exception occurred: ModuleNotFoundError
No module named 'google'
File "J:\My Drive\Perso\Richard\! Create-Nas-DIY-Perso-Ressources\Python\Test-004.py", line 6, in <module>
import google.auth
Could you please help me to solve this problem!
Have a nice day Richard
 

Nick2253

Wizard
Joined
Apr 21, 2014
Messages
1,633
You need to install the correct packages using pip.

So as to not monkey with the stock Python install, I recommend creating a virtual environment using the venv package.
 

Richardszu

Dabbler
Joined
Jan 21, 2022
Messages
49
You need to install the correct packages using pip.

So as to not monkey with the stock Python install, I recommend creating a virtual environment using the venv package.
Thanks @Nick2253 for your reply,
I'm new to Python and I don't know what you're talking about "packages using pip" Can you send me a link to understand how to do it STP! When you talk about virtual environment? I'm currently using Windows Terminal with PowerShell, Ubuntu, Debian and have the latest version of Python installed. Do you recommend another way to do it? Maybe I'm not using the right way, I'm sorry, but I'm ready to change or evolve. Thank you for your help, have a good day Richard :)
 
Top