Zoho email using a python or html template

Zoho email using a python or html template


# main.py

import smtplib
import csv
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from config import SENDER_EMAIL, APP_PASSWORD, SMTP_SERVER, SMTP_PORT

# email Subject
email_subject = "🎉 Python + Zoho Mail HTML Email Test"

# Read HTML email template
with open("template.html", "r", encoding="utf-8") as file:
    html_template = file.read()

# Read recipient emails from CSV
with open("contacts.csv", newline='', encoding="utf-8") as csvfile:
    reader = csv.DictReader(csvfile)
    recipients = [row["email"] for row in reader]

# Send email to each recipient
for email in recipients:
    msg = MIMEMultipart("alternative")
    msg["From"] = SENDER_EMAIL
    msg["To"] = email
    msg["Subject"] = email_subject

    # Customize message (you can replace {{name}} if you add name column)
    html_content = html_template.replace("{{name}}", email.split("@")[0])
    msg.attach(MIMEText(html_content, "html"))

    try:
        with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
            server.login(SENDER_EMAIL, APP_PASSWORD)
            server.sendmail(SENDER_EMAIL, email, msg.as_string())
            print(f"âś… Email sent successfully to {email}")
    except Exception as e:
        print(f"❌ Error sending to {email}: {e}")




# config.py
SENDER_EMAIL = "mrmishrajee@zohomail.in"
APP_PASSWORD = "YOUR_APP_PASSWORD"  # Replace with your Zoho App Password

# SMTP Settings
SMTP_SERVER = "smtp.zoho.in"
SMTP_PORT = 465



# contacts.csv
email



# template.html
​

<!DOCTYPE html>
<html>
  <body style="font-family: Arial, sans-serif; background-color: #f6f8fa; padding: 20px;">
    <div style="background-color: #fff; border-radius: 10px; padding: 20px;">
      <h2 style="color: #0066cc;">Hello {{name}},</h2>
      <p>
        This is a <strong>test email</strong> sent from Python using
        <span style="color: #ff6600;">Zoho Mail SMTP server</span>.
      </p>
      <p>Best regards,<br><b>Mr. Mishra</b></p>
    </div>
  </body>
</html>





    Nederlandse Hulpbronnen