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""" {percentage}% """ 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'
Donut{i}' # Attach the final HTML body message.attach(MIMEText(f"{body}", "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()