Usecase - User wants to pull data from client source (Web, email, servers, shared locations, etc) on daily basis and need to ETL process, information retrivals and generate report. This report is of any type (Ex. Html file, Excel file, etc) and for further client activities, we need to notify by mail to team members/ HODs on daily basis as per the reqiorements.
- Google has announced that itβs disabling the Less Secure Apps feature on some Google accounts from May 30th, 2022. If youβre using Gmail SMTP details with our Other SMTP mailer, you may have difficulty sending emails when this feature is disabled. What next??
- With growing awareness of security, itβs highly recommended to have 2-Step Verification set up in your Google account, if not already. However, with 2-Step Verification enabled, youβll realise that:
- Less secure app accessβ option is no longer visible in Google
- Programmatic login with your Google password only satisfies one of the 2-Step Verification
Essentially, you are blocked from accessing your Google Account programmatically now. So, how do we proceed from here now?
Automate.reprot.via.mail.demo.mp4
Structure
A) Generating App Passwords from Google
B) Programming: Setup script and send email in Python
C) Adding script in Task Scheduler for automation
A) Generating App Passwords from Google: First, I recommend setting up a new Google account as a development environment for this tutorial instead of direct implementation in your desired (production/personal) Google Gmail account.
- i. Set up a Google account. Skip this if you are using an existing Gmail account, and skip to point 4 if the account has 2-Step Verification enabled.
- ii) Once logged on to your Google account, navigate to Manage your Google Account -> Security -> Privacy Setting
- iii) Setup 2-Step Verification by following the on-screen instructions. Once you have 2-Step Verification enabled, the App Passwords option should be visible.
- iv) Under App Passwords, select Other (Custom name) from the Select app dropdown list.
- v) Give it a name for your own identification. Click on GENERATE and a 16-character code (in the format of xxxx-xxxx-xxxx-xxxx) should be generated. This code is important for programatically access.
B) Programming: Setup script and send email in Python: We will be using the built-in module for sending emails via SMTP. SMTP is the protocol used for sending emails and routing emails between mail servers.
-
Create virtual environment
python -m venv VIRTUAL_ENV_NAME
and activate it.\VIRTUAL_ENV_NAME\Scripts\activate
. -
Install necessary library for this project from the file
requirements.txt
or manually install bypip
.pip install -r requirements.txt
To create project library requirements, use below command,
pip freeze > requirements.txt
-
Store your own 16-character code (xxxx-xxxx-xxxx-xxxx) generated from Google in credentials JSON file into pass key without '-'. Also mentioned email person details To, CC, BCC as per below.
{ "from": "XYZ@gmail.com", "pass": "xxxxxxxxxxxxxxxx", "to": "ABC@gmail.com", "cc": ["ABC1@gmail.com", "ABC2@gmail.com"], "bcc": ["ABC3@gmail.com", "ABC4@gmail.com"] }
-
Configure input and output path with data as per requirements in code file.
SOURCE_NODE = './sourceNode/' # Input directory / data DESTINATION_NODE = './destinationNode/' # Output directory / data # Email body text1 = ''' Hi Team, Good Morning, Please find below report. ''' # Email configuration setup context = ssl.create_default_context() server = smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) server.login(sender_id, sender_pass)
-
Run app.py python file locally in machine. Once all set, check mails, windows notification and then we will move to automate the process by Schedulling job.
python run app.py
Once all set, check mails, windows notification and then we will move to automate the process by Scheduling job.
C) Adding script in Task Scheduler for automation: Here I have created video for this process automation on daily basis for a references (System: Windows 10).
-
Create task from Task Scheduler (Add name, description, machine settings)
-
Configure trigger settings on daily/ weekly/ specific day with time and repeatition for job.
-
Provide script path, script name and python path for python application.
C:\Users...\Scripts\python.exe -> python cmd enabled
C:\Users...\Scripts\pythonw.exe -> python cmd disabled
Automate.using.Task.Scheduler.mp4
. # Root directory
βββ sourceNode # Data source directory
β βββus-500.csv # Data file - input
βββ destinationNode # Data destination directory
β βββoutput.html # Data file - output
βββ app.py # Script file
βββ credentials.json # Email credentials
βββ requirements.txt # Project requirements library with versions
βββ README.md # Project README file
βββ LICENSE # Project License file
You say freak, I say unique. Don't wait for an opportunity, create it.
Letβs connect, share the ideas and feel free to ping me...