Skip to content

Email Alert Automation Script #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Email_alert_automation/email_alert.py
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]'
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 = ['[email protected]', '[email protected]']

# Send the email alert
email_alert.send_email(subject, body, recipients)
75 changes: 75 additions & 0 deletions Email_alert_automation/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 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 <repository_url>
cd <repository_directory>
```

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 = '[email protected]' # 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)
- @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.
2 changes: 2 additions & 0 deletions Email_alert_automation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# requirements.txt
logging
Loading