Zoho ZeptoMail for Django
Applications built using Django can now use Zoho ZeptoMail to send their transactional emails. This integration supports both text and HTML emails along with attachment support.
Pre-requisites
- Verify the email sending domain
- Generate the Send Mail Token you will use to send emails.
Installation
Add the following installation data in the console.
Copiedpip install django-zoho-zeptomail
Settings configuration
Add the following parameters in the configuration settings - settings.py
Note:
- 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.
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'
Email sending
There are two ways in which you can send emails using the ZeptoMail plugin. They are :
- Plain text only emails
- Plain text and HTML emails
Text only emails
- Use the following code to send plain text emails using the send_mail function.
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']
)
- Use the following code 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()
Text and HTML emails
- Use the following code to send text and 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()
Using attachments
You can use attachments while sending emails. There are three methods to add attachments :
- 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()
Show full
Show less