Skip to content

Commit c77b6ac

Browse files
committedOct 8, 2024
Added a feature and READme.md guide
1 parent 6db7238 commit c77b6ac

File tree

2 files changed

+191
-10
lines changed

2 files changed

+191
-10
lines changed
 

‎Automate Emails Daily/README.md

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Guidebook: Email Automation Script with Advanced Features
2+
3+
# Overview
4+
5+
This Python script automates the process of sending emails to multiple recipients with enhanced features such as support for attachments and HTML-formatted emails. The script uses Python's built-in libraries (smtplib, ssl, and email) to send emails securely through Gmail's SMTP server. It also allows the user to customize the email's content, subject, recipients, and more via environment variables or in-script configuration.
6+
7+
# Features Added
8+
9+
1. Support for Attachments: You can now attach files to the email, making it more versatile for different use cases like sending reports, documents, or images.
10+
2. HTML Email Support: You can send emails in HTML format, giving you more flexibility with rich text formatting, embedded links, and other styling options.
11+
12+
# Prerequisites
13+
14+
1. Python 3.x
15+
2. A Gmail account (with less secure apps access enabled or an app-specific password if using 2FA)
16+
3. Required libraries: smtplib, ssl, os, email (all built-in Python libraries)
17+
18+
# Environment Setup
19+
To ensure security, it's recommended to store sensitive information like email credentials in environment variables. For this guide, we will store the Gmail password as an environment variable:
20+
21+
export EMAIL_PASSWORD='your_gmail_password'
22+
23+
# Code Breakdown
24+
1. Import Required Modules
25+
26+
import smtplib
27+
import ssl
28+
import os
29+
from email.message import EmailMessage
30+
from email.utils import formataddr
31+
-------------------------------------------------------------------------------------
32+
smtplib: Used to create the connection to the Gmail SMTP server.
33+
ssl: Provides a layer of security for the email communication.
34+
os: Used to access environment variables (like the email password).
35+
email.message: Allows crafting email messages, including text, HTML, and attachments.
36+
37+
**send_email Function**
38+
39+
This is the main function that sends the email.
40+
41+
Function Parameters:
42+
sender_email (str): The email address sending the email.
43+
sender_name (str): The sender's name that will appear in the email.
44+
password (str): The sender's email password, pulled from environment variables.
45+
receiver_emails (list): A list of email addresses to send the email to.
46+
email_body (str): The body of the email, which can be in plain text or HTML.
47+
email_subject (str): The subject line of the email. Default is "No subject."
48+
49+
**Example Function**
50+
51+
send_email(
52+
sender_email="youremail@gmail.com",
53+
sender_name="Your Name",
54+
password=os.environ.get("EMAIL_PASSWORD"),
55+
receiver_emails=["recipient1@gmail.com", "recipient2@gmail.com"],
56+
email_body="Hello, this is a test email!",
57+
email_subject="Test Email"
58+
)
59+
60+
-------------------------------------------------------------------------------------
61+
62+
**Setting Up Email Headers**
63+
The email headers include the subject, sender, recipient, and format:
64+
65+
msg["Subject"] = email_subject
66+
msg["From"] = formataddr((f"{sender_name}", f"{sender_email}"))
67+
msg["BCC"] = sender_email
68+
msg.set_content(email_body) # This can also be an HTML body
69+
70+
-------------------------------------------------------------------------------------
71+
72+
**SMTP Server Connection**
73+
Here we establish a connection to Gmail's SMTP server and use TLS (Transport Layer Security) to ensure a secure connection.
74+
75+
smtp_port = 587
76+
smtp_server = "smtp.gmail.com"
77+
ssl_context = ssl.create_default_context()
78+
79+
-------------------------------------------------------------------------------------
80+
81+
**Login and Sending the Email**
82+
After logging in, the script loops through each recipient in the receiver_emails list and sends the email.
83+
84+
my_server = smtplib.SMTP(smtp_server, smtp_port)
85+
my_server.starttls(context=ssl_context)
86+
my_server.login(sender_email, password)
87+
88+
-------------------------------------------------------------------------------------
89+
90+
**Adding Attachments**
91+
If you want to send attachments, use the following modification:
92+
93+
if attachments:
94+
for file in attachments:
95+
with open(file, "rb") as f:
96+
file_data = f.read()
97+
file_name = os.path.basename(file)
98+
msg.add_attachment(file_data, maintype='application', subtype='octet-stream', filename=file_name)
99+
100+
-------------------------------------------------------------------------------------
101+
102+
**Sending HTML Emails**
103+
To send HTML emails, modify the email body to contain HTML:
104+
105+
msg.add_alternative("""\
106+
<html>
107+
<body>
108+
<p>Hello, <br>
109+
This is an <b>HTML email</b>!</p>
110+
</body>
111+
</html>
112+
""", subtype='html')
113+
114+
-------------------------------------------------------------------------------------
115+
116+
**Error Handling**
117+
The script includes basic error handling to notify you if the connection or email-sending process fails:
118+
119+
except Exception as e:
120+
print(f"ERROR: {e}")
121+
122+
--------------------------------------------------------------------------------------
123+
124+
**Full Example with Attachment and HTML Support**
125+
126+
send_email(
127+
sender_email="youremail@gmail.com",
128+
sender_name="Your Name",
129+
password=os.environ.get("EMAIL_PASSWORD"),
130+
receiver_emails=["recipient1@gmail.com", "recipient2@gmail.com"],
131+
email_body="<h1>This is a Test Email with HTML</h1>",
132+
email_subject="Test Email with HTML and Attachment",
133+
attachments=["path/to/attachment1", "path/to/attachment2"]
134+
)
135+
136+
137+
--------------------------------------------------------------------------------------
138+
139+
# How to run the script
140+
141+
Ensure the required environment variable (EMAIL_PASSWORD) is set.
142+
Customize the sender email, receiver emails, email body, and subject in the script.
143+
Run the script from the command line:
144+
145+
python email_automation.py or main.py
146+
147+
You can also schedule the script to run daily using cron jobs or Task Scheduler (Windows).
148+
149+
150+
# Thank You for reading this tutorial. I hope you found it helpful. If you have any questions or need further
151+

