Skip to content

Commit 4c7646b

Browse files
authored
Merge pull request #1014 from mihirrd/feature/automate-gmail-script
Add script to send emails via gmail
2 parents 54f41dc + d8452bd commit 4c7646b

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

gmail_automation/README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Automated Gmail sender
2+
3+
## Key features
4+
- Secure SMTP connection using TLS
5+
- Support for attachments
6+
- Error handling
7+
- Easy to integrate into other Python programs
8+
- Object-oriented design for re-usability
9+
10+
## Setup instructions
11+
12+
To use this script, you'll need to follow these steps:
13+
14+
1. First, set up Google App Password:
15+
- Go to your Google Account settings
16+
- Navigate to Security > 2-Step Verification
17+
- At the bottom, click on "App passwords"
18+
- Generate a new app password for your Python script
19+
- Save this password safely (you'll only see it once)
20+
21+
Note:
22+
In https://myaccount.google.com/security, do you see 2-step verification set to ON? If yes, then visiting https://myaccount.google.com/apppasswords should allow you to set up application specific passwords.
23+
24+
2. Install "secure-smtplib"
25+
```
26+
pip install secure-smtplib
27+
```
28+
29+
Use the script
30+
```
31+
# Create the sender object
32+
sender = GmailSender("your.email@gmail.com", "your-app-password")
33+
34+
sender.send_email(
35+
to_email="recipient@example.com",
36+
subject="Hello!",
37+
body="This is an automated email."
38+
)
39+
40+
# Use the below script to send the email via attachments
41+
sender.send_email(
42+
to_email="recipient@example.com",
43+
subject="Report",
44+
body="Please find the attached report.",
45+
attachments=["report.pdf", "data.xlsx"]
46+
)
47+
```
48+
49+
## Author
50+
Mihir Deshpande
51+
52+
## Important security notes:
53+
- Never share your app password
54+
- Don't commit the script with your credentials
55+
- Consider using environment variables for sensitive data

gmail_automation/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
secure-smtplib==0.1.1

gmail_automation/sender.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import smtplib
2+
from email.mime.text import MIMEText
3+
from email.mime.multipart import MIMEMultipart
4+
from email.mime.application import MIMEApplication
5+
import os
6+
7+
8+
class GmailSender:
9+
def __init__(self, sender_email, app_password):
10+
self.sender_email = sender_email
11+
self.app_password = app_password
12+
self.smtp_server = "smtp.gmail.com"
13+
self.smtp_port = 587
14+
15+
def create_message(self, to_email, subject, body, attachments=None):
16+
message = MIMEMultipart()
17+
message["From"] = self.sender_email
18+
message["To"] = to_email
19+
message["Subject"] = subject
20+
message.attach(MIMEText(body, "plain"))
21+
if attachments:
22+
for file_path in attachments:
23+
if os.path.exists(file_path):
24+
with open(file_path, "rb") as file:
25+
attachment = MIMEApplication(file.read(), _subtype="txt")
26+
attachment.add_header(
27+
"Content-Disposition",
28+
"attachment",
29+
filename=os.path.basename(file_path)
30+
)
31+
message.attach(attachment)
32+
33+
return message
34+
35+
def send_email(self, to_email, subject, body, attachments=None):
36+
"""
37+
Send an email through Gmail.
38+
39+
Args:
40+
to_email (str): Recipient's email address
41+
subject (str): Email subject
42+
body (str): Email body content
43+
attachments (list): List of file paths to attach (optional)
44+
"""
45+
try:
46+
message = self.create_message(to_email, subject, body, attachments)
47+
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
48+
server.starttls()
49+
server.login(self.sender_email, self.app_password)
50+
server.send_message(message)
51+
52+
print(f"Email sent successfully to {to_email}")
53+
return True
54+
55+
except Exception as e:
56+
print(f"Error sending email: {str(e)}")
57+
return False
58+
59+
60+
if __name__ == "__main__":
61+
SENDER_EMAIL = "sender-dude@gmail.com"
62+
APP_PASSWORD = "<app-password>" # Generate this from Google Account settings
63+
gmail_sender = GmailSender(SENDER_EMAIL, APP_PASSWORD)
64+
gmail_sender.send_email(
65+
to_email="receiver-dude@example.com",
66+
subject="Test Email",
67+
body="Adios!",
68+
attachments=["path/to/file.txt"] # Optional
69+
)

0 commit comments

Comments
 (0)