This project automates the process of sending scheduled emails using a Python script. The email content and attachments are sent from a Gmail account to a specified receiver every day at scheduled times. The script checks if the current day is a weekday and runs the scheduled email jobs accordingly.
- Sends automated emails at 08:30, 13:45, and 19:00 every weekday.
- Reads email content from a CSV file and attaches images.
- Sends the email to a receiver and BCC to another email address.
- Uses environment variables to securely handle Gmail credentials.
Before running the application, make sure the following are installed:
- Python 3.x
- Docker (if running in a containerized environment)
schedule
: To manage the scheduling of the emails.smtplib
: To send emails.email.message
: To construct email messages.dotenv
: To load environment variables from a.env
file.os
: To access environment variables and handle file paths.csv
: To read email content from a CSV file.random
: For generating random numbers used in email subjects.datetime
: To handle date and time operations.
Use the requirements.txt
to install dependencies:
pip install -r requirements.txt
Create a .env
file at the root of the project to store your Gmail credentials and other sensitive information:
GMAIL_USERNAME=your_email@gmail.com
GMAIL_PASSWORD=your_password
Ensure the CSV file content.csv
exists in the project directory and contains the content to be included in the email body.
The images passport.png
and visa.png
should also be placed in the ./files/
directory for attachment.
- Imports necessary libraries:
send_email
,schedule
,time
, anddatetime
. - Job function: Calls
send_email()
function to send the email. - Scheduler Setup: The
run_scheduler()
function schedules the job to run three times daily at 08:30, 13:45, and 19:00. It ensures that the script only runs on weekdays (Monday to Friday). - Main function: Checks if today is a weekend (Saturday or Sunday) and exits the script if it is. Otherwise, it starts the scheduler.
- Reset Function: The
reset()
function creates an email message with a randomly generated subject and reads email content fromcontent.csv
. It also attachespassport.png
andvisa.png
from the./files/
directory. - Send Email: The
send_email()
function logs into Gmail via SMTP, sends the email message, and prints "Sent..." when successful.
If you want to run the application in a Docker container, use the following commands:
-
Build the Docker image:
docker build --no-cache -t email-scheduler .
-
Run the container with the following command:
docker run -d --restart always email-scheduler
The Dockerfile
defines the steps to build the Docker image for this project:
FROM python:3.11-bullseye
RUN apt-get update && apt-get install -y --no-install-recommends --no-install-suggests build-essential && pip install --no-cache-dir --upgrade pip
WORKDIR /app
COPY ./requirements.txt /app
RUN pip install --no-cache-dir --requirement /app/requirements.txt
COPY . /app
CMD [ "python3", "./app.py"]
-
Set up your environment variables in
.env
. -
Ensure the CSV file and image files are in place.
-
Run the application:
python3 app.py
- Build and run the Docker container as described above.
The scheduler will start running and will send emails at the defined times during weekdays.
- Email not sent: Make sure the Gmail username and password are correct in the
.env
file. If you're using 2FA for Gmail, you may need to create an app-specific password. - Missing Files: Ensure that
content.csv
and the image files (passport.png
,visa.png
) are present in the correct directories (./files/
for images). - Scheduling issues: Check if your system time is synchronized properly. You can also log the times the script runs for debugging.
This project is licensed under the MIT License - see the LICENSE file for details.