Skip to content

Commit 4e69e5f

Browse files
committed
Added Email-alert-automation-script
1 parent a1068d9 commit 4e69e5f

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

Email_alert_automation/email_alert.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import smtplib
2+
from email.mime.multipart import MIMEMultipart
3+
from email.mime.text import MIMEText
4+
import logging
5+
6+
# Configure logging
7+
logging.basicConfig(level=logging.INFO)
8+
9+
class EmailAlert:
10+
def __init__(self, smtp_server, smtp_port, username, password):
11+
self.smtp_server = smtp_server
12+
self.smtp_port = smtp_port
13+
self.username = username
14+
self.password = password
15+
16+
def send_email(self, subject, body, recipients):
17+
try:
18+
# Create a multipart message
19+
msg = MIMEMultipart()
20+
msg['From'] = self.username
21+
msg['To'] = ", ".join(recipients)
22+
msg['Subject'] = subject
23+
24+
# Attach the email body
25+
msg.attach(MIMEText(body, 'plain'))
26+
27+
# Set up the server connection
28+
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
29+
server.starttls() # Upgrade to secure connection
30+
server.login(self.username, self.password)
31+
server.send_message(msg)
32+
logging.info(f'Email sent to {", ".join(recipients)}')
33+
34+
except Exception as e:
35+
logging.error(f'Failed to send email: {e}')
36+
37+
if __name__ == "__main__":
38+
# Example configuration (replace with your actual SMTP server settings)
39+
SMTP_SERVER = 'smtp.example.com'
40+
SMTP_PORT = 587
41+
USERNAME = 'your_email@example.com'
42+
PASSWORD = 'your_email_password'
43+
44+
# Create an instance of EmailAlert
45+
email_alert = EmailAlert(SMTP_SERVER, SMTP_PORT, USERNAME, PASSWORD)
46+
47+
# Define the email content
48+
subject = 'Critical Event Notification'
49+
body = 'This is a notification about a critical event that requires your attention.'
50+
recipients = ['recipient1@example.com', 'recipient2@example.com']
51+
52+
# Send the email alert
53+
email_alert.send_email(subject, body, recipients)

Email_alert_automation/readme.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Email Alert Automation Script
2+
3+
The Email Alert Automation Script is a Python script designed to automate the process of sending email notifications based on predefined conditions or triggers.
4+
5+
## Functionalities
6+
- Sends email notifications based on critical events such as errors or updates.
7+
- Customizable email templates, allowing users to modify subject lines and body content.
8+
- Supports multiple recipients for notifications.
9+
- Includes error handling to manage failed email deliveries.
10+
- Provides logging for successful email sends and errors.
11+
12+
## Setup Instructions
13+
To set up and run the Email Alert Automation Script on your system, follow these steps:
14+
15+
1. **Clone the Repository** (if applicable):
16+
```bash
17+
git clone <repository_url>
18+
cd <repository_directory>
19+
```
20+
21+
2. **Install Dependencies**:
22+
Create a `requirements.txt` file with the following content (if you need additional dependencies):
23+
```
24+
# requirements.txt
25+
logging
26+
```
27+
28+
3. **Install required packages**:
29+
If you have other dependencies (in this case, none are needed beyond the standard library), install them:
30+
```bash
31+
pip install -r requirements.txt
32+
```
33+
34+
4. **Modify Configuration**:
35+
Update the following variables in the `email_alert.py` script:
36+
```python
37+
SMTP_SERVER = 'smtp.example.com' # Replace with your SMTP server
38+
SMTP_PORT = 587 # SMTP port (e.g., 587 for TLS)
39+
USERNAME = 'your_email@example.com' # Your email address
40+
PASSWORD = 'your_email_password' # Your email password
41+
```
42+
43+
5. **Run the Script**:
44+
Execute the script using Python:
45+
```bash
46+
python email_alert.py
47+
```
48+
49+
## Detailed Explanation of Script
50+
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.
51+
52+
- **Functionality**: The script can be easily customized to fit different use cases by modifying the subject, body, and recipient lists.
53+
- **Error Handling**: It includes basic error handling to log any issues that arise during email sending.
54+
55+
```python
56+
def send_email(subject, body, recipients):
57+
try:
58+
...
59+
except Exception as e:
60+
logging.error(f'Failed to send email: {e}')
61+
```
62+
63+
## Output
64+
The script sends email notifications to the specified recipients. Below is a sample output:
65+
66+
![Sample Email Notification](link_to_your_image.png)
67+
68+
*Replace `link_to_your_image.png` with the actual image URL showing the email notification.*
69+
70+
## Author(s)
71+
- Your Name
72+
- Contributor Name(s)
73+
74+
## Disclaimers
75+
- Ensure that you comply with your email provider's sending limits and policies to avoid being blocked.
76+
- Use this script responsibly and avoid spamming recipients.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# requirements.txt
2+
logging

0 commit comments

Comments
 (0)