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>





    Access your files securely from anywhere







                            Zoho Developer Community




                                                  • Desk Community Learning Series


                                                  • Digest


                                                  • Functions


                                                  • Meetups


                                                  • Kbase


                                                  • Resources


                                                  • Glossary


                                                  • Desk Marketplace


                                                  • MVP Corner


                                                  • Word of the Day


                                                  • Ask the Experts





                                                            Manage your brands on social media



                                                                  Zoho TeamInbox Resources



                                                                      Zoho CRM Plus Resources

                                                                        Zoho Books Resources


                                                                          Zoho Subscriptions Resources

                                                                            Zoho Projects Resources


                                                                              Zoho Sprints Resources


                                                                                Qntrl Resources


                                                                                  Zoho Creator Resources



                                                                                      Zoho CRM Resources

                                                                                      • CRM Community Learning Series

                                                                                        CRM Community Learning Series


                                                                                      • Kaizen

                                                                                        Kaizen

                                                                                      • Functions

                                                                                        Functions

                                                                                      • Meetups

                                                                                        Meetups

                                                                                      • Kbase

                                                                                        Kbase

                                                                                      • Resources

                                                                                        Resources

                                                                                      • Digest

                                                                                        Digest

                                                                                      • CRM Marketplace

                                                                                        CRM Marketplace

                                                                                      • MVP Corner

                                                                                        MVP Corner









                                                                                          Design. Discuss. Deliver.

                                                                                          Create visually engaging stories with Zoho Show.

                                                                                          Get Started Now


                                                                                            Zoho Show Resources

                                                                                              Zoho Writer

                                                                                              Get Started. Write Away!

                                                                                              Writer is a powerful online word processor, designed for collaborative work.

                                                                                                Zoho CRM コンテンツ



                                                                                                  Nederlandse Hulpbronnen


                                                                                                      ご検討中の方