Skip to main content

Django MailerSend makes it easier to send emails in Django apps using the MailerSend API.

Project description

Django MailerSend

Django MailerSend makes it easier to send emails in Django apps using the MailerSend API. Uses the official MailerSend Python SDK internally.

Index

  • Installation
  • Configuration
  • Usage
    • Text Only Emails
    • Text + HTML Emails
    • Attachments

Installation

pip install django-mailersend

Configuration

Add the following to your Django settings:

EMAIL_BACKEND = 'django_mailersend.backend.MailerSendEmailBackend'
MAILERSEND_API_KEY = 'Your API key'

Usage

Text Only Emails

  • Send a Text Only email using Django's send_mail function:
from django.core.mail import send_mail

send_mail(
  subject='Hello world!',
  message='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  recipient_list=['receiver@example.com']
)
  • Send a Text Only email using Django's EmailMessage class for more options:
from django.core.mail.message import EmailMessage

email_message = EmailMessage(
  subject='Hello world!',
  body='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  to=['receiver@example.com'],
  bcc=['bcc@example.com'], # Optional
  cc=['cc@example.com'], # Optional
  reply_to=['reply-to@example.com'] # Optional
)
email_message.send()

Text + HTML Emails

  • Send a Text + HTML email using Django's send_mail function:
from django.core.mail import send_mail

send_mail(
  subject='Hello world!',
  message='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  recipient_list=['receiver@example.com'],
  html_message='<p>Sent using <strong>Django MailerSend Email Backend</strong>!</p>'
)
  • Send a Text + HTML email using Django's EmailMultiAlternatives class for more options:
from django.core.mail.message import EmailMultiAlternatives

email_message = EmailMultiAlternatives(
  subject='Hello world!',
  body='Sent using Django MailerSend Email Backend!',
  from_email='sender@example.com',
  to=['receiver@example.com'],
  bcc=['bcc@example.com'], # Optional
  cc=['cc@example.com'], # Optional
  reply_to=['reply-to@example.com'] # Optional
)
email_message.attach_alternative(
    '<p>Sent using <strong>Django MailerSend Email Backend</strong>!</p>',
    'text/html'
)
email_message.send()

Attachments

Sending attachments is supported when using Django's EmailMessage or EmailMultiAlternatives classes. There are several ways of doing this:

  • Send an attachment by providing the file path to the attach_file method:
from django.core.mail.message import EmailMessage

email_message = EmailMessage(
  subject='Hello world!',
  body='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  to=['receiver@example.com']
)
email_message.attach_file('/example/attachment.txt')
email_message.attach_file('/example/attachment.jpg')
email_message.send()
  • Send an attachment by providing a filename and the file content to the attach method:
from django.core.mail.message import EmailMessage

email_message = EmailMessage(
  subject='Hello world!',
  body='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  to=['receiver@example.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()
  • Send an attachment by providing a MIMEBase instance to the attach method:
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from django.core.mail.message import EmailMessage

email_message = EmailMessage(
  subject='Hello world!',
  body='Sent using the Django MailerSend Email Backend!',
  from_email='sender@example.com',
  to=['receiver@example.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()

Resources

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_mailersend-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_mailersend-0.2.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file django_mailersend-0.2.1.tar.gz.

File metadata

  • Download URL: django_mailersend-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for django_mailersend-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e3c258a219aa48b651f3a6688a0c9675a80f808fea9bf62edf866d226c4efed7
MD5 92f1b50057557d3f65109987bc9f3455
BLAKE2b-256 ef41c0f6437acf426620128e4c6e84e1238950d9a8c1024a2791783617df76aa

See more details on using hashes here.

File details

Details for the file django_mailersend-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mailersend-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84bb191257f9e665d84424ef1b73db1c6051d4fbd817d6f24b9573e070eda91e
MD5 e76d3a136e5bf850726e0449825bfae6
BLAKE2b-256 2b1a573280a7d7a0197ddb22d18c19f90712d61e71c3462063572580775117a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page