2
0

email.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. .. _topics-email:
  2. ==============
  3. Sending e-mail
  4. ==============
  5. .. module:: django.core.mail
  6. :synopsis: Helpers to easily send e-mail.
  7. Although Python makes sending e-mail relatively easy via the `smtplib
  8. library`_, Django provides a couple of light wrappers over it. These wrappers
  9. are provided to make sending e-mail extra quick, to make it easy to test
  10. email sending during development, and to provide support for platforms that
  11. can't use SMTP.
  12. The code lives in the ``django.core.mail`` module.
  13. .. _smtplib library: http://docs.python.org/library/smtplib.html
  14. Quick example
  15. =============
  16. In two lines::
  17. from django.core.mail import send_mail
  18. send_mail('Subject here', 'Here is the message.', 'from@example.com',
  19. ['to@example.com'], fail_silently=False)
  20. Mail is sent using the SMTP host and port specified in the
  21. :setting:`EMAIL_HOST` and :setting:`EMAIL_PORT` settings. The
  22. :setting:`EMAIL_HOST_USER` and :setting:`EMAIL_HOST_PASSWORD` settings, if
  23. set, are used to authenticate to the SMTP server, and the
  24. :setting:`EMAIL_USE_TLS` setting controls whether a secure connection is used.
  25. .. note::
  26. The character set of e-mail sent with ``django.core.mail`` will be set to
  27. the value of your :setting:`DEFAULT_CHARSET` setting.
  28. send_mail()
  29. ===========
  30. The simplest way to send e-mail is using the function
  31. ``django.core.mail.send_mail()``. Here's its definition:
  32. .. function:: send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None)
  33. The ``subject``, ``message``, ``from_email`` and ``recipient_list`` parameters
  34. are required.
  35. * ``subject``: A string.
  36. * ``message``: A string.
  37. * ``from_email``: A string.
  38. * ``recipient_list``: A list of strings, each an e-mail address. Each
  39. member of ``recipient_list`` will see the other recipients in the "To:"
  40. field of the e-mail message.
  41. * ``fail_silently``: A boolean. If it's ``False``, ``send_mail`` will raise
  42. an ``smtplib.SMTPException``. See the `smtplib docs`_ for a list of
  43. possible exceptions, all of which are subclasses of ``SMTPException``.
  44. * ``auth_user``: The optional username to use to authenticate to the SMTP
  45. server. If this isn't provided, Django will use the value of the
  46. ``EMAIL_HOST_USER`` setting.
  47. * ``auth_password``: The optional password to use to authenticate to the
  48. SMTP server. If this isn't provided, Django will use the value of the
  49. ``EMAIL_HOST_PASSWORD`` setting.
  50. * ``connection``: The optional email backend to use to send the mail.
  51. If unspecified, an instance of the default backend will be used.
  52. See the documentation on :ref:`E-mail backends <topic-email-backends>`
  53. for more details.
  54. .. _smtplib docs: http://docs.python.org/library/smtplib.html
  55. send_mass_mail()
  56. ================
  57. ``django.core.mail.send_mass_mail()`` is intended to handle mass e-mailing.
  58. Here's the definition:
  59. .. function:: send_mass_mail(datatuple, fail_silently=False, auth_user=None, auth_password=None, connection=None)
  60. ``datatuple`` is a tuple in which each element is in this format::
  61. (subject, message, from_email, recipient_list)
  62. ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
  63. as in :meth:`~django.core.mail.send_mail()`.
  64. Each separate element of ``datatuple`` results in a separate e-mail message.
  65. As in :meth:`~django.core.mail.send_mail()`, recipients in the same
  66. ``recipient_list`` will all see the other addresses in the e-mail messages'
  67. "To:" field.
  68. send_mass_mail() vs. send_mail()
  69. --------------------------------
  70. The main difference between :meth:`~django.core.mail.send_mass_mail()` and
  71. :meth:`~django.core.mail.send_mail()` is that
  72. :meth:`~django.core.mail.send_mail()` opens a connection to the mail server
  73. each time it's executed, while :meth:`~django.core.mail.send_mass_mail()` uses
  74. a single connection for all of its messages. This makes
  75. :meth:`~django.core.mail.send_mass_mail()` slightly more efficient.
  76. mail_admins()
  77. =============
  78. ``django.core.mail.mail_admins()`` is a shortcut for sending an e-mail to the
  79. site admins, as defined in the :setting:`ADMINS` setting. Here's the definition:
  80. .. function:: mail_admins(subject, message, fail_silently=False, connection=None)
  81. ``mail_admins()`` prefixes the subject with the value of the
  82. :setting:`EMAIL_SUBJECT_PREFIX` setting, which is ``"[Django] "`` by default.
  83. The "From:" header of the e-mail will be the value of the
  84. :setting:`SERVER_EMAIL` setting.
  85. This method exists for convenience and readability.
  86. mail_managers() function
  87. ========================
  88. ``django.core.mail.mail_managers()`` is just like ``mail_admins()``, except it
  89. sends an e-mail to the site managers, as defined in the :setting:`MANAGERS`
  90. setting. Here's the definition:
  91. .. function:: mail_managers(subject, message, fail_silently=False, connection=None)
  92. Examples
  93. ========
  94. This sends a single e-mail to john@example.com and jane@example.com, with them
  95. both appearing in the "To:"::
  96. send_mail('Subject', 'Message.', 'from@example.com',
  97. ['john@example.com', 'jane@example.com'])
  98. This sends a message to john@example.com and jane@example.com, with them both
  99. receiving a separate e-mail::
  100. datatuple = (
  101. ('Subject', 'Message.', 'from@example.com', ['john@example.com']),
  102. ('Subject', 'Message.', 'from@example.com', ['jane@example.com']),
  103. )
  104. send_mass_mail(datatuple)
  105. Preventing header injection
  106. ===========================
  107. `Header injection`_ is a security exploit in which an attacker inserts extra
  108. e-mail headers to control the "To:" and "From:" in e-mail messages that your
  109. scripts generate.
  110. The Django e-mail functions outlined above all protect against header injection
  111. by forbidding newlines in header values. If any ``subject``, ``from_email`` or
  112. ``recipient_list`` contains a newline (in either Unix, Windows or Mac style),
  113. the e-mail function (e.g. :meth:`~django.core.mail.send_mail()`) will raise
  114. ``django.core.mail.BadHeaderError`` (a subclass of ``ValueError``) and, hence,
  115. will not send the e-mail. It's your responsibility to validate all data before
  116. passing it to the e-mail functions.
  117. If a ``message`` contains headers at the start of the string, the headers will
  118. simply be printed as the first bit of the e-mail message.
  119. Here's an example view that takes a ``subject``, ``message`` and ``from_email``
  120. from the request's POST data, sends that to admin@example.com and redirects to
  121. "/contact/thanks/" when it's done::
  122. from django.core.mail import send_mail, BadHeaderError
  123. def send_email(request):
  124. subject = request.POST.get('subject', '')
  125. message = request.POST.get('message', '')
  126. from_email = request.POST.get('from_email', '')
  127. if subject and message and from_email:
  128. try:
  129. send_mail(subject, message, from_email, ['admin@example.com'])
  130. except BadHeaderError:
  131. return HttpResponse('Invalid header found.')
  132. return HttpResponseRedirect('/contact/thanks/')
  133. else:
  134. # In reality we'd use a form class
  135. # to get proper validation errors.
  136. return HttpResponse('Make sure all fields are entered and valid.')
  137. .. _Header injection: http://www.nyphp.org/phundamentals/email_header_injection.php
  138. .. _emailmessage-and-smtpconnection:
  139. The EmailMessage class
  140. ======================
  141. .. versionadded:: 1.0
  142. Django's :meth:`~django.core.mail.send_mail()` and
  143. :meth:`~django.core.mail.send_mass_mail()` functions are actually thin
  144. wrappers that make use of the :class:`~django.core.mail.EmailMessage` class.
  145. Not all features of the :class:`~django.core.mail.EmailMessage` class are
  146. available through the :meth:`~django.core.mail.send_mail()` and related
  147. wrapper functions. If you wish to use advanced features, such as BCC'ed
  148. recipients, file attachments, or multi-part e-mail, you'll need to create
  149. :class:`~django.core.mail.EmailMessage` instances directly.
  150. .. note::
  151. This is a design feature. :meth:`~django.core.mail.send_mail()` and
  152. related functions were originally the only interface Django provided.
  153. However, the list of parameters they accepted was slowly growing over
  154. time. It made sense to move to a more object-oriented design for e-mail
  155. messages and retain the original functions only for backwards
  156. compatibility.
  157. :class:`~django.core.mail.EmailMessage` is responsible for creating the e-mail
  158. message itself. The :ref:`e-mail backend <topic-email-backends>` is then
  159. responsible for sending the e-mail.
  160. For convenience, :class:`~django.core.mail.EmailMessage` provides a simple
  161. ``send()`` method for sending a single email. If you need to send multiple
  162. messages, the email backend API :ref:`provides an alternative
  163. <topics-sending-multiple-emails>`.
  164. EmailMessage Objects
  165. --------------------
  166. .. class:: EmailMessage
  167. The :class:`~django.core.mail.EmailMessage` class is initialized with the
  168. following parameters (in the given order, if positional arguments are used).
  169. All parameters are optional and can be set at any time prior to calling the
  170. ``send()`` method.
  171. * ``subject``: The subject line of the e-mail.
  172. * ``body``: The body text. This should be a plain text message.
  173. * ``from_email``: The sender's address. Both ``fred@example.com`` and
  174. ``Fred <fred@example.com>`` forms are legal. If omitted, the
  175. :setting:`DEFAULT_FROM_EMAIL` setting is used.
  176. * ``to``: A list or tuple of recipient addresses.
  177. * ``bcc``: A list or tuple of addresses used in the "Bcc" header when
  178. sending the e-mail.
  179. * ``connection``: An e-mail backend instance. Use this parameter if
  180. you want to use the same connection for multiple messages. If omitted, a
  181. new connection is created when ``send()`` is called.
  182. * ``attachments``: A list of attachments to put on the message. These can
  183. be either ``email.MIMEBase.MIMEBase`` instances, or ``(filename,
  184. content, mimetype)`` triples.
  185. * ``headers``: A dictionary of extra headers to put on the message. The
  186. keys are the header name, values are the header values. It's up to the
  187. caller to ensure header names and values are in the correct format for
  188. an e-mail message.
  189. For example::
  190. email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
  191. ['to1@example.com', 'to2@example.com'], ['bcc@example.com'],
  192. headers = {'Reply-To': 'another@example.com'})
  193. The class has the following methods:
  194. * ``send(fail_silently=False)`` sends the message. If a connection was
  195. specified when the email was constructed, that connection will be used.
  196. Otherwise, an instance of the default backend will be instantiated and
  197. used. If the keyword argument ``fail_silently`` is ``True``, exceptions
  198. raised while sending the message will be quashed.
  199. * ``message()`` constructs a ``django.core.mail.SafeMIMEText`` object (a
  200. subclass of Python's ``email.MIMEText.MIMEText`` class) or a
  201. ``django.core.mail.SafeMIMEMultipart`` object holding the message to be
  202. sent. If you ever need to extend the
  203. :class:`~django.core.mail.EmailMessage` class, you'll probably want to
  204. override this method to put the content you want into the MIME object.
  205. * ``recipients()`` returns a list of all the recipients of the message,
  206. whether they're recorded in the ``to`` or ``bcc`` attributes. This is
  207. another method you might need to override when subclassing, because the
  208. SMTP server needs to be told the full list of recipients when the message
  209. is sent. If you add another way to specify recipients in your class, they
  210. need to be returned from this method as well.
  211. * ``attach()`` creates a new file attachment and adds it to the message.
  212. There are two ways to call ``attach()``:
  213. * You can pass it a single argument that is an
  214. ``email.MIMEBase.MIMEBase`` instance. This will be inserted directly
  215. into the resulting message.
  216. * Alternatively, you can pass ``attach()`` three arguments:
  217. ``filename``, ``content`` and ``mimetype``. ``filename`` is the name
  218. of the file attachment as it will appear in the e-mail, ``content`` is
  219. the data that will be contained inside the attachment and
  220. ``mimetype`` is the optional MIME type for the attachment. If you
  221. omit ``mimetype``, the MIME content type will be guessed from the
  222. filename of the attachment.
  223. For example::
  224. message.attach('design.png', img_data, 'image/png')
  225. * ``attach_file()`` creates a new attachment using a file from your
  226. filesystem. Call it with the path of the file to attach and, optionally,
  227. the MIME type to use for the attachment. If the MIME type is omitted, it
  228. will be guessed from the filename. The simplest use would be::
  229. message.attach_file('/images/weather_map.png')
  230. .. _DEFAULT_FROM_EMAIL: ../settings/#default-from-email
  231. Sending alternative content types
  232. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  233. It can be useful to include multiple versions of the content in an e-mail; the
  234. classic example is to send both text and HTML versions of a message. With
  235. Django's e-mail library, you can do this using the ``EmailMultiAlternatives``
  236. class. This subclass of :class:`~django.core.mail.EmailMessage` has an
  237. ``attach_alternative()`` method for including extra versions of the message
  238. body in the e-mail. All the other methods (including the class initialization)
  239. are inherited directly from :class:`~django.core.mail.EmailMessage`.
  240. To send a text and HTML combination, you could write::
  241. from django.core.mail import EmailMultiAlternatives
  242. subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
  243. text_content = 'This is an important message.'
  244. html_content = '<p>This is an <strong>important</strong> message.</p>'
  245. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  246. msg.attach_alternative(html_content, "text/html")
  247. msg.send()
  248. By default, the MIME type of the ``body`` parameter in an
  249. :class:`~django.core.mail.EmailMessage` is ``"text/plain"``. It is good
  250. practice to leave this alone, because it guarantees that any recipient will be
  251. able to read the e-mail, regardless of their mail client. However, if you are
  252. confident that your recipients can handle an alternative content type, you can
  253. use the ``content_subtype`` attribute on the
  254. :class:`~django.core.mail.EmailMessage` class to change the main content type.
  255. The major type will always be ``"text"``, but you can change it to the
  256. subtype. For example::
  257. msg = EmailMessage(subject, html_content, from_email, [to])
  258. msg.content_subtype = "html" # Main content is now text/html
  259. msg.send()
  260. .. _topic-email-backends:
  261. E-Mail Backends
  262. ===============
  263. .. versionadded:: 1.2
  264. The actual sending of an e-mail is handled by the e-mail backend.
  265. The e-mail backend class has the following methods:
  266. * ``open()`` instantiates an long-lived email-sending connection.
  267. * ``close()`` closes the current email-sending connection.
  268. * ``send_messages(email_messages)`` sends a list of
  269. :class:`~django.core.mail.EmailMessage` objects. If the connection is
  270. not open, this call will implicitly open the connection, and close the
  271. connection afterwards. If the connection is already open, it will be
  272. left open after mail has been sent.
  273. Obtaining an instance of an e-mail backend
  274. ------------------------------------------
  275. The :meth:`get_connection` function in ``django.core.mail`` returns an
  276. instance of the e-mail backend that you can use.
  277. .. currentmodule:: django.core.mail
  278. .. function:: get_connection(backend=None, fail_silently=False, *args, **kwargs)
  279. By default, a call to ``get_connection()`` will return an instance of the
  280. email backend specified in :setting:`EMAIL_BACKEND`. If you specify the
  281. ``backend`` argument, an instance of that backend will be instantiated.
  282. The ``fail_silently`` argument controls how the backend should handle errors.
  283. If ``fail_silently`` is True, exceptions during the email sending process
  284. will be silently ignored.
  285. All other arguments are passed directly to the constructor of the
  286. e-mail backend.
  287. Django ships with several e-mail sending backends. With the exception of the
  288. SMTP backend (which is the default), these backends are only useful during
  289. testing and development. If you have special email sending requirements, you
  290. can :ref:`write your own email backend <topic-custom-email-backend>`.
  291. .. _topic-email-smtp-backend:
  292. SMTP backend
  293. ~~~~~~~~~~~~
  294. This is the default backend. E-mail will be sent through a SMTP server.
  295. The server address and authentication credentials are set in the
  296. :setting:`EMAIL_HOST`, :setting:`EMAIL_POST`, :setting:`EMAIL_HOST_USER`,
  297. :setting:`EMAIL_HOST_PASSWORD` and :setting:`EMAIL_USE_TLS` settings in your
  298. settings file.
  299. The SMTP backend is the default configuration inherited by Django. If you
  300. want to specify it explicitly, put the following in your settings::
  301. EMAIL_BACKEND = 'django.core.mail.backends.smtp'
  302. .. admonition:: SMTPConnection objects
  303. Prior to version 1.2, Django provided a
  304. :class:`~django.core.mail.SMTPConnection` class. This class provided a way
  305. to directly control the use of SMTP to send email. This class has been
  306. deprecated in favor of the generic email backend API.
  307. For backwards compatibility :class:`~django.core.mail.SMTPConnection` is
  308. still available in ``django.core.mail`` as an alias for the SMTP backend.
  309. New code should use :meth:`~django.core.mail.get_connection` instead.
  310. .. _topic-email-console-backend:
  311. Console backend
  312. ~~~~~~~~~~~~~~~
  313. Instead of sending out real e-mails the console backend just writes the
  314. e-mails that would be send to the standard output. By default, the console
  315. backend writes to ``stdout``. You can use a different stream-like object by
  316. providing the ``stream`` keyword argument when constructing the connection.
  317. To specify this backend, put the following in your settings::
  318. EMAIL_BACKEND = 'django.core.mail.backends.console'
  319. This backend is not intended for use in production -- it is provided as a
  320. convenience that can be used during development.
  321. .. _topic-email-file-backend:
  322. File backend
  323. ~~~~~~~~~~~~
  324. The file backend writes e-mails to a file. A new file is created for each new
  325. session that is opened on this backend. The directory to which the files are
  326. written is either taken from the :setting:`EMAIL_FILE_PATH` setting or from
  327. the ``file_path`` keyword when creating a connection with
  328. :meth:`~django.core.mail.get_connection`.
  329. To specify this backend, put the following in your settings::
  330. EMAIL_BACKEND = 'django.core.mail.backends.filebased'
  331. EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
  332. This backend is not intended for use in production -- it is provided as a
  333. convenience that can be used during development.
  334. .. _topic-email-memory-backend:
  335. In-memory backend
  336. ~~~~~~~~~~~~~~~~~
  337. The ``'locmem'`` backend stores messages in a special attribute of the
  338. ``django.core.mail`` module. The ``outbox`` attribute is created when the
  339. first message is send. It's a list with an
  340. :class:`~django.core.mail.EmailMessage` instance for each message that would
  341. be send.
  342. To specify this backend, put the following in your settings::
  343. EMAIL_BACKEND = 'django.core.mail.backends.locmem'
  344. This backend is not intended for use in production -- it is provided as a
  345. convenience that can be used during development and testing.
  346. .. _topic-email-dummy-backend:
  347. Dummy backend
  348. ~~~~~~~~~~~~~
  349. As the name suggests the dummy backend does nothing with your messages. To
  350. specify this backend, put the following in your settings::
  351. EMAIL_BACKEND = 'django.core.mail.backends.dummy'
  352. This backend is not intended for use in production -- it is provided as a
  353. convenience that can be used during development.
  354. .. _topic-custom-email-backend:
  355. Defining a custom e-mail backend
  356. --------------------------------
  357. If you need to change how e-mails are send you can write your own e-mail
  358. backend. The ``EMAIL_BACKEND`` setting in your settings file is then the
  359. Python import path for your backend.
  360. Custom e-mail backends should subclass ``BaseEmailBackend`` that is located in
  361. the ``django.core.mail.backends.base`` module. A custom e-mail backend must
  362. implement the ``send_messages(email_messages)`` method. This method receives a
  363. list of :class:`~django.core.mail.EmailMessage` instances and returns the
  364. number of successfully delivered messages. If your backend has any concept of
  365. a persistent session or connection, you should also implement the ``open()``
  366. and ``close()`` methods. Refer to ``SMTPEmailBackend`` for a reference
  367. implementation.
  368. .. _topics-sending-multiple-emails:
  369. Sending multiple emails
  370. -----------------------
  371. Establishing and closing an SMTP connection (or any other network connection,
  372. for that matter) is an expensive process. If you have a lot of emails to send,
  373. it makes sense to reuse an SMTP connection, rather than creating and
  374. destroying a connection every time you want to send an email.
  375. There are two ways you tell an email backend to reuse a connection.
  376. Firstly, you can use the ``send_messages()`` method. ``send_messages()`` takes
  377. a list of :class:`~django.core.mail.EmailMessage` instances (or subclasses),
  378. and sends them all using a single connection.
  379. For example, if you have a function called ``get_notification_email()`` that
  380. returns a list of :class:`~django.core.mail.EmailMessage` objects representing
  381. some periodic e-mail you wish to send out, you could send these emails using
  382. a single call to send_messages::
  383. from django.core import mail
  384. connection = mail.get_connection() # Use default email connection
  385. messages = get_notification_email()
  386. connection.send_messages(messages)
  387. In this example, the call to ``send_messages()`` opens a connection on the
  388. backend, sends the list of messages, and then closes the connection again.
  389. The second approach is to use the ``open()`` and ``close()`` methods on the
  390. email backend to manually control the connection. ``send_messages()`` will not
  391. manually open or close the connection if it is already open, so if you
  392. manually open the connection, you can control when it is closed. For example::
  393. from django.core import mail
  394. connection = mail.get_connection()
  395. # Manually open the connection
  396. connection.open()
  397. # Construct an email message that uses the connection
  398. email1 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',
  399. ['to1@example.com'], connection=connection)
  400. email1.send() # Send the email
  401. # Construct two more messages
  402. email2 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',
  403. ['to2@example.com'])
  404. email3 = mail.EmailMessage('Hello', 'Body goes here', 'from@example.com',
  405. ['to3@example.com'])
  406. # Send the two emails in a single call -
  407. connection.send_messages([email2, email3])
  408. # The connection was already open so send_messages() doesn't close it.
  409. # We need to manually close the connection.
  410. connection.close()
  411. Testing e-mail sending
  412. ======================
  413. The are times when you do not want Django to send e-mails at all. For example,
  414. while developing a website, you probably don't want to send out thousands of
  415. e-mails -- but you may want to validate that e-mails will be sent to the right
  416. people under the right conditions, and that those e-mails will contain the
  417. correct content.
  418. The easiest way to test your project's use of e-mail is to use the ``console``
  419. email backend. This backend redirects all email to stdout, allowing you to
  420. inspect the content of mail.
  421. The ``file`` email backend can also be useful during development -- this backend
  422. dumps the contents of every SMTP connection to a file that can be inspected
  423. at your leisure.
  424. Another approach is to use a "dumb" SMTP server that receives the e-mails
  425. locally and displays them to the terminal, but does not actually send
  426. anything. Python has a built-in way to accomplish this with a single command::
  427. python -m smtpd -n -c DebuggingServer localhost:1025
  428. This command will start a simple SMTP server listening on port 1025 of
  429. localhost. This server simply prints to standard output all e-mail headers and
  430. the e-mail body. You then only need to set the :setting:`EMAIL_HOST` and
  431. :setting:`EMAIL_PORT` accordingly, and you are set.
  432. For a more detailed discussion of testing and processing of e-mails locally,
  433. see the Python documentation on the `SMTP Server`_.
  434. .. _SMTP Server: http://docs.python.org/library/smtpd.html
  435. SMTPConnection
  436. ==============
  437. .. class:: SMTPConnection
  438. .. deprecated:: 1.2
  439. The ``SMTPConnection`` class has been deprecated in favor of the generic email
  440. backend API.
  441. For backwards compatibility ``SMTPConnection`` is still available in
  442. ``django.core.mail`` as an alias for the :ref:`SMTP backend
  443. <topic-email-smtp-backend>`. New code should use
  444. :meth:`~django.core.mail.get_connection` instead.