diff --git a/gmail_automation/README.md b/gmail_automation/README.md new file mode 100644 index 00000000..b202001b --- /dev/null +++ b/gmail_automation/README.md @@ -0,0 +1,55 @@ +# Automated Gmail sender + +## Key features +- Secure SMTP connection using TLS +- Support for attachments +- Error handling +- Easy to integrate into other Python programs +- Object-oriented design for re-usability + +## Setup instructions + +To use this script, you'll need to follow these steps: + +1. First, set up Google App Password: +- Go to your Google Account settings +- Navigate to Security > 2-Step Verification +- At the bottom, click on "App passwords" +- Generate a new app password for your Python script +- Save this password safely (you'll only see it once) + +Note: +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. + +2. Install "secure-smtplib" +``` +pip install secure-smtplib +``` + +Use the script +``` +# Create the sender object +sender = GmailSender("your.email@gmail.com", "your-app-password") + +sender.send_email( + to_email="recipient@example.com", + subject="Hello!", + body="This is an automated email." +) + +# Use the below script to send the email via attachments +sender.send_email( + to_email="recipient@example.com", + subject="Report", + body="Please find the attached report.", + attachments=["report.pdf", "data.xlsx"] +) +``` + +## Author +Mihir Deshpande + +## Important security notes: +- Never share your app password +- Don't commit the script with your credentials +- Consider using environment variables for sensitive data \ No newline at end of file diff --git a/gmail_automation/requirements.txt b/gmail_automation/requirements.txt new file mode 100644 index 00000000..5146fef9 --- /dev/null +++ b/gmail_automation/requirements.txt @@ -0,0 +1 @@ +secure-smtplib==0.1.1 \ No newline at end of file diff --git a/gmail_automation/sender.py b/gmail_automation/sender.py new file mode 100644 index 00000000..706482bd --- /dev/null +++ b/gmail_automation/sender.py @@ -0,0 +1,69 @@ +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart +from email.mime.application import MIMEApplication +import os + + +class GmailSender: + def __init__(self, sender_email, app_password): + self.sender_email = sender_email + self.app_password = app_password + self.smtp_server = "smtp.gmail.com" + self.smtp_port = 587 + + def create_message(self, to_email, subject, body, attachments=None): + message = MIMEMultipart() + message["From"] = self.sender_email + message["To"] = to_email + message["Subject"] = subject + message.attach(MIMEText(body, "plain")) + if attachments: + for file_path in attachments: + if os.path.exists(file_path): + with open(file_path, "rb") as file: + attachment = MIMEApplication(file.read(), _subtype="txt") + attachment.add_header( + "Content-Disposition", + "attachment", + filename=os.path.basename(file_path) + ) + message.attach(attachment) + + return message + + def send_email(self, to_email, subject, body, attachments=None): + """ + Send an email through Gmail. + + Args: + to_email (str): Recipient's email address + subject (str): Email subject + body (str): Email body content + attachments (list): List of file paths to attach (optional) + """ + try: + message = self.create_message(to_email, subject, body, attachments) + with smtplib.SMTP(self.smtp_server, self.smtp_port) as server: + server.starttls() + server.login(self.sender_email, self.app_password) + server.send_message(message) + + print(f"Email sent successfully to {to_email}") + return True + + except Exception as e: + print(f"Error sending email: {str(e)}") + return False + + +if __name__ == "__main__": + SENDER_EMAIL = "sender-dude@gmail.com" + APP_PASSWORD = "" # Generate this from Google Account settings + gmail_sender = GmailSender(SENDER_EMAIL, APP_PASSWORD) + gmail_sender.send_email( + to_email="receiver-dude@example.com", + subject="Test Email", + body="Adios!", + attachments=["path/to/file.txt"] # Optional + )