Skip to content
Luboš Mátl edited this page Aug 4, 2013 · 32 revisions

Django-emails provides send emails in batch, because send big number of mails at one time is not good idea in terms of mail server. It provides mail log and signals that simplify mail notification if new object is created.

Requirements

django 1.2 or higher

Instalation

Easy with pip or easy_install

pip

pip install django-emails

easy_install

easy_install django-emails

Setup

only add emails to INSTALLED_APPS

Settings

Add this constants to settings.py

Your email addres used as mail sender: EMAIL_ADDRESS = 'your.email@domain'

If mail server use TLS or not: EMAIL_USE_TLS = True

Mail server host: EMAIL_HOST = 'smtp.domain'

User (otpional): EMAIL_HOST_USER = 'user'

Password (otpional): EMAIL_HOST_PASSWORD = 'password'

Number of mail that will be send in one batch (otpional, default 10): COUNT_MAILS_IN_BATCH = 10

Number of days that sent emails will be stored in DB (otpional, default 7): COUNT_DAYS_TO_DELETE_MAIL = 7

Site URL: SITE_URL='http://localhost:8000'

Test mail address, only for unit tests (optional): TEST_EMAIL_ADDRESS = 'test.email@domain'

Cron

Add entry to cronab crontab -e

Example (every two minutes): */2 * * * * python [django_app_home]/manage.py sendbatch

Usage

Send text mail, priority has values from 1 to 3

from emails.engine import MailSender
mail_sender = MailSender()
mail_sender.send_mail('Test text mail subject', RECEIVER_EMAIL_ADDRESS, 'Test text mail content', priority=2, type='plain')

Send text with more receivers

from emails.engine import MailSender
mail_sender = MailSender()
mail_sender.send_mails('Test text mail subject', [RECEIVER_EMAIL_ADDRESS1, RECEIVER_EMAIL_ADDRESS2], 'Test text mail content', priority=2, type='plain')

Send html mail

from emails.engine import MailSender
mail_sender = MailSender()
mail_sender.send_mail('Test text mail subject', RECEIVER_EMAIL_ADDRESS, '<strong>Test text mail content</strong>', priority=2, type='html')

Send html mail from template

from emails.engine import MailSender
mail_sender = MailSender()
template = 'emails/test.html'
context = {'text': 'test template mail content'}
mail_sender.send_htmlmail('Test template mail subject',RECEIVER_EMAIL_ADDRESS, template, context, priority = 3)

Send html mail from template with more receivers

from emails.engine import MailSender
mail_sender = MailSender()
template = 'emails/test.html'
context = {'text': 'test template mail content'}
mail_sender.send_htmlmails('Test template mail subject',[RECEIVER_EMAIL_ADDRESS1, RECEIVER_EMAIL_ADDRESS2], template, context, priority = 1)

Administration

Django-emails provide emails administration:

emails Administration

Emails administration

Signals

Django-emails too adds two signals. The signals automatic send mail if new model instance is created.

from django.db import models
from django.utils.translation import ugettext_lazy as _
from emails.signals import send_notification, send_field_notification

class Example(models.Model):
    email = models.EmailField(_('Email'))
    text = models.TextField(_('Text'))
    boolean = models.BooleanField(_('Boolean'))

    def __unicode__(self):
        return self.email

    class Meta:
        verbose_name = _('Example')
        verbose_name_plural = _('Example')
    
send_notification.register(sender = Example, notification_fields = ['email', 'text', 'boolean'])
send_field_notification.register(sender = Example, email_field = 'email', subject = 'Test field notification', template = 'emails/test2.html')

send_notification

Send_notification signal send notification about new object to all users which have Can receive notification permission and email user permissions

send_field_notification

Send email that was formed from template ('emails/test2.html') to email address that is saved in email_field ('email').

Example of this template:

<strong>{{ obj }}</strong>

Variable obj is the new instance of model Example.