Email sending using Zoho ZeptoMail in Django
Django ZeptoMail
ZeptoMail for Django facilitates email sending from Django applications using ZeptoMail APIs.
Installation
pip install django-zoho-zeptomail
Settings configuration
Add the following parameters in the application settings.
EMAIL_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
Text-only emails
- To send a plain text message using Django's send_mail function
from 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']
)
- To add other recipients (CC, BCC) using EmailMessage class
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'],
bcc=['john@zylker.com'],
cc=['george@zylker.com'],
reply_to=['samuel@zylker.com']
)
email_message.send()
Text + HTML emails
- To send text and HTML emails,
from 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 EmailMultiAlternatives class
from 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'],
cc=['george@zylker.com'],
reply_to=['samuel@zylker.com']
)
email_message.attach_alternative(
'This is an acknowledgement for your action on our website',
'text/html'
)
email_message.send()
Using attachments
You can add attachments while using EmailMessage as well as EmailMultiAlternatives classes. This can be achieved in three ways using different methods:
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']
)
email_message.attach_file('/example/attachment.txt')
email_message.attach_file('/example/attachment.jpg')
email_message.send()
- Using a filename and file content
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']
)
with open('/example/attachment.txt', 'r') as file:
email_message.attach('attachment.txt', file.read())
with open('/example/attachment.jpg', 'rb') as file:
email_message.attach('attachment.jpg', file.read())
email_message.send()
from 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']
)
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)
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()
Resources