-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.py
More file actions
33 lines (29 loc) · 1.15 KB
/
mail.py
File metadata and controls
33 lines (29 loc) · 1.15 KB
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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import os
def email_alert(subject, body, to, attachment_path=None):
msg = MIMEMultipart()
msg['From'] = 'geerthi006@gmail.com'
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
if attachment_path and os.path.isfile(attachment_path):
part = MIMEBase('application', 'octet-stream')
with open(attachment_path, 'rb') as attachment:
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename={os.path.basename(attachment_path)}')
msg.attach(part)
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(msg['From'], 'vzll fxjh omys spmo')
text = msg.as_string()
server.sendmail(msg['From'], to, text)
server.quit()
print("Email sent successfully!")
except Exception as e:
print(f"Failed to send email. Error: {e}")