‎Automate Emails Daily/main.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,46 @@
33
import os
44
from email.message import EmailMessage
55
from email.utils import formataddr
6+
from mimetypes import guess_type
67

78
def send_email(sender_email: str,
89
sender_name: str,
910
password:str,
10-
receiver_emails: str ,
11+
receiver_emails: list,
1112
email_body: str,
12-
email_subject: str="No subject",)-> None:
13+
email_subject: str = "No subject",
14+
is_html: bool = False,
15+
attachments: list = None) -> None:
1316

1417
msg = EmailMessage()
1518
msg["Subject"] = email_subject
1619
msg["From"] = formataddr((f"{sender_name}", f"{sender_email}"))
17-
msg["BCC"] = sender_email
18-
msg.set_content(email_body)
20+
msg["BCC"] = sender_email # Can add CC or BCC here if needed
1921

22+
# Support both plain text and HTML emails
23+
if is_html:
24+
msg.add_alternative(email_body, subtype='html')
25+
else:
26+
msg.set_content(email_body)
27+
28+
# Add attachments if provided
29+
if attachments:
30+
for file_path in attachments:
31+
try:
32+
with open(file_path, 'rb') as file:
33+
file_data = file.read()
34+
file_name = os.path.basename(file_path)
35+
mime_type, _ = guess_type(file_path)
36+
if mime_type:
37+
mime_main, mime_subtype = mime_type.split('/')
38+
else:
39+
mime_main, mime_subtype = 'application', 'octet-stream'
40+
41+
msg.add_attachment(file_data, maintype=mime_main, subtype=mime_subtype, filename=file_name)
42+
print(f"Attached file: {file_name}")
43+
except Exception as e:
44+
print(f"Failed to attach {file_path}: {e}")
45+
2046
smtp_port = 587
2147
smtp_server = "smtp.gmail.com"
2248

@@ -48,14 +74,18 @@ def send_email(sender_email: str,
4874
finally:
4975
my_server.quit()
5076

51-
# change these variables to suite your requirements
77+
# Example usage
5278
sender_email = "your-email@gmail.com"
5379
sender_name = "your name"
5480
password = os.environ.get("EMAIL_PASSWORD")
5581

56-
email_subject = "good morning"
57-
email_body = "good morning, hope you have a wonderful day"
58-
59-
receiver_emails = ["receiver1-email@gmail.com", "receiver2-email@gmail.com", "receiver3-email@gmail.com"]
82+
email_subject = "Good morning"
83+
email_body = """
84+
<h1>Good Morning!</h1>
85+
<p>Hope you have a <strong>wonderful day</strong>.</p>
86+
"""
87+
receiver_emails = ["receiver1-email@gmail.com", "receiver2-email@gmail.com"]
88+
attachments = ["path/to/attachment1.pdf", "path/to/attachment2.jpg"]
6089

61-
send_email(sender_email, sender_name, password, receiver_emails, email_body,email_subject)
90+
# Sending the email as HTML with attachments
91+
send_email(sender_email, sender_name, password, receiver_emails, email_body, email_subject, is_html=True, attachments=attachments)

0 commit comments

Comments
 (0)
Please sign in to comment.