summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek van Gent <loek@1procentclub.nl>2015-03-07 13:55:59 +0100
committerTim Graham <timograham@gmail.com>2015-03-20 12:03:50 -0400
commitd898ba1bec588450d591ebd5076993f96d05eb24 (patch)
tree04d40bad0735180546fbc8f0eb7bec3fd2cd3b2a
parent55f12f8709f0604df7e1817a4c114ead1fb9a311 (diff)
Fixed #24419 -- Added sendtestemail management command
-rw-r--r--AUTHORS1
-rw-r--r--django/core/management/commands/sendtestemail.py20
-rw-r--r--docs/ref/django-admin.txt12
-rw-r--r--docs/releases/1.9.txt3
-rw-r--r--docs/spelling_wordlist1
-rw-r--r--tests/mail/test_sendtestemail.py43
6 files changed, 79 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index b273572353..8dfd26f293 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -408,6 +408,7 @@ answer newbie questions, and generally made Django that much better:
lerouxb@gmail.com
Liang Feng <hutuworm@gmail.com>
limodou
+ Loek van Gent <loek@barakken.nl>
Loïc Bistuer <loic.bistuer@sixmedia.com>
Lowe Thiderman <lowe.thiderman@gmail.com>
Luan Pablo <luanpab@gmail.com>
diff --git a/django/core/management/commands/sendtestemail.py b/django/core/management/commands/sendtestemail.py
new file mode 100644
index 0000000000..c0284bb95f
--- /dev/null
+++ b/django/core/management/commands/sendtestemail.py
@@ -0,0 +1,20 @@
+import datetime
+import socket
+
+from django.core.mail import send_mail
+from django.core.management.base import BaseCommand, CommandError
+
+
+class Command(BaseCommand):
+ help = "Sends a test email to the email addresses specified as arguments."
+ args = "<email1 email2 ...>"
+
+ def handle(self, *args, **kwargs):
+ if not args:
+ raise CommandError('You must provide at least one destination email.')
+ send_mail(
+ subject='Test email from %s on %s' % (socket.gethostname(), datetime.datetime.now()),
+ message="If you\'re reading this, it was successful.",
+ from_email=None,
+ recipient_list=args,
+ )
diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt
index a354bdb045..e9dbe4b746 100644
--- a/docs/ref/django-admin.txt
+++ b/docs/ref/django-admin.txt
@@ -875,6 +875,18 @@ By default, the development server doesn't serve any static files for your site
you want to configure Django to serve static media, read
:doc:`/howto/static-files/index`.
+sendtestemail
+-------------
+
+.. django-admin:: sendtestemail
+
+.. versionadded:: 1.9
+
+Sends a test email (to confirm email sending through Django is working) to the
+recipient(s) specified. For example::
+
+ django-admin sendtestemail foo@example.com bar@example.com
+
shell
-----
diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt
index 20589c8fd6..bce55085ef 100644
--- a/docs/releases/1.9.txt
+++ b/docs/releases/1.9.txt
@@ -146,7 +146,8 @@ Internationalization
Management Commands
^^^^^^^^^^^^^^^^^^^
-* ...
+* The new :djadmin:`sendtestemail` command lets you send a test email to
+ easily confirm that email sending through Django is working.
Models
^^^^^^
diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist
index e9fe8cdfec..78480713fb 100644
--- a/docs/spelling_wordlist
+++ b/docs/spelling_wordlist
@@ -682,6 +682,7 @@ screenshots
sdist
semimajor
semiminor
+sendtestemail
serializability
serializable
serializer
diff --git a/tests/mail/test_sendtestemail.py b/tests/mail/test_sendtestemail.py
new file mode 100644
index 0000000000..7a4203fb75
--- /dev/null
+++ b/tests/mail/test_sendtestemail.py
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.core import mail
+from django.core.management import call_command
+from django.core.management.base import CommandError
+from django.test import SimpleTestCase
+from django.utils.six import StringIO
+
+
+class SendTestEmailManagementCommand(SimpleTestCase):
+ """
+ Test the sending of a test email using the `sendtestemail` command.
+ """
+
+ def test_send_test_email(self):
+ """
+ The mail is sent with the correct subject and recipient.
+ """
+ recipient = "joe@somewhere.org"
+ call_command("sendtestemail", recipient)
+ self.assertEqual(len(mail.outbox), 1)
+ mail_message = mail.outbox[0]
+ self.assertEqual(mail_message.subject[0:15], 'Test email from')
+ self.assertEqual(mail_message.recipients(), [recipient])
+
+ def test_send_test_email_with_multiple_addresses(self):
+ """
+ The mail may be sent with multiple recipients.
+ """
+ recipients = ["joe@somewhere.org", "jane@elsewhere.net"]
+ call_command("sendtestemail", recipients[0], recipients[1])
+ self.assertEqual(len(mail.outbox), 1)
+ mail_message = mail.outbox[0]
+ self.assertEqual(mail_message.subject[0:15], 'Test email from')
+ self.assertEqual(mail_message.recipients(), recipients)
+
+ def test_send_test_email_missing_recipient(self):
+ """
+ A CommandError is raised if no recipients are specified.
+ """
+ with self.assertRaisesMessage(CommandError, 'You must provide at least one destination email'):
+ call_command("sendtestemail")