Quickstart ========== Django makes it really easy to send e-mail messages: .. code-block:: python from django.core.mail import EmailMessage msg = EmailMessage(subject="Hello", body="Hello there!", to=['me@example.com']) msg.send() But this code will soon get really complicated. For example - we should send both plain text version (for old or simple e-mail clients) and html version of an e-mail: .. code-block:: python from django.core.mail import EmailMultiAlternatives subject, from_email, to = 'hello', 'from@example.com', 'to@example.com' text_content = 'This is an important message.' html_content = '
This is an important message.
' msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.attach_alternative(html_content, "text/html") msg.send() This code will soon get even more complicated, because you need to also worry about things like: * More meaningful (dynamic) topic * Which language and timezone should be used when rendering templates? We also should use them when generating e-mail's subject. * Inlining css to make writing html e-mail templates easier * Generating full urls for e-mails (not just paths) * Attachments * Code reuse Basics ------ ClassyMail uses concept similar to django class based views. You should be already familiar with it. Just like with CBVs (Class Based Views) you can set attributes on class that will be used to build an e-mail: .. code-block:: python from django.utils.translation import ugettext_lazy as _ from classymail import ClassyMail class WelcomeMail(ClassyMail): subject = _("Welcome to our website. Thanks for joining us!") builder = WelcomeMail() # prints 'Welcome to our website. Thanks for joining us!' print(builder.subject) Also - just like with CBVs you can easily override those attributes using keyword arguments to class. The only difference is that there is no ``.as_view()`` method: .. code-block:: python builder = WelcomeMail(subject="We can override subject", to=['me@example.com']) print(builder.subject) # prints 'We can override subject' print(builder.to) # prints ['me@example.com'] Both ``subject`` and ``to`` are attributes of ``ClassyMail`` parent class. ``subject`` defaults to empty string and ``to`` defaults to ``None``. You can override them using keyword arguments to your e-mail class like on the example above. But sometimes just setting attribute on a class is not enough. Sometimes we need things to be more dynamic. When you subclass ClassyMail you can override attributes like ``subject`` or methods like ``get_subject``: .. code-block:: python class WelcomeMail(ClassyMail): user = None def get_subject(self): return _("Welcome %s! Thanks for joining us!") % self.user.first_name Html and text content --------------------- Now that we know how to set a proper topic for our e-mail message let's set html and text template names to create our first real, working e-mail: .. code-block:: python class WelcomeMail(ClassyMail): user = None html_template_name = 'emails/welcome.html' text_template_name = 'emails/welcome.txt' def get_subject(self): return _("Welcome %s! Thanks for joining us!") % self.user.first_name # there's also get_context_data() similar to one in CBVs def get_context_data(self): data = super(WelcomeMail, self).get_context_data() data['user'] = self.user return data Now we have to prepare templates. If you have ever sent an html e-mail using django you know that you can put your styles only in ``style=""`` attributes because ``We are very happy that you decided to join us.