85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
import smtplib
|
|
from email.mime.text import MIMEText
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.image import MIMEImage
|
|
import os
|
|
import cairosvg
|
|
|
|
def generate_donut_svg(percentage, color="#0d6efd", size=120, thickness=4):
|
|
"""
|
|
Generate an inline SVG donut chart.
|
|
"""
|
|
svg = f"""
|
|
<svg width="{size}" height="{size}" viewBox="0 0 36 36" xmlns="http://www.w3.org/2000/svg">
|
|
<!-- Track -->
|
|
<path
|
|
d="M18 2.0845
|
|
a 15.9155 15.9155 0 0 1 0 31.831
|
|
a 15.9155 15.9155 0 0 1 0 -31.831"
|
|
fill="none"
|
|
stroke="#e9ecef"
|
|
stroke-width="{thickness}"
|
|
/>
|
|
<!-- Progress -->
|
|
<path
|
|
d="M18 2.0845
|
|
a 15.9155 15.9155 0 0 1 0 31.831
|
|
a 15.9155 15.9155 0 0 1 0 -31.831"
|
|
fill="none"
|
|
stroke="{color}"
|
|
stroke-width="{thickness}"
|
|
stroke-dasharray="{percentage}, 100"
|
|
/>
|
|
<!-- Label -->
|
|
<text x="18" y="20.35" fill="#333" font-size="5" text-anchor="middle">{percentage}%</text>
|
|
</svg>
|
|
"""
|
|
return svg
|
|
|
|
# Sender and receiver
|
|
sender_email = "marcoioitsoft@gmail.com"
|
|
receiver_emails = ["vikasnale@gmail.com", "vikas@marcoaiot.com", "umeshvdesai@outlook.com"]
|
|
password = "qrtq wfuj hwpp fhqr" # Use Gmail App Password here
|
|
|
|
# Read HTML body from file
|
|
file_path = os.path.join(os.path.dirname(__file__), "body.txt")
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
body = f.read()
|
|
|
|
# Create the email
|
|
message = MIMEMultipart("related")
|
|
message["From"] = sender_email
|
|
message["To"] = ", ".join(receiver_emails)
|
|
message["Subject"] = "Test HTML Email with Donut Charts"
|
|
|
|
# Generate SVGs and convert to PNGs
|
|
donut_svgs = [
|
|
generate_donut_svg(45, "#e63946"),
|
|
generate_donut_svg(73, "#0d6efd"),
|
|
generate_donut_svg(90, "#198754")
|
|
]
|
|
|
|
# Attach donuts as inline images
|
|
for i, svg in enumerate(donut_svgs, start=1):
|
|
png_bytes = cairosvg.svg2png(bytestring=svg.encode("utf-8"))
|
|
img = MIMEImage(png_bytes, "png")
|
|
cid = f"donut{i}"
|
|
img.add_header("Content-ID", f"<{cid}>")
|
|
message.attach(img)
|
|
body += f'<br><img src="cid:{cid}" alt="Donut{i}">'
|
|
|
|
# Attach the final HTML body
|
|
message.attach(MIMEText(f"<html><body>{body}</body></html>", "html"))
|
|
|
|
# Send the email
|
|
try:
|
|
server = smtplib.SMTP("smtp.gmail.com", 587)
|
|
server.starttls()
|
|
server.login(sender_email, password)
|
|
server.sendmail(sender_email, receiver_emails, message.as_string())
|
|
print("✅ Email sent successfully with embedded donut PNGs!")
|
|
except Exception as e:
|
|
print("❌ Error:", e)
|
|
finally:
|
|
server.quit()
|