-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
59 lines (50 loc) · 1.69 KB
/
send_email.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import smtplib
import yaml
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def send_email(subject = "", body = ""):
# Load configuration from config.yaml
with open('config/config.yaml', 'r') as file:
config = yaml.safe_load(file)
# Gmail credentials
email_sender = config['gmail']['email_sender']
email_password = config['gmail']['email_password']
email_receiver = config['gmail']['email_receiver']
# Create a sample DataFrame
# data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [24, 30, 22]}
# df = pd.DataFrame(data)
# Create the email content
html_body = f"""
<html>
<head></head>
<body>
{body}
</body>
</html>
"""
# {df.to_html(index=False)}
# <img src="cid:image1" alt="Embedded Image"/>
# Create the email message
msg = MIMEMultipart('related')
msg['From'] = email_sender
msg['To'] = email_receiver
msg['Subject'] = subject
# Attach the HTML content
msg_html = MIMEText(html_body, 'html')
msg.attach(msg_html)
# Attach the image, optionally
# with open('img/logo.jpg', 'rb') as img_file:
# img = MIMEImage(img_file.read())
# img.add_header('Content-ID', '<image1>')
# msg.attach(img)
# Send the email using Gmail's SMTP server
try:
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls() # Secure the connection
server.login(email_sender, email_password)
server.send_message(msg)
print(f"Email sent successfully")
except Exception as e:
print(f"Failed to send email: {e}")