Zoho ZeptoMail for Django

Applications built using Django can now integrate with Zoho ZeptoMail to send transactional emails. The integration supports both plain text and HTML emails, along with attachments.

Before proceeding with the API integration, make sure you have completed the domain verification and Send Mail Token generation steps.

Installing ZeptoMail

Django can be installed as library file in your system's terminal. Add the following installation data.

Copiedpip install django-zoho-zeptomail

Configure ZeptoMail

To configure ZeptoMail, add the following parameters in the settings.py file of your Django application. 

CopiedEMAIL_BACKEND = 'zoho_zeptomail.backend.zeptomail_backend.ZohoZeptoMailEmailBackend'
DEFAULT_FROM_EMAIL = 'rebecca@zylker.com' 
ZOHO_ZEPTOMAIL_API_KEY_TOKEN = 'Send Mail Token' 
ZOHO_ZEPTOMAIL_HOSTED_REGION = 'zeptomail.zoho.com' 
  • The default FROM address will be used for all emails sent out from the application.
  • It is mandatory to mention the region where your application is hosted. It is optional for applications hosted in US.

 

Email sending

There are two types of emails you can send using ZeptoMail. They are:

  • Plain text emails
  • Plain text and HTML emails

Plain text emails

  • Use the following code to send plain text emails using the send_mail function.
Copied<div class="help-blockquote bg-blue">
    <div class="zhi-note">
        <ul>
            <li>
                The default FROM address will be used for all emails sent out from the application.
            </li>
            <li>
                It is mandatory to mention the region where your application is hosted. It is optional for applications hosted in US.
            </li>
        </ul>
    </div>
</div>
<p>
    &nbsp;
</p>
<h2>
    <a class="ck-anchor" id="emailsending" name="emailsending"></a>Email sending
</h2>
<p>
    There are two types of emails you can send using ZeptoMail. They are:
</p>
<ul>
    <li>
        Plain text emails
    </li>
    <li>
        Plain text and HTML emails
    </li>
</ul>
<h3>
    <a class="ck-anchor" id="plaintext" name="plaintext"></a>Plain text emails
</h3>
<ul>
    <li>
        Use the following code to send plain text emails using the <strong>send_mail</strong> function.
    </li>
</ul>
  • To add multiple recipients (CC, BCC):
Copiedfrom django.core.mail.message import EmailMessage

email_message = EmailMessage(
    subject='Hello!',
    body='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    to=['sam@zylker.com'],
    bcc=['john@zylker.com'], 
    cc=['george@zylker.com'], 
    reply_to=['samuel@zylker.com']
)
email_message.send()

Plain text and HTML emails

  • Use the following code to send plain text + HTML emails
Copiedfrom django.core.mail import send_mail

send_mail(
    subject='Hello!',
    message='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    recipient_list=['sam@zylker.com'],
    html_message='This is an acknowledgement for your action on our website'
)
  • To add other recipients - CC, BCC using the EmailMultiAlternatives class
Copiedfrom django.core.mail.message import EmailMultiAlternatives

email_message = EmailMultiAlternatives(
    subject='Hello!',
    body='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    to=['sam@zylker.com'],
    bcc=['john@zylker.com'], # Optional
    cc=['george@zylker.com'], # Optional
    reply_to=['samuel@zylker.com'] # Optional
)
email_message.attach_alternative(
    'This is an acknowledgement for your action on our website',
    'text/html'
)
email_message.send()

Adding attachments

There are three methods to add attachments to emails sent using ZeptoMail:

  • Using file path
Copiedfrom django.core.mail.message import EmailMessage

email_message = EmailMessage(
    subject='Hello!',
    body='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    to=['sam@zylker.com']
)
email_message.attach_file('/example/attachment.txt')
email_message.attach_file('/example/attachment.jpg')
email_message.send()
  • Using filename and file content
Copiedfrom django.core.mail.message import EmailMessage

email_message = EmailMessage(
    subject='Hello!',
    body='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    to=['sam@zylker.com']
)

# Text file (Read mode 'r')
with open('/example/attachment.txt', 'r') as file:
    email_message.attach('attachment.txt', file.read())

# Binary file (Read mode 'rb')
with open('/example/attachment.jpg', 'rb') as file:
    email_message.attach('attachment.jpg', file.read())

email_message.send()
  • Using the MIMEBase instance
Copiedfrom email.mime.image import MIMEImage
from email.mime.text import MIMEText
from django.core.mail.message import EmailMessage

email_message = EmailMessage(
    subject='Hello!',
    body='This is an acknowledgement for your action on our website',
    from_email='rebecca@zylker.com',
    to=['sam@zylker.com']
)

# Text file (Read mode 'r')
with open('/example/attachment.txt', 'r') as file:
    mime_text = MIMEText(file.read())
    mime_text.add_header(
        'Content-Disposition', 'attachment; filename=attachment.txt')
    email_message.attach(mime_text)

# Binary file (Read mode 'rb')
with open('/example/attachment.jpg', 'rb') as file:
    mime_image = MIMEImage(file.read())
    mime_image.add_header(
        'Content-Disposition', 'inline; filename=attachment.jpg')
    email_message.attach(mime_image)

email_message.send()