-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutomating_Email_Reports.py
More file actions
35 lines (28 loc) · 990 Bytes
/
Automating_Email_Reports.py
File metadata and controls
35 lines (28 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv, dotenv_values
# loading variables from .env file
load_dotenv()
def send_email(subject, body, to_email):
sender_email = os.getenv("GMAIL_ID")
sender_password = os.getenv("GMAIL_APP_PASS")
receiver_email = to_email
msg = MIMEMultipart()
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, msg.as_string())
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email: {e}")
subject = 'Monthly Report1'
body = 'Here is the monthly report.'