From 4e69e5f1aaf167e6654aeaaf31e4b23e3514a59c Mon Sep 17 00:00:00 2001 From: aashiq-q Date: Sun, 13 Oct 2024 18:28:34 +0530 Subject: [PATCH 1/2] Added Email-alert-automation-script --- Email_alert_automation/email_alert.py | 53 +++++++++++++++++ Email_alert_automation/readme.md | 76 +++++++++++++++++++++++++ Email_alert_automation/requirements.txt | 2 + 3 files changed, 131 insertions(+) create mode 100644 Email_alert_automation/email_alert.py create mode 100644 Email_alert_automation/readme.md create mode 100644 Email_alert_automation/requirements.txt diff --git a/Email_alert_automation/email_alert.py b/Email_alert_automation/email_alert.py new file mode 100644 index 000000000..ffee4ed9c --- /dev/null +++ b/Email_alert_automation/email_alert.py @@ -0,0 +1,53 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import logging + +# Configure logging +logging.basicConfig(level=logging.INFO) + +class EmailAlert: + def __init__(self, smtp_server, smtp_port, username, password): + self.smtp_server = smtp_server + self.smtp_port = smtp_port + self.username = username + self.password = password + + def send_email(self, subject, body, recipients): + try: + # Create a multipart message + msg = MIMEMultipart() + msg['From'] = self.username + msg['To'] = ", ".join(recipients) + msg['Subject'] = subject + + # Attach the email body + msg.attach(MIMEText(body, 'plain')) + + # Set up the server connection + with smtplib.SMTP(self.smtp_server, self.smtp_port) as server: + server.starttls() # Upgrade to secure connection + server.login(self.username, self.password) + server.send_message(msg) + logging.info(f'Email sent to {", ".join(recipients)}') + + except Exception as e: + logging.error(f'Failed to send email: {e}') + +if __name__ == "__main__": + # Example configuration (replace with your actual SMTP server settings) + SMTP_SERVER = 'smtp.example.com' + SMTP_PORT = 587 + USERNAME = 'your_email@example.com' + PASSWORD = 'your_email_password' + + # Create an instance of EmailAlert + email_alert = EmailAlert(SMTP_SERVER, SMTP_PORT, USERNAME, PASSWORD) + + # Define the email content + subject = 'Critical Event Notification' + body = 'This is a notification about a critical event that requires your attention.' + recipients = ['recipient1@example.com', 'recipient2@example.com'] + + # Send the email alert + email_alert.send_email(subject, body, recipients) diff --git a/Email_alert_automation/readme.md b/Email_alert_automation/readme.md new file mode 100644 index 000000000..e528724ba --- /dev/null +++ b/Email_alert_automation/readme.md @@ -0,0 +1,76 @@ +# Email Alert Automation Script + +The Email Alert Automation Script is a Python script designed to automate the process of sending email notifications based on predefined conditions or triggers. + +## Functionalities +- Sends email notifications based on critical events such as errors or updates. +- Customizable email templates, allowing users to modify subject lines and body content. +- Supports multiple recipients for notifications. +- Includes error handling to manage failed email deliveries. +- Provides logging for successful email sends and errors. + +## Setup Instructions +To set up and run the Email Alert Automation Script on your system, follow these steps: + +1. **Clone the Repository** (if applicable): + ```bash + git clone + cd + ``` + +2. **Install Dependencies**: + Create a `requirements.txt` file with the following content (if you need additional dependencies): + ``` + # requirements.txt + logging + ``` + +3. **Install required packages**: + If you have other dependencies (in this case, none are needed beyond the standard library), install them: + ```bash + pip install -r requirements.txt + ``` + +4. **Modify Configuration**: + Update the following variables in the `email_alert.py` script: + ```python + SMTP_SERVER = 'smtp.example.com' # Replace with your SMTP server + SMTP_PORT = 587 # SMTP port (e.g., 587 for TLS) + USERNAME = 'your_email@example.com' # Your email address + PASSWORD = 'your_email_password' # Your email password + ``` + +5. **Run the Script**: + Execute the script using Python: + ```bash + python email_alert.py + ``` + +## Detailed Explanation of Script +The script leverages the `smtplib` and `email.mime.text` libraries to send email notifications. It defines a `send_email` function that constructs an email message and connects to the SMTP server to send it. + +- **Functionality**: The script can be easily customized to fit different use cases by modifying the subject, body, and recipient lists. +- **Error Handling**: It includes basic error handling to log any issues that arise during email sending. + +```python +def send_email(subject, body, recipients): + try: + ... + except Exception as e: + logging.error(f'Failed to send email: {e}') +``` + +## Output +The script sends email notifications to the specified recipients. Below is a sample output: + +![Sample Email Notification](link_to_your_image.png) + +*Replace `link_to_your_image.png` with the actual image URL showing the email notification.* + +## Author(s) +- Your Name +- Contributor Name(s) + +## Disclaimers +- Ensure that you comply with your email provider's sending limits and policies to avoid being blocked. +- Use this script responsibly and avoid spamming recipients. \ No newline at end of file diff --git a/Email_alert_automation/requirements.txt b/Email_alert_automation/requirements.txt new file mode 100644 index 000000000..737cc730a --- /dev/null +++ b/Email_alert_automation/requirements.txt @@ -0,0 +1,2 @@ +# requirements.txt +logging From 61cc4aa656dcba7b799bf4a45658a9e38d03b7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=94=20=CE=94=20S=20H=20I=20Q?= <146057447+aashiq-q@users.noreply.github.com> Date: Sun, 13 Oct 2024 18:31:07 +0530 Subject: [PATCH 2/2] Update readme.md --- Email_alert_automation/readme.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Email_alert_automation/readme.md b/Email_alert_automation/readme.md index e528724ba..c12bdf680 100644 --- a/Email_alert_automation/readme.md +++ b/Email_alert_automation/readme.md @@ -68,9 +68,8 @@ The script sends email notifications to the specified recipients. Below is a sam *Replace `link_to_your_image.png` with the actual image URL showing the email notification.* ## Author(s) -- Your Name -- Contributor Name(s) +- @aashiq-q ## Disclaimers - Ensure that you comply with your email provider's sending limits and policies to avoid being blocked. -- Use this script responsibly and avoid spamming recipients. \ No newline at end of file +- Use this script responsibly and avoid spamming recipients.