Last active
August 29, 2015 13:56
-
-
Save wjurkowlaniec/8831874 to your computer and use it in GitHub Desktop.
Django mailing made easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django.core.mail import send_mail, mail_managers, EmailMultiAlternatives | |
from django.template.loader import render_to_string | |
from django.conf import settings | |
class Mailing: | |
path_prefix = 'mail/' | |
path_suffix = '.html' | |
prefix_title = u"TITLE BEGINNING - " | |
mail_name_1 = 'mail_template_name_1', u"Title of mail 1" | |
mail_name_2 = 'mail_template_name_2', u"Title of mail 2" | |
adm_mail_name_2 = 'adm_mail_template_name_2', u"Title of admin mail 2" | |
@staticmethod | |
def send(msg_type, recipents=None, **kwargs): | |
if not recipents and str(msg_type[0]).startswith('adm'): | |
recipents = [x[1] for x in settings.MANAGERS] | |
if not recipents: | |
raise Exception("No recipents list") | |
mail_title = Mailing.prefix_title + msg_type[1] | |
template_path = Mailing.path_prefix + msg_type[0] + Mailing.path_suffix | |
mail_content = render_to_string(template_path, kwargs) | |
msg = EmailMultiAlternatives(mail_title, mail_content, settings.DEFAULT_FROM_EMAIL, recipents) | |
msg.content_subtype = "html" | |
msg.send() | |
------------ | |
How to use it? | |
just | |
from app.mailing import Mailing | |
Mailing.send(Mailing.mail_name_1, [email_addr], var1=obj_1, var2=obj_2) | |
Mailing.send(Mailing.adm_mail_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment