fbpx
Skip to content

Send Zoho Email Using Python Script

    Send Email Using Python Script

    Last Updated on: 10th November 2023, 07:12 pm

    Send Thousands of emails using the custom email service Zoho with Python. With this Python code, you can send multiple emails just like an email marketing tool. The best part is it’s free and you can customize your emails using HTML and CSS.

    Send Emails using Python for Zoho Mail Service – Complete Code

    Below is a code snippet showcasing how to send emails using a custom Zoho email ID. In this code, we’ll read recipient email addresses from a TXT file and send an email to each one.

    import smtplib
    import csv
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    # Zoho mail SMTP server settings
    smtp_server = "smtp.zoho.com"
    smtp_port = 465
    
    # Email and password for the Zoho account
    username = "[email protected]"
    password = "yourpassword"
    
    # Email Content
    subject = 'Hello'
    body = 'This is a test email.'
    
    def send_mail(to_email, subject, body):
        # Creating a email message
        msg = MIMEMultipart()
        msg['From'] = username
        msg['To'] = to_email
        msg['Subject'] = subject
        msg.attach(MIMEText(body, 'plain'))
    
        # Starting the Zoho SMTP server
        server = smtplib.SMTP_SSL(smtp_server,smtp_port)
    
        # Login to Zoho SMTP server
        server.login(username, password)
    
        # Sending email
        server.sendmail(username, to_email, msg.as_string())
    
        # Quitting the SMTP server
        server.quit()
    
    # Reading emails from text file &
    # Sending email to each one
    with open("emails.txt","r") as f:
        emails = [line.strip() for line in f.readlines()]
        for email in emails:
            try:
                send_mail(email,subject,body)
                print(f'Email sent to {email}')
            except Exception as e:
                print(f'Error: {e}')

    Please replace '[email protected]' and 'yourpassword' with your Zoho email id and password. Also replace, 'Hello' and 'This is a test email.' with your email subject and body, respectively.

    The open("emails.txt","r") loads in a TXT file, ’emails.txt’, in read mode. You have to place this file in the same directory as your python file or you need to give the full path to the TXT file.

    This script reads the email addresses from ’emails.txt’, loops over these email addresses and for each address, sends an email.

    Send Mails Using Python Zoho Service

    import smtplib
    import csv
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    The code above imports necessary Python libraries. The smtplib module defines an SMTP (Simple Mail Transfer Protocol) client session object used for sending mail. The csv module will be used for reading email addresses from a file. The email.mime.multipart and email.mime.text modules are used to compose the email message.

    smtp_server = "smtp.zoho.com"
    smtp_port = 465
    username = "[email protected]"
    password = "yourpassword"

    These lines save the Zoho SMTP settings for server and port number. It also saves the email and password of the Zoho account. This email address will be used as the sender’s email address.

    subject = 'Hello'
    body = 'This is a test email.'

    These lines define the subject and the body for the email.

    def send_mail(to_email, subject, body):

    Define a new function called send_mail which takes three parameters: to_email (the recipient’s email id), subject (the email subject), and body (the email body).

    msg = MIMEMultipart()
    msg['From'] = username
    msg['To'] = to_email
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))

    In these lines, we create a new instance of the MIMEMultipart class (email message object). We set its From, To, and Subject fields. The email’s body message is attached to the MIMEMultipart instance using the attach method.

    server = smtplib.SMTP_SSL(smtp_server,smtp_port)

    Create a new SMTP instance which encapsulates an SMTP connection. We use SMTP_SSL for secure connections.

    server.login(username, password)

    The login method logs in to the email server using your email id and password.

    server.sendmail(username, to_email, msg.as_string())

    Here, we send the email using the sendmail method. We additionally use the as_string method to convert the MIMEMultipart instance to a string.

    server.quit()

    End the SMTP server session and disconnect from the email server.

    with open("emails.txt","r") as f:
        emails = [line.strip() for line in f.readlines()]

    Here, we open our text file emails.txt which contains our recipient email addresses (one address on each line). We read all lines of the text file and strip whitespace from them. The stripped email addresses are stored in the variable emails.

    for email in emails:
        try:
            send_mail(email,subject,body)
            print(f'Email sent to {email}')
        except Exception as e:
            print(f'Error: {e}')

    Finally, we iterate over each email address stored in emails. For each email address, we attempt to call our previously defined send_mail function. If the email is sent successfully, a message is printed to the console. If any error occurs during the sending of the email, the error message gets printed to the console.

    Using this script, you can send a single email to each email address stored in your text file. As a reminder, always respect user privacy and email best practices when sending any automated emails to prevent your emails from being marked as spam or your email address from being blacklisted.

    Share this post on social!

    Comment on Post

    Your email address will not be published. Required fields are marked *