Django-Db-Mailer
Documentation available at Read the Docs.
What's that
Installation
- Using pip:
$ pip install django-db-mailer
- Add the
dbmail
application toINSTALLED_APPS
in your settings file (usuallysettings.py
) - Sync database (
./manage.py migrate
).
Mail API
from dbmail.models import MailTemplate
from dbmail import send_db_mail
# New dbmail template
MailTemplate.objects.create(
name="Site welcome template",
subject="[{{prefix}}] Welcome {{full_name}}!",
message="Hi, {{username}}. Welcome to our site.",
slug="welcome",
is_html=False,
)
# Send message with created template
send_db_mail(
# slug which defined on db template
slug='welcome',
# recipient can be list, or str separated with comma or simple string
# '[email protected]' or '[email protected], [email protected]' or
# ['[email protected]', '[email protected]'] or string Mail group slug
recipient='[email protected]',
# All *args params will be accessible on template context
{
'username': request.user.username,
'full_name': request.user.get_full_name(),
'signup_date': request.user.date_joined,
'prefix': "DbMail",
},
# You can access to all model fields. For m2m and fk fields, you should use module_name
MyModel.objects.get(pk=1),
# Optional kwargs:
# backend='dbmail.backends.mail',
# provider='apps.utils.some.mail.provider',
# from_email='[email protected]'
# cc=['[email protected]'],
# bcc=['[email protected]'],
# user=User.objects.get(pk=1),
#
# language='ru',
#
# attachments=[(filename, content, mimetype)],
# files=['hello.jpg', 'world.png'],
# headers={'Custom-Header':'Some value'},
#
# queue='default',
# retry_delay=300,
# max_retries=3,
# retry=True,
# time_limit=30,
# send_after=60,
#
# use_celery=True,
)
Sms API
from dbmail import send_db_sms
send_db_sms(
# slug which defined on db template
slug='welcome',
# recipient can be list, or str separated with comma or simple string
# '+79031234567' or +79031234567, +79031234568, +79031234569' or
# ['+79031234567', '+79031234568'] or string Mail group slug
recipient='+79031234567',
# All *args params will be accessible on template context
{
'username': request.user.username,
'full_name': request.user.get_full_name(),
'signup_date': request.user.date_joined
},
# You can access to all model fields. For m2m and fk fields, you should use module_name
MyModel.objects.get(pk=1),
# Optional kwargs:
# backend='dbmail.backends.sms',
# provider='dbmail.providers.nexmo.sms',
# from_email='DBMail'
# user=User.objects.get(pk=1),
#
# language='ru',
#
# queue='default',
# retry_delay=300,
# max_retries=3,
# retry=True,
# time_limit=30,
# send_after=60,
#
# use_celery=True,
)
Text to speech API
from dbmail import send_db_tts
send_db_tts(
# slug which defined on db template
slug='welcome',
# recipient can be list, or str separated with comma or simple string
# '+79031234567' or +79031234567, +79031234568, +79031234569' or
# ['+79031234567', '+79031234568'] or string Mail group slug
recipient='+79031234567',
# All *args params will be accessible on template context
{
'username': request.user.username,
'full_name': request.user.get_full_name(),
'signup_date': request.user.date_joined
},
# You can access to all model fields. For m2m and fk fields, you should use module_name
MyModel.objects.get(pk=1),
# Optional kwargs:
# backend='dbmail.backends.tts',
# provider='dbmail.providers.nexmo.tts',
# from_email='DBMail'
# user=User.objects.get(pk=1),
#
# language='ru',
#
# queue='default',
# retry_delay=300,
# max_retries=3,
# retry=True,
# time_limit=30,
# send_after=60,
#
# use_celery=True,
)
Text to speech supported by default provider. But maybe not supported by your provider.
Push notification API
from dbmail import send_db_push
send_db_push(
# slug which defined on db template
slug='welcome',
# recipient can be list, or str separated with comma or simple string
# '34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8a' or '34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8a, 34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8b' or
# ['34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8a', '34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8b'] or string Mail group slug
recipient='34cc3e5f0d2abf2ca0f9af170bd8cd2372a22f8c',
# All *args params will be accessible on template context
{
'username': request.user.username,
'full_name': request.user.get_full_name(),
'signup_date': request.user.date_joined
},
# You can access to all model fields. For m2m and fk fields, you should use module_name
MyModel.objects.get(pk=1),
# Optional kwargs:
# backend='dbmail.backends.push',
# provider='dbmail.providers.prowl.push',
# event='Server is down!',
# from_email='ConsoleApp'
# user=User.objects.get(pk=1),
#
# language='ru',
#
# queue='default',
# retry_delay=300,
# max_retries=3,
# retry=True,
# time_limit=30,
# send_after=60,
#
# use_celery=True,
)
DBMail Backends
By default django-dbmail
used 4 built-in backends (Mail/Sms/Tts/Push). But nothing prevents to write your own backend to work with all that you want.
DBMail Providers
Battery have some built-in providers for most popular services, which will be used without any dependencies with built-in backends.
Push notifications for mobile apps:
- Apple APNs/APNs2
- Google GCM
- Microsoft Tile/Toast/Raw
- BoxCar
- Parse
Browser notifications:
- GCM (Desktop: Google Chrome, FireFox; Mobile: Google Chrome on Android)
- APNs (Desktop: Safari)
- Centrifugo
- PubNub
- BoxCar
- PushAll
Notifications for team:
- Slack/Mattermost
- Boxcar
- Prowl
- Pushover
- PushAll
SMS notifications:
- Nexmo
- Twilio
- IQsms
- SmsAero
- SmsBliss
Mail notifications:
- SendinBlue
- Any, which designed as django email backend
You can find providers settings on docs.
Demo installation
Docker
$ git clone --depth 1 -b master https://github.com/LPgenerator/django-db-mailer.git db-mailer
$ cd db-mailer
$ docker build -t dbmail .
$ docker run -it -d -p 8000:8000 --name dbmail dbmail
$ docker exec -i -t dbmail /bin/bash
$ cd /mailer/
Vagrant
$ git clone --depth 1 -b master https://github.com/LPgenerator/django-db-mailer.git db-mailer
$ cd db-mailer
$ vagrant up --provider virtualbox
$ vagrant ssh
$ cd /mailer/
OS X/Linux
$ sudo apt-get install -y virtualenvwrapper redis-server git python-dev libxml2-dev libxslt-dev zlib1g-dev || brew install pyenv-virtualenvwrapper redis git
$ source /usr/share/virtualenvwrapper/virtualenvwrapper.sh || source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv db-mailer
$ workon db-mailer
$ git clone --depth 1 https://github.com/LPgenerator/django-db-mailer.git db-mailer
$ cd db-mailer
$ python setup.py develop
$ cd demo
$ pip install -r requirements.txt
$ python manage.py migrate --noinput
$ python manage.py createsuperuser --username admin --email [email protected]
$ redis-server >& /dev/null &
$ python manage.py runserver >& /dev/null &
$ python manage.py celeryd -Q default >& /dev/null &
Open Shell:
$ python manage.py shell_plus --print-sql
Create new template:
from dbmail.models import MailTemplate
from dbmail import send_db_mail
MailTemplate.objects.create(
name="Site welcome template",
subject="Welcome",
message="Welcome to our site. We are glad to see you.",
slug="welcome",
is_html=False,
)
Try to send test email with created template (without celery):
send_db_mail('welcome', '[email protected]', use_celery=False)
Send email using celery:
send_db_mail('welcome', '[email protected]')
Check mail logs:
from pprint import pprint
from django.forms.models import model_to_dict
from dbmail.models import MailLog
pprint([model_to_dict(obj) for obj in MailLog.objects.all()])
Open app in browser (login and password is admin/admin):
$ xdg-open http://127.0.0.1:8000/admin/dbmail/ >& /dev/null || open http://127.0.0.1:8000/admin/dbmail/ >& /dev/null
Additional information
Revision
For support template reversion, you can install django-reversion
. Find information about compatibility with your Django versions here.
Editor
To enable editor, you may install and configure django-tinymce
or django-ckeditor
app.
Theme
django-db-mailer
supported from box django-grappelli
and django-suit
skin. Information about compatibility available here.
Queue
Install and configure django-celery
for background message sending with priorities. You can find celery settings examples on demo project. We recommended to use django-celery-mon
with django-celery
for monitoring celery and supervisor processes.
Premailer
For turns CSS blocks into style attributes, you can install premailer
from PyPi.
Translation
For use different language on your mail templates, install django-modeltranslation
or grappelli-modeltranslation
. Add into settings.py:
MODELTRANSLATION_DEFAULT_LANGUAGE = 'en'
MODELTRANSLATION_LANGUAGES = ('ru', 'en')
MODELTRANSLATION_TRANSLATION_FILES = (
'dbmail.translation',
)
INSTALLED_APPS = ('modeltranslation',) + INSTALLED_APPS
# INSTALLED_APPS = ('grappelli', 'grappelli_modeltranslation', 'modeltranslation',) + INSTALLED_APPS
Update dbmail fields:
$ ./manage.py sync_translation_fields --noinput
Postmark Django Backend
Install python-postmark
app via pip. Configure your settings:
POSTMARK_API_KEY = ''
POSTMARK_SENDER = '[email protected]'
POSTMARK_TEST_MODE = False
EMAIL_BACKEND = 'postmark.django_backend.EmailBackend'
Amazon's Simple Email Service Django Backend
Install django-ses
app via pip. Configure your settings:
EMAIL_BACKEND = 'django_ses.SESBackend'
# These are optional -- if they're set as environment variables they won't
# need to be set here as well
AWS_ACCESS_KEY_ID = 'YOUR-ACCESS-KEY-ID'
AWS_SECRET_ACCESS_KEY = 'YOUR-SECRET-ACCESS-KEY'
# Additionally, you can specify an optional region, like so:
AWS_SES_REGION_NAME = 'us-east-1'
AWS_SES_REGION_ENDPOINT = 'email.us-east-1.amazonaws.com'
Note: You can use any backends designed as django email backend
Tracking
$ pip install httpagentparser django-ipware geoip2
For track information about user, or about mail is read, you must be enable logging, and enable tracking on settings.
If you use Django 1.8, you should install geoip package instead of geoip2.
MJML
MJML is a markup language designed to reduce the pain of coding a responsive email. Install django-mjml
app via pip and mjml
via npm. And configure your settings:
INSTALLED_APPS = (
...,
'mjml',
)
Older versions
Very simple version of this app, available here. That version do not include celery settings, bcc, api, mail settings, signals, mail groups and model browser.
Notes
All app features available only with django-celery
and with Redis
.
$ pip install redis hiredis django-celery
External API usage
from dbmail.models import ApiKey
ApiKey.objects.create(name='Test', api_key='ZzriUzE')
$ pip install httpie
$ http -f POST http://127.0.0.1:8000/dbmail/api/ api_key=ZzriUzE slug=welcome [email protected] data='{"name": "Ivan", "age": 20}'
or
$ apt-get install curl || brew install curl
$ curl -X POST http://127.0.0.1:8000/dbmail/api/ --data 'api_key=ZzriUzE&slug=welcome&[email protected]&backend=mail'
API bandwidth is 1k+ rps on i7 2.3GHz
Responsive transactional HTML email templates
Fixtures with Base transactional HTML email templates was added into dbmail fixtures. This templates was optimized for desktop clients, web clients, mobile clients, various devices, various providers. Thanks for Mailgun Team. You can use it as default basic templates on your project.
python manage.py load_dbmail_base_templates
Publications
- Установка и использование с примерами на русском.
- Completely installation and usage with examples. Translated by Google.
Screenshots
Compatibility
- Python: 2.7, pypy2.7, 3.4, 3.5, 3.6, 3.7, pypy3.5
- Django: 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 1.10, 1.11, 2.0, 2.1