While developing a module that requires to send emails, using actual email servers would be an overhead. They might introduce network latency, possible spam filtering etc. All end up retarding productivity.
We have smtpd.DebuggingServer
that could be used as a local email server. All emails that are sent to this server outputs to the stdout. To start the server.
$ python -m smtpd -n -c DebuggingServer localhost:1025
and the corresponding setting to be used in settings.py is
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
The code snippet that sends the mail is below.
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
subject, from_email = 'Account created', 'admin@mywebsite.com'
html_content = render_to_string('email.html')
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives(subject, text_content, from_email, [user@somewebsite.com])
msg.attach_alternative(html_content, 'text/html')
msg.send()
Upon calling send message, the mail would be delivered to the DebuggingServer and displayed in stdout as below.
---------- MESSAGE FOLLOWS ----------
Content-Type: multipart/alternative; boundary="===============0236101087=="
MIME-Version: 1.0
Subject: Account created
From: admin@mywebsite.com
To: user@somewebsite.com
Date: Thu, 09 May 2013 05:53:21 -0000
X-Peer: 127.0.0.1
--===============0236101087==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Mail sent from mywebsite.com
Account created.
--===============0236101087==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
<p>Mail sent from mywebsite.com</p>
<div style="background: #ddd;border: 1px solid #d3d3d3;-moz-border-radius: 3px;border-radius: 3px;width: 50%;line-height: 100px;text-align: center;">
Account created.
</div>
--===============0236101087==--
------------ END MESSAGE ------------
Hope this helps.