{"title":"Sending email","version":"6.1","locale":"pl","docname":"topics/email","url":"/pl/6.1/topics/email/","canonical":"https://djangodocs.dev/pl/6.1/topics/email/","summary":"Django provides wrappers for Python’s email and smtplib modules to simplify composing and sending email. Django’s email framework also supports swapping in…","html":"<span id=\"sending-email\"></span><h1>Sending email<a class=\"heading-anchor\" href=\"#module-django.core.mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django provides wrappers for Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.html#module-email\" title=\"(w Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">email</span></code></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#module-smtplib\" title=\"(w Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">smtplib</span></code></a> modules\nto simplify composing and sending email. Django’s email framework also supports\nswapping in different delivery mechanisms: you can direct email to the console\nor a file during development and an SMTP server or email service provider in\nproduction.</p>\n<p>The code lives in the <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code> module.</p>\n<section id=\"quick-examples\">\n<h2>Quick examples<a class=\"heading-anchor\" href=\"#quick-examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Use <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> for straightforward email sending. For example, to send a\nplain text message:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">send_mail</span>\n\n<span class=\"n\">send_mail</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Here is the message.&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">[</span><span class=\"s2\">&quot;to@example.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>When additional email sending functionality is needed, use the\n<a class=\"reference internal\" href=\"#topic-email-message\"><span class=\"std std-ref\">EmailMessage</span></a> or <a class=\"reference internal\" href=\"#django.core.mail.EmailMultiAlternatives\" title=\"django.core.mail.EmailMultiAlternatives\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMultiAlternatives</span></code></a>\nclass. For example, to send a multipart email that includes both HTML and plain\ntext versions with a specific template and custom headers, you can use the\nfollowing approach:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">EmailMultiAlternatives</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.template.loader</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render_to_string</span>\n\n<span class=\"c1\"># First, render the plain text content.</span>\n<span class=\"n\">text_content</span> <span class=\"o\">=</span> <span class=\"n\">render_to_string</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;templates/emails/my_email.txt&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">context</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;my_variable&quot;</span><span class=\"p\">:</span> <span class=\"mi\">42</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Secondly, render the HTML content.</span>\n<span class=\"n\">html_content</span> <span class=\"o\">=</span> <span class=\"n\">render_to_string</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;templates/emails/my_email.html&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">context</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;my_variable&quot;</span><span class=\"p\">:</span> <span class=\"mi\">42</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Then, create a multipart email instance.</span>\n<span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"n\">EmailMultiAlternatives</span><span class=\"p\">(</span>\n    <span class=\"n\">subject</span><span class=\"o\">=</span><span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">body</span><span class=\"o\">=</span><span class=\"n\">text_content</span><span class=\"p\">,</span>\n    <span class=\"n\">from_email</span><span class=\"o\">=</span><span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">to</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;to@example.com&quot;</span><span class=\"p\">],</span>\n    <span class=\"n\">headers</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;List-Unsubscribe&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;&lt;mailto:unsub@example.com&gt;&quot;</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Lastly, attach the HTML content to the email instance and send.</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">attach_alternative</span><span class=\"p\">(</span><span class=\"n\">html_content</span><span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">send</span><span class=\"p\">()</span>\n</code></pre></div>\n</section>\n<section id=\"configuring-email\">\n<span id=\"topic-email-configuration\"></span><h2>Configuring email<a class=\"heading-anchor\" href=\"#configuring-email\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>New Django projects are not configured to send email by default. Instead, email\nis printed to the console as a development aid (for projects created with\n<a class=\"reference internal\" href=\"/pl/6.1/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a>) or results in a <code class=\"docutils literal notranslate\"><span class=\"pre\">MailerDoesNotExist</span></code> error (when the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting isn’t defined).</p>\n<p>Use the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting to tell Django how to send email. For\nexample, to send through an SMTP server running on the local machine:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;localhost&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>Django abstracts the email sending process into an „email backend” class.\n<a class=\"reference internal\" href=\"#topic-email-backends\"><span class=\"std std-ref\">Email backends</span></a> lists the email backends that come with Django.</p>\n<p>The example above uses Django’s <a class=\"reference internal\" href=\"#topic-email-smtp-backend\"><span class=\"std std-ref\">SMTP email backend</span></a>, which sends using the standard <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol\">SMTP</a> protocol.\nThis backend is useful for many production configurations, including SMTP\nservers in your own infrastructure and most commercial email service providers\n(ESPs). There are also <a class=\"reference internal\" href=\"#topic-third-party-email-backends\"><span class=\"std std-ref\">third-party email backends</span></a> available that integrate directly with ESP\nAPIs or add other sending features.</p>\n<p>During development or testing you often don’t want to send email at all.\nDjango’s test runner <a class=\"reference internal\" href=\"/pl/6.1/topics/testing/tools/#topics-testing-email\"><span class=\"std std-ref\">automatically overrides</span></a> the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration to substitute Django’s <a class=\"reference internal\" href=\"#topic-email-memory-backend\"><span class=\"std std-ref\">memory email\nbackend</span></a>. This prevents test cases from sending\nreal email and gives them access to the messages that would have been sent.\n<a class=\"reference internal\" href=\"#configuring-email-for-development\"><span class=\"std std-ref\">Configuring email for development</span></a> discusses some other\napproaches.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>In earlier releases, Django defaulted to sending email through an SMTP\nserver running on localhost, using the now-deprecated\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a> and related settings.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>Until Django 7.0, if the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting is not defined then the\nearlier behavior still applies: Django will default to using an SMTP server\non localhost (but will issue deprecation warnings). Starting in Django 7.0,\nattempts to send email without <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> defined will result in a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">MailerDoesNotExist</span></code> error.</p>\n<p>Existing projects can opt into the new behavior early by adding\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> to <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.py</span></code>. See <a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers\"><span class=\"std std-ref\">Migrating email to mailers</span></a>.</p>\n</aside>\n<section id=\"multiple-mailers\">\n<h3>Multiple mailers<a class=\"heading-anchor\" href=\"#multiple-mailers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Sometimes different types of email need to be sent in different ways: e.g.,\ninternal vs. external email, different SMTP servers for users in different\nregions, using different services for transactional notifications and bulk\nmarketing email, etc.</p>\n<p>The <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting can define multiple mail configurations. For\nexample:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">os</span>\n\n<span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;smtp.example.net&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;use_tls&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">environ</span><span class=\"p\">[</span><span class=\"s2\">&quot;EMAIL_ACCOUNT_ID&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">environ</span><span class=\"p\">[</span><span class=\"s2\">&quot;EMAIL_API_KEY&quot;</span><span class=\"p\">],</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;notifications&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;example.third.party.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;api_key&quot;</span><span class=\"p\">:</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">environ</span><span class=\"p\">[</span><span class=\"s2\">&quot;THIRD_PARTY_API_KEY&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;region&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;eu&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"s2\">&quot;admin&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;localhost&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>This defines three mailer configurations:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;default&quot;</span></code> sends through an SMTP server at <code class=\"docutils literal notranslate\"><span class=\"pre\">smtp.example.net</span></code> with a TLS\nsecured connection. It reads an account id and API key from environment\nvariables and uses them as the SMTP authentication username and password.\n(Many SMTP services use some variation of this authentication scheme.)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;notifications&quot;</span></code> sends through a hypothetical commercial email service,\nusing a third-party EmailBackend that connects directly to their API.\n(See <a class=\"reference internal\" href=\"#topic-third-party-email-backends\"><span class=\"std std-ref\">Third-party backends</span></a> for pointers on locating real,\ncommunity maintained email backend packages.)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;admin&quot;</span></code> sends through an SMTP server running on <code class=\"docutils literal notranslate\"><span class=\"pre\">localhost</span></code>, with no\nother options required.</p></li>\n</ul>\n<p>With this configuration, you can provide the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument to Django’s\n<a class=\"reference internal\" href=\"#topic-email-sending\"><span class=\"std std-ref\">email sending functions</span></a> to specify a particular\nmailer configuration:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">send_mail</span>\n\n<span class=\"n\">send_mail</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Account activated&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Congratulations, you&#39;re all ready to use our Django app!&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">[</span><span class=\"s2\">&quot;user@example.com&quot;</span><span class=\"p\">],</span>\n    <span class=\"n\">using</span><span class=\"o\">=</span><span class=\"s2\">&quot;notifications&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> is not specified, Django uses the mailer defined for the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;default&quot;</span></code> configuration.</p>\n<p>With reusable apps or Django features that send email for you, there may be an\noption to use a specific mailer configuration. For example, Django’s logging\n<a class=\"reference internal\" href=\"/pl/6.1/ref/logging/#django.utils.log.AdminEmailHandler\" title=\"django.utils.log.AdminEmailHandler\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AdminEmailHandler</span></code></a> allows specifying the mailer\nconfiguration in its <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> option.</p>\n</section>\n</section>\n<section id=\"sending-messages\">\n<span id=\"topic-email-sending\"></span><h2>Sending messages<a class=\"heading-anchor\" href=\"#sending-messages\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code> provides functions for conveniently sending email, as\nwell as classes for building and sending more complex email messages with\nattachments and multiple content types.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>The character set of email sent with <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code> will be set to\nthe value of your <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_CHARSET</span></code></a> setting.</p>\n</aside>\n<section id=\"send-mail\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code><a class=\"heading-anchor\" href=\"#send-mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.send_mail\">\n<span class=\"sig-name descname\"><span class=\"pre\">send_mail</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">subject</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">message</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">from_email</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">recipient_list</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">auth_user</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">auth_password</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">connection</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">html_message</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.send_mail\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.send_mail()</span></code> sends a single email message.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">from_email</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">recipient_list</span></code> parameters\nare required.</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code>: A string.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code>: A string.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">from_email</span></code>: A string. If <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, Django will use the value of the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_FROM_EMAIL</span></code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">recipient_list</span></code>: A list of strings, each an email address. Each\nmember of <code class=\"docutils literal notranslate\"><span class=\"pre\">recipient_list</span></code> will see the other recipients in the „To:”\nfield of the email message.</p></li>\n</ul>\n<p>The following parameters are optional, and must be given as keyword arguments\nif used.</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>: A boolean, default <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>. If set <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code> will suppress some errors during sending. (The exact\nexceptions ignored depend on the email backend in use.)</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>: The optional username to use to authenticate to the SMTP\nserver. If this isn’t provided, Django will use the value of the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_USER</span></code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code>: The optional password to use to authenticate to the\nSMTP server. If this isn’t provided, Django will use the value of the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_PASSWORD</span></code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>: The optional email backend to use to send the mail.\nIf unspecified, an instance of the default backend will be used.\nSee the documentation on <a class=\"reference internal\" href=\"#topic-email-backends\"><span class=\"std std-ref\">Email backends</span></a>\nfor more details.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">html_message</span></code>: If <code class=\"docutils literal notranslate\"><span class=\"pre\">html_message</span></code> is provided, the resulting email will\nbe a <em class=\"mimetype\">multipart/alternative</em> email with <code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code> as the\n<em class=\"mimetype\">text/plain</em> content type and <code class=\"docutils literal notranslate\"><span class=\"pre\">html_message</span></code> as the\n<em class=\"mimetype\">text/html</em> content type.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code>: An optional <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> alias to use to send the mail. If\nunspecified, the default mailer configuration will be used.</p></li>\n</ul>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> are not\nallowed with the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument.</p>\n<p>The return value will be the number of successfully delivered messages (which\ncan be <code class=\"docutils literal notranslate\"><span class=\"pre\">0</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">1</span></code> since it can only send one message).</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>\narguments are deprecated. In most cases they can be replaced by <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code>\nwith an appropriate <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a>,\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-auth\"><span class=\"std std-ref\">Replacing auth_user and auth_password</span></a>, and\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a>.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument was added.</p>\n<p>Older versions ignored <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>,\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code> when a <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> was also provided.\nThis now raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>.</p>\n</aside>\n</section>\n<section id=\"send-mass-mail\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code><a class=\"heading-anchor\" href=\"#send-mass-mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.send_mass_mail\">\n<span class=\"sig-name descname\"><span class=\"pre\">send_mass_mail</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">datatuple</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">auth_user</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">auth_password</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">connection</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.send_mass_mail\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.send_mass_mail()</span></code> is intended to handle mass emailing.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">datatuple</span></code> is a tuple in which each element is in this format:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">message</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"p\">,</span> <span class=\"n\">recipient_list</span><span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> have the\nsame functions as in <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a>. They must be given as keyword arguments\nif used, and are not allowed with the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument.</p>\n<p>The keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> is an optional <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> alias to use\nto send the mail. If unspecified, the default mailer configuration will be\nused.</p>\n<p>Each separate element of <code class=\"docutils literal notranslate\"><span class=\"pre\">datatuple</span></code> results in a separate email message.\nAs in <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a>, recipients in the same <code class=\"docutils literal notranslate\"><span class=\"pre\">recipient_list</span></code> will all see\nthe other addresses in the email messages» „To:” field.</p>\n<p>For example, the following code would send two different messages to\ntwo different sets of recipients; however, only one connection to the\nmail server would be opened:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">message1</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Here is the message&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">[</span><span class=\"s2\">&quot;first@example.com&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;other@example.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n<span class=\"n\">message2</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Another Subject&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Here is another message&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">[</span><span class=\"s2\">&quot;second@test.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n<span class=\"n\">send_mass_mail</span><span class=\"p\">((</span><span class=\"n\">message1</span><span class=\"p\">,</span> <span class=\"n\">message2</span><span class=\"p\">))</span>\n</code></pre></div>\n<p>The return value will be the number of successfully delivered messages.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>\narguments are deprecated. In most cases they can be replaced by <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code>\nwith an appropriate <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a>,\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-auth\"><span class=\"std std-ref\">Replacing auth_user and auth_password</span></a>, and\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a>.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument was added.</p>\n<p>Older versions ignored <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code>,\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code> when a <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> was also provided.\nThis now raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>.</p>\n</aside>\n<section id=\"send-mass-mail-vs-send-mail\">\n<h4><code class=\"docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code> vs. <code class=\"docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code><a class=\"heading-anchor\" href=\"#send-mass-mail-vs-send-mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The main difference between <a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> and repeatedly calling\n<a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> is that <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> opens a connection to the mail\nserver each time it’s executed, while <a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> uses a single\nconnection for all of its messages. This makes <a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> slightly\nmore efficient.</p>\n<p><a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> with multiple <code class=\"docutils literal notranslate\"><span class=\"pre\">to</span></code> addresses sends a single email message,\nwith <code class=\"docutils literal notranslate\"><span class=\"pre\">john&#64;example.com</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">jane&#64;example.com</span></code> both appearing in the „To:”\nfield:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">send_mail</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Subject&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Message.&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">[</span><span class=\"s2\">&quot;john@example.com&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;jane@example.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p><a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> sends a separate message per <code class=\"docutils literal notranslate\"><span class=\"pre\">datatuple</span></code> element, so\n<code class=\"docutils literal notranslate\"><span class=\"pre\">john&#64;example.com</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">jane&#64;example.com</span></code> each receive their own email:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">datatuple</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"p\">(</span><span class=\"s2\">&quot;Subject&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message.&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;john@example.com&quot;</span><span class=\"p\">]),</span>\n    <span class=\"p\">(</span><span class=\"s2\">&quot;Subject&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message.&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;jane@example.com&quot;</span><span class=\"p\">]),</span>\n<span class=\"p\">)</span>\n<span class=\"n\">send_mass_mail</span><span class=\"p\">(</span><span class=\"n\">datatuple</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"mail-admins\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins()</span></code><a class=\"heading-anchor\" href=\"#mail-admins\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mail_admins\">\n<span class=\"sig-name descname\"><span class=\"pre\">mail_admins</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">subject</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">message</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">connection</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">html_message</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.mail_admins\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.mail_admins()</span></code> is a shortcut for sending an email to the\nsite admins, as defined in the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ADMINS</span></code></a> setting.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins()</span></code> prefixes the subject with the value of the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_SUBJECT_PREFIX\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SUBJECT_PREFIX</span></code></a> setting, which is <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;[Django]</span> <span class=\"pre\">&quot;</span></code> by default.</p>\n<p>The „From:” header of the email will be the value of the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-SERVER_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SERVER_EMAIL</span></code></a> setting.</p>\n<p>This method exists for convenience and readability.</p>\n<p>If <code class=\"docutils literal notranslate\"><span class=\"pre\">html_message</span></code> is provided, the resulting email will be a\n<em class=\"mimetype\">multipart/alternative</em> email with <code class=\"docutils literal notranslate\"><span class=\"pre\">message</span></code> as the\n<em class=\"mimetype\">text/plain</em> content type and <code class=\"docutils literal notranslate\"><span class=\"pre\">html_message</span></code> as the\n<em class=\"mimetype\">text/html</em> content type.</p>\n<p>The keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> is an optional <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> alias to use\nto send the mail. If unspecified, the default mailer configuration will be\nused.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> arguments are deprecated. In most\ncases they can be replaced by <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> with an appropriate\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a> and\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a>.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument was added.</p>\n<p>Older versions ignored <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code> when a <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>\nwas also provided. This now raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>.</p>\n</aside>\n</section>\n<section id=\"mail-managers\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">mail_managers()</span></code><a class=\"heading-anchor\" href=\"#mail-managers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mail_managers\">\n<span class=\"sig-name descname\"><span class=\"pre\">mail_managers</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">subject</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">message</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">connection</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">html_message</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.mail_managers\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.mail_managers()</span></code> is just like <code class=\"docutils literal notranslate\"><span class=\"pre\">mail_admins()</span></code>, except it\nsends an email to the site managers, as defined in the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MANAGERS</span></code></a>\nsetting.</p>\n<p>The keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> is an optional <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> alias to use\nto send the mail. If unspecified, the default mailer configuration will be\nused.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> arguments are deprecated. In most\ncases they can be replaced by <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> with an appropriate\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a> and\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a>.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument was added.</p>\n<p>Older versions ignored <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code> when a <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>\nwas also provided. This now raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>.</p>\n</aside>\n</section>\n<section id=\"the-emailmessage-class\">\n<span id=\"topic-email-message\"></span><h3>The <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code> class<a class=\"heading-anchor\" href=\"#the-emailmessage-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django’s <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> and <a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> functions are actually\nthin wrappers that make use of the <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> class.</p>\n<p>Not all features of the <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> class are available through the\n<a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> and related wrapper functions. If you wish to use advanced\nfeatures, such as BCC’ed recipients, file attachments, or multi-part email,\nyou’ll need to create <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> instances directly.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>This is a design feature. <a class=\"reference internal\" href=\"#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> and related functions were\noriginally the only interface Django provided. However, the list of\nparameters they accepted was slowly growing over time. It made sense to\nmove to a more object-oriented design for email messages and retain the\noriginal functions only for backwards compatibility.</p>\n</aside>\n<p><a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> is responsible for creating the email message itself. The\n<a class=\"reference internal\" href=\"#topic-email-backends\"><span class=\"std std-ref\">email backend</span></a> is then responsible for sending the\nemail.</p>\n<p>For convenience, <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> provides a <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage.send\" title=\"django.core.mail.EmailMessage.send\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">send()</span></code></a>\nmethod for sending a single email. If you need to send multiple messages, the\nemail backend API <a class=\"reference internal\" href=\"#topics-sending-multiple-emails\"><span class=\"std std-ref\">provides an alternative</span></a>.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">EmailMessage</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The <code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code> class is initialized with the following\nparameters. All parameters are optional and can be set at any time prior\nto calling the <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage.send\" title=\"django.core.mail.EmailMessage.send\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">send()</span></code></a> method.</p>\n<p>The first four parameters can be passed as positional or keyword arguments,\nbut must be in the given order if positional arguments are used:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">subject</span></code>: The subject line of the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">body</span></code>: The body text. This should be a plain text message.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">from_email</span></code>: The sender’s address. Both <code class=\"docutils literal notranslate\"><span class=\"pre\">fred&#64;example.com</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;Fred&quot;</span> <span class=\"pre\">&lt;fred&#64;example.com&gt;</span></code> forms are supported (see\n<a class=\"reference internal\" href=\"#formatting-addresses\"><span class=\"std std-ref\">Formatting email addresses</span></a>). If omitted, the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_FROM_EMAIL</span></code></a> setting is used.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">to</span></code>: A list or tuple of recipient addresses.</p></li>\n</ul>\n<p>The following parameters must be given as keyword arguments if used:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">cc</span></code>: A list or tuple of recipient addresses used in the „Cc” header\nwhen sending the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">bcc</span></code>: A list or tuple of addresses used for blind carbon copies when\nsending the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">reply_to</span></code>: A list or tuple of recipient addresses used in the\n„Reply-To” header when sending the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">attachments</span></code>: A list of attachments to put on the message. Each can\nbe an instance of <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> or\n<a class=\"reference internal\" href=\"#django.core.mail.EmailAttachment\" title=\"django.core.mail.EmailAttachment\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailAttachment</span></code></a>, or a tuple with attributes\n<code class=\"docutils literal notranslate\"><span class=\"pre\">(filename,</span> <span class=\"pre\">content,</span> <span class=\"pre\">mimetype)</span></code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.0\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>Support for <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> objects in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">attachments</span></code> list was added.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Support for Python’s legacy <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.mime.html#email.mime.base.MIMEBase\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEBase</span></code></a>\nobjects in <code class=\"docutils literal notranslate\"><span class=\"pre\">attachments</span></code> is deprecated. Use\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> instead.</p>\n</aside>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">headers</span></code>: A dictionary of extra headers to put on the message. The\nkeys are the header name, values are the header values. It’s up to the\ncaller to ensure header names and values are in the correct format for\nan email message. The corresponding attribute is <code class=\"docutils literal notranslate\"><span class=\"pre\">extra_headers</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>: An <a class=\"reference internal\" href=\"#topic-email-backends\"><span class=\"std std-ref\">email backend</span></a> instance.\nThis parameter is ignored when using\n<a class=\"reference internal\" href=\"#topics-sending-multiple-emails\"><span class=\"std std-ref\">send_messages()</span></a>.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> argument is deprecated. Instead, define a\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration with the desired connection options,\nand then call <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage.send\" title=\"django.core.mail.EmailMessage.send\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">EmailMessage.send(using=&quot;...&quot;)</span></code></a> with that\nconfiguration’s alias. See <a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers\"><span class=\"std std-ref\">Migrating email to mailers</span></a>.</p>\n</aside>\n</li>\n</ul>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing all except the first four parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<p>Na przykład:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">EmailMessage</span>\n\n<span class=\"n\">email</span> <span class=\"o\">=</span> <span class=\"n\">EmailMessage</span><span class=\"p\">(</span>\n    <span class=\"n\">subject</span><span class=\"o\">=</span><span class=\"s2\">&quot;Hello&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">body</span><span class=\"o\">=</span><span class=\"s2\">&quot;Body goes here&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">from_email</span><span class=\"o\">=</span><span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">to</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;to1@example.com&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;to2@example.com&quot;</span><span class=\"p\">],</span>\n    <span class=\"n\">bcc</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;bcc@example.com&quot;</span><span class=\"p\">],</span>\n    <span class=\"n\">reply_to</span><span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;another@example.com&quot;</span><span class=\"p\">],</span>\n    <span class=\"n\">headers</span><span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;Message-ID&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;foo&quot;</span><span class=\"p\">},</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>The class has the following methods:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage.send\">\n<span class=\"sig-name descname\"><span class=\"pre\">send</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">using</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage.send\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Sends the message. Returns <code class=\"docutils literal notranslate\"><span class=\"pre\">1</span></code> if the message was sent successfully,\notherwise <code class=\"docutils literal notranslate\"><span class=\"pre\">0</span></code>. (An empty list of recipients returns <code class=\"docutils literal notranslate\"><span class=\"pre\">0</span></code> – it will\nnot raise an exception.)</p>\n<p>The optional <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> keyword argument specifies a <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a>\nalias to use to send the mail. If not given, the default mailer\nconfiguration will be used.</p>\n<p>If a deprecated connection was specified when the email was\nconstructed, that connection will be used. Providing both a connection\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> will raise an error.</p>\n<p>If the deprecated keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>,\ncertain backend-dependent exceptions while sending the message will be\nignored. Providing both <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> will raise an\nerror.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument was added.</p>\n<p>Older versions ignored <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code> when a <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code>\nwas also provided. This now raises a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> argument is deprecated. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a> for alternatives.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage.message\">\n<span class=\"sig-name descname\"><span class=\"pre\">message</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">policy</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">email.policy.default</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage.message\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Constructs and returns a Python <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.EmailMessage\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">email.message.EmailMessage</span></code></a>\nobject representing the message to be sent.</p>\n<p>The keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">policy</span></code> allows specifying the set of rules for\nupdating and serializing the representation of the message. It must be\nan <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#module-email.policy\" title=\"(w Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">email.policy.Policy</span></code></a> object. Defaults to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.default\" title=\"(w Python v3.14)\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">email.policy.default</span></code></a>. In certain cases you may want to use\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.SMTP\" title=\"(w Python v3.14)\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">SMTP</span></code></a>, <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.SMTPUTF8\" title=\"(w Python v3.14)\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">SMTPUTF8</span></code></a> or a custom\npolicy. For example, the <a class=\"reference internal\" href=\"#topic-email-smtp-backend\"><span class=\"std std-ref\">SMTP email backend</span></a> uses the <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.SMTP\" title=\"(w Python v3.14)\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">SMTP</span></code></a> policy\nto ensure <code class=\"docutils literal notranslate\"><span class=\"pre\">\\r\\n</span></code> line endings as required by the SMTP protocol.</p>\n<p>If you ever need to extend Django’s <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> class,\nyou’ll probably want to override this method to put the content you\nwant into the Python EmailMessage object.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.0\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">policy</span></code> keyword argument was added and the return type was\nupdated to an instance of <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.EmailMessage\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a>.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage.recipients\">\n<span class=\"sig-name descname\"><span class=\"pre\">recipients</span></span><span class=\"sig-paren\">(</span><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage.recipients\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a list of all the recipients of the message, whether they’re\nrecorded in the <code class=\"docutils literal notranslate\"><span class=\"pre\">to</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">cc</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">bcc</span></code> attributes. This is another\nmethod you might need to override when subclassing, because the SMTP\nserver needs to be told the full list of recipients when the message\nis sent. If you add another way to specify recipients in your class,\nthey need to be returned from this method as well.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage.attach\">\n<span class=\"sig-name descname\"><span class=\"pre\">attach</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">filename</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mimetype</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage.attach\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dt class=\"sig sig-object py\">\n<span class=\"sig-name descname\"><span class=\"pre\">attach</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mimepart</span></span></em><span class=\"sig-paren\">)</span></dt>\n<dd><p>Creates a new attachment and adds it to the message. There are two ways\nto call <code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">attach()</span></code>:</p>\n<ul>\n<li><p>You can pass it three arguments: <code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code>. <code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code> is the name of the file attachment as it\nwill appear in the email, <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> is the data that will be\ncontained inside the attachment and <code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code> is the optional MIME\ntype for the attachment. If you omit <code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code>, the MIME content\ntype will be guessed from the filename of the attachment.</p>\n<p>Na przykład:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">message</span><span class=\"o\">.</span><span class=\"n\">attach</span><span class=\"p\">(</span><span class=\"s2\">&quot;design.png&quot;</span><span class=\"p\">,</span> <span class=\"n\">img_data</span><span class=\"p\">,</span> <span class=\"s2\">&quot;image/png&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>If you specify a <code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code> of <em class=\"mimetype\">message/rfc822</em>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> can be a <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.core.mail.EmailMessage</span></code></a> or\nPython’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.EmailMessage\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">email.message.EmailMessage</span></code></a> or\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.compat32-message.html#email.message.Message\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">email.message.Message</span></code></a>.</p>\n<p>For a <code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code> starting with <em class=\"mimetype\">text/</em>, content is\nexpected to be a string. Binary data will be decoded using UTF-8,\nand if that fails, the MIME type will be changed to\n<em class=\"mimetype\">application/octet-stream</em> and the data will be attached\nunchanged.</p>\n</li>\n<li><p>Or for attachments requiring additional headers or parameters, you\ncan pass <code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">attach()</span></code> a single Python\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> object. This will be attached\ndirectly to the resulting message. For example, to attach an inline\nimage with a <em class=\"mailheader\">Content-ID</em>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">email.utils</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">email.message</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">MIMEPart</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">EmailMultiAlternatives</span>\n\n<span class=\"n\">message</span> <span class=\"o\">=</span> <span class=\"n\">EmailMultiAlternatives</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\n<span class=\"n\">image_data_bytes</span> <span class=\"o\">=</span> <span class=\"o\">...</span>  <span class=\"c1\"># Load image as bytes</span>\n\n<span class=\"c1\"># Create a random Content-ID, including angle brackets</span>\n<span class=\"n\">cid</span> <span class=\"o\">=</span> <span class=\"n\">email</span><span class=\"o\">.</span><span class=\"n\">utils</span><span class=\"o\">.</span><span class=\"n\">make_msgid</span><span class=\"p\">()</span>\n<span class=\"n\">inline_image</span> <span class=\"o\">=</span> <span class=\"n\">email</span><span class=\"o\">.</span><span class=\"n\">message</span><span class=\"o\">.</span><span class=\"n\">MIMEPart</span><span class=\"p\">()</span>\n<span class=\"n\">inline_image</span><span class=\"o\">.</span><span class=\"n\">set_content</span><span class=\"p\">(</span>\n    <span class=\"n\">image_data_bytes</span><span class=\"p\">,</span>\n    <span class=\"n\">maintype</span><span class=\"o\">=</span><span class=\"s2\">&quot;image&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">subtype</span><span class=\"o\">=</span><span class=\"s2\">&quot;png&quot;</span><span class=\"p\">,</span>  <span class=\"c1\"># or &quot;jpeg&quot;, etc. depending on the image type</span>\n    <span class=\"n\">disposition</span><span class=\"o\">=</span><span class=\"s2\">&quot;inline&quot;</span><span class=\"p\">,</span>\n    <span class=\"n\">cid</span><span class=\"o\">=</span><span class=\"n\">cid</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n<span class=\"n\">message</span><span class=\"o\">.</span><span class=\"n\">attach</span><span class=\"p\">(</span><span class=\"n\">inline_image</span><span class=\"p\">)</span>\n<span class=\"c1\"># Refer to Content-ID in HTML without angle brackets</span>\n<span class=\"n\">message</span><span class=\"o\">.</span><span class=\"n\">attach_alternative</span><span class=\"p\">(</span><span class=\"sa\">f</span><span class=\"s1\">&#39;… &lt;img src=&quot;cid:</span><span class=\"si\">{</span><span class=\"n\">cid</span><span class=\"p\">[</span><span class=\"mi\">1</span><span class=\"p\">:</span><span class=\"o\">-</span><span class=\"mi\">1</span><span class=\"p\">]</span><span class=\"si\">}</span><span class=\"s1\">&quot;&gt; …&#39;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.contentmanager.html#email.contentmanager.set_content\" title=\"(w Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">email.contentmanager.set_content()</span></code></a> documentation\ndescribes the supported arguments for <code class=\"docutils literal notranslate\"><span class=\"pre\">MIMEPart.set_content()</span></code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.0\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>Support for <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> attachments was\nadded.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Support for <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.mime.html#email.mime.base.MIMEBase\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">email.mime.base.MIMEBase</span></code></a> attachments is\ndeprecated. Use <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">MIMEPart</span></code></a> instead.</p>\n</aside>\n</li>\n</ul>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage.attach_file\">\n<span class=\"sig-name descname\"><span class=\"pre\">attach_file</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">path</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mimetype</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMessage.attach_file\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Creates a new attachment using a file from your filesystem. Call it\nwith the path of the file to attach and, optionally, the MIME type to\nuse for the attachment. If the MIME type is omitted, it will be guessed\nfrom the filename. You can use it like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">message</span><span class=\"o\">.</span><span class=\"n\">attach_file</span><span class=\"p\">(</span><span class=\"s2\">&quot;/images/weather_map.png&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>For MIME types starting with <em class=\"mimetype\">text/</em>, binary data is handled\nas in <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage.attach\" title=\"django.core.mail.EmailMessage.attach\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">attach()</span></code></a>.</p>\n</dd></dl>\n\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailAttachment\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">EmailAttachment</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailAttachment\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A named tuple to store attachments to an email.</p>\n<p>The named tuple has the following indexes:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">filename</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code></p></li>\n</ul>\n</dd></dl>\n\n<section id=\"sending-alternative-content-types\">\n<h4>Sending alternative content types<a class=\"heading-anchor\" href=\"#sending-alternative-content-types\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<section id=\"sending-multiple-content-versions\">\n<h5>Sending multiple content versions<a class=\"heading-anchor\" href=\"#sending-multiple-content-versions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>It can be useful to include multiple versions of the content in an email; the\nclassic example is to send both text and HTML versions of a message. With\nDjango’s email library, you can do this using the\n<a class=\"reference internal\" href=\"#django.core.mail.EmailMultiAlternatives\" title=\"django.core.mail.EmailMultiAlternatives\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMultiAlternatives</span></code></a> class.</p>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMultiAlternatives\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">EmailMultiAlternatives</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMultiAlternatives\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A subclass of <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> that allows additional versions of the\nmessage body in the email via the <a class=\"reference internal\" href=\"#django.core.mail.EmailMultiAlternatives.attach_alternative\" title=\"django.core.mail.EmailMultiAlternatives.attach_alternative\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">attach_alternative()</span></code></a> method. This\ndirectly inherits all methods (including the class initialization) from\n<a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a>.</p>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMultiAlternatives.alternatives\">\n<span class=\"sig-name descname\"><span class=\"pre\">alternatives</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMultiAlternatives.alternatives\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A list of <a class=\"reference internal\" href=\"#django.core.mail.EmailAlternative\" title=\"django.core.mail.EmailAlternative\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailAlternative</span></code></a> named tuples.\nThis is particularly useful in tests:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span><span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">alternatives</span><span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">alternatives</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">content</span><span class=\"p\">,</span> <span class=\"n\">html_content</span><span class=\"p\">)</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertEqual</span><span class=\"p\">(</span><span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">alternatives</span><span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">mimetype</span><span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Alternatives should only be added using the <a class=\"reference internal\" href=\"#django.core.mail.EmailMultiAlternatives.attach_alternative\" title=\"django.core.mail.EmailMultiAlternatives.attach_alternative\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">attach_alternative()</span></code></a>\nmethod, or passed to the constructor.</p>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMultiAlternatives.attach_alternative\">\n<span class=\"sig-name descname\"><span class=\"pre\">attach_alternative</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mimetype</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMultiAlternatives.attach_alternative\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Attach an alternative representation of the message body in the email.</p>\n<p>For example, to send a text and HTML combination, you could write:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">EmailMultiAlternatives</span>\n\n<span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;hello&quot;</span>\n<span class=\"n\">from_email</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;from@example.com&quot;</span>\n<span class=\"n\">to</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;to@example.com&quot;</span>\n<span class=\"n\">text_content</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;This is an important message.&quot;</span>\n<span class=\"n\">html_content</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;&lt;p&gt;This is an &lt;strong&gt;important&lt;/strong&gt; message.&lt;/p&gt;&quot;</span>\n<span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"n\">EmailMultiAlternatives</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">text_content</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">to</span><span class=\"p\">])</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">attach_alternative</span><span class=\"p\">(</span><span class=\"n\">html_content</span><span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">send</span><span class=\"p\">()</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMultiAlternatives.body_contains\">\n<span class=\"sig-name descname\"><span class=\"pre\">body_contains</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">text</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailMultiAlternatives.body_contains\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Returns a boolean indicating whether the provided <code class=\"docutils literal notranslate\"><span class=\"pre\">text</span></code> is\ncontained in the email <code class=\"docutils literal notranslate\"><span class=\"pre\">body</span></code> and in all attached MIME type\n<code class=\"docutils literal notranslate\"><span class=\"pre\">text/*</span></code> alternatives.</p>\n<p>This can be useful when testing emails. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">test_contains_email_content</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    <span class=\"n\">subject</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;Hello World&quot;</span>\n    <span class=\"n\">from_email</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;from@example.com&quot;</span>\n    <span class=\"n\">to</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;to@example.com&quot;</span>\n    <span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"n\">EmailMultiAlternatives</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"s2\">&quot;I am content.&quot;</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">to</span><span class=\"p\">])</span>\n    <span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">attach_alternative</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;p&gt;I am content.&lt;/p&gt;&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\n\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">body_contains</span><span class=\"p\">(</span><span class=\"s2\">&quot;I am content&quot;</span><span class=\"p\">),</span> <span class=\"kc\">True</span><span class=\"p\">)</span>\n    <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">assertIs</span><span class=\"p\">(</span><span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">body_contains</span><span class=\"p\">(</span><span class=\"s2\">&quot;&lt;p&gt;I am content.&lt;/p&gt;&quot;</span><span class=\"p\">),</span> <span class=\"kc\">False</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n</dd></dl>\n\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailAlternative\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">EmailAlternative</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.EmailAlternative\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>A named tuple to store alternative versions of email content.</p>\n<p>The named tuple has the following indexes:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">mimetype</span></code></p></li>\n</ul>\n</dd></dl>\n\n</section>\n<section id=\"updating-the-default-content-type\">\n<h5>Updating the default content type<a class=\"heading-anchor\" href=\"#updating-the-default-content-type\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h5>\n<p>By default, the MIME type of the <code class=\"docutils literal notranslate\"><span class=\"pre\">body</span></code> parameter in an <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a>\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;text/plain&quot;</span></code>. It is good practice to leave this alone, because it\nguarantees that any recipient will be able to read the email, regardless of\ntheir mail client. However, if you are confident that your recipients can\nhandle an alternative content type, you can use the <code class=\"docutils literal notranslate\"><span class=\"pre\">content_subtype</span></code>\nattribute on the <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> class to change the main content type.\nThe major type will always be <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;text&quot;</span></code>, but you can change the subtype. For\nexample:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">msg</span> <span class=\"o\">=</span> <span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">html_content</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">to</span><span class=\"p\">])</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">content_subtype</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;html&quot;</span>  <span class=\"c1\"># Main content is now text/html</span>\n<span class=\"n\">msg</span><span class=\"o\">.</span><span class=\"n\">send</span><span class=\"p\">()</span>\n</code></pre></div>\n</section>\n</section>\n</section>\n<section id=\"safely-sending-email\">\n<h3>Safely sending email<a class=\"heading-anchor\" href=\"#safely-sending-email\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Any public website that can send email will eventually be targeted by attempts\nto abuse it for spam, phishing, or other malicious content. While a complete\ndiscussion of vulnerabilities in sending email is beyond the scope of Django’s\ndocumentation, there are many references available on the web. Two good\nstarting points are:</p>\n<ul class=\"simple\">\n<li><p>Princeton University’s guidance on <a class=\"reference external\" href=\"https://sitebuilder.princeton.edu/site-administration/spam\">preventing email abuse in web forms</a>.\nAlthough this is an internal reference for users of Princeton’s Drupal Site\nBuilder, nearly all of its advice applies equally to sites built with Django\n(or any web framework).</p></li>\n<li><p>OWASP’s <a class=\"reference external\" href=\"https://cheatsheetseries.owasp.org/cheatsheets/Email_Validation_and_Verification_Cheat_Sheet.html\">Email Validation and Verification in Identity Systems Cheat Sheet</a>.\nThis primarily covers using email in authentication contexts. While many of\nits recommendations are handled by <a class=\"reference internal\" href=\"/pl/6.1/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.auth</span></code></a> and other\nDjango features, items like rate limiting and securing email change workflows\nare the developer’s responsibility.</p></li>\n</ul>\n<p>Thinking through how email might be abused (and taking steps to mitigate it) is\nespecially important if your site can send to unverified addresses. Features\nlike newsletter sign-up, contact forms that cc or auto-reply to the sender, and\n„share this page” can be attractive targets.</p>\n<section id=\"formatting-email-addresses\">\n<span id=\"formatting-addresses\"></span><h4>Formatting email addresses<a class=\"heading-anchor\" href=\"#formatting-email-addresses\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Email addresses allow a „friendly” display name alongside the <code class=\"docutils literal notranslate\"><span class=\"pre\">user&#64;domain</span></code>\naddress. For example, you could include your company name in the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_FROM_EMAIL</span></code></a> setting:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">DEFAULT_FROM_EMAIL</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;&quot;Example, Inc.&quot; &lt;contact@example.com&gt;&#39;</span>\n</code></pre></div>\n<p>The double quotes around <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;Example,</span> <span class=\"pre\">Inc.&quot;</span></code> are needed so the comma isn’t read\nas separating two different addresses. A fixed address, written out by hand as\nin the example above, is safe, but composing one from variable parts\n(especially untrusted input) needs more care.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Ostrzeżenie</p>\n<p>Never use string formatting to build an email address from variable parts.\nFor example, <code class=\"docutils literal notranslate\"><span class=\"pre\">f'&quot;{name}&quot;</span> <span class=\"pre\">&lt;{email}&gt;'</span></code> is <strong>unsafe</strong>.</p>\n</aside>\n<p>Email address headers have complex syntax rules (much like HTML or SQL), so\nconstructing them by combining strings creates an injection vulnerability. Even\nif you’ve <a class=\"reference internal\" href=\"/pl/6.1/ref/validators/#django.core.validators.EmailValidator\" title=\"django.core.validators.EmailValidator\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">validated</span></code></a> the format of <code class=\"docutils literal notranslate\"><span class=\"pre\">email</span></code>, an\nattacker could exploit the <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> portion to inject additional addresses.</p>\n<p>To avoid this, always use a well-tested library specifically meant to format\nemail addresses, like Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.headerregistry.html#email.headerregistry.Address\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">email.headerregistry.Address</span></code></a> class (the\nreplacement for the legacy <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.utils.html#email.utils.formataddr\" title=\"(w Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">formataddr()</span></code></a> function, which does\nnot support internationalized domain names).</p>\n<p>For example, to include a user’s full name when sending them email (where\n<code class=\"docutils literal notranslate\"><span class=\"pre\">user</span></code> is an instance of the default <a class=\"reference internal\" href=\"/pl/6.1/ref/contrib/auth/#django.contrib.auth.models.User\" title=\"django.contrib.auth.models.User\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">User</span></code></a> model):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">send_mail</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">email.headerregistry</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Address</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">send_mail_to_user</span><span class=\"p\">(</span><span class=\"n\">user</span><span class=\"p\">,</span> <span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">body</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n    <span class=\"c1\"># Safely create an email address with the user&#39;s name.</span>\n    <span class=\"c1\"># (addr_spec is the technical term for the user@domain address.)</span>\n    <span class=\"n\">address</span> <span class=\"o\">=</span> <span class=\"n\">Address</span><span class=\"p\">(</span>\n        <span class=\"n\">display_name</span><span class=\"o\">=</span><span class=\"n\">user</span><span class=\"o\">.</span><span class=\"n\">get_full_name</span><span class=\"p\">(),</span>\n        <span class=\"n\">addr_spec</span><span class=\"o\">=</span><span class=\"n\">user</span><span class=\"o\">.</span><span class=\"n\">email</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n    <span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"n\">subject</span><span class=\"p\">,</span> <span class=\"n\">body</span><span class=\"p\">,</span> <span class=\"n\">from_email</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">address</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>Django’s built-in email backends support using\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.headerregistry.html#email.headerregistry.Address\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Address</span></code></a> objects directly in any address field,\nas shown here. So do many custom and third-party email backends. But if this\ncauses a <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code> or other problem with a particular backend, use\n<code class=\"docutils literal notranslate\"><span class=\"pre\">str(address)</span></code> to convert the object to a safe, properly formatted string.</p>\n</section>\n<section id=\"preventing-header-injection\">\n<h4>Preventing header injection<a class=\"heading-anchor\" href=\"#preventing-header-injection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p><a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Email_injection\">Email header injection</a> is a security exploit in which an attacker\nmanipulates email headers to change the intended sender or recipients, subject,\nor potentially even the entire visible message body.</p>\n<p>One type of header injection exploits address header syntax. You are\nresponsible for preventing this when constructing email addresses from\nuser-supplied input, as described in <a class=\"reference internal\" href=\"#formatting-addresses\"><span class=\"std std-ref\">Formatting email addresses</span></a> above.</p>\n<p>Another (perhaps better understood) attack, CRLF injection, uses carriage\nreturn and line feed characters to insert additional headers into the email.\nDjango prevents this by raising a <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#ValueError\" title=\"(w Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ValueError</span></code></a> if those characters appear\nin any header field when trying to send the message.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.0\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>Older versions raised <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.BadHeaderError</span></code> for some\ninvalid headers. This has been replaced with <code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">ValueError</span></code>.</p>\n</aside>\n<p>Django’s CRLF protection relies on using Python’s modern\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.EmailPolicy\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailPolicy</span></code></a> in Django’s <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage.message\" title=\"django.core.mail.EmailMessage.message\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">EmailMessage.message()</span></code></a>.\nCustom email backends that don’t call that function, or that call it with the\nlegacy <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.compat32\" title=\"(w Python v3.14)\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">compat32</span></code></a> policy, are responsible for implementing\ntheir own CRLF injection prevention.</p>\n</section>\n</section>\n<section id=\"sending-many-messages-efficiently\">\n<span id=\"topics-sending-multiple-emails\"></span><h3>Sending many messages efficiently<a class=\"heading-anchor\" href=\"#sending-many-messages-efficiently\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Establishing and closing an SMTP connection (or any other network connection,\nfor that matter) is an expensive process. If you have a lot of emails to send,\nit makes sense to reuse an SMTP connection, rather than creating and\ndestroying a connection every time you want to send an email.</p>\n<p>There are two ways to tell an email backend to reuse a connection. Both involve\nobtaining an email backend instance from <a class=\"reference internal\" href=\"#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mail.mailers</span></code></a> and using the\n<a class=\"reference internal\" href=\"#topic-email-backend-api\"><span class=\"std std-ref\">backend’s API</span></a>.</p>\n<p>The first approach is to use the backend’s <code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages()</span></code> method. This\ntakes a list of <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> (or subclass) instances, and sends them\nall using that single connection.</p>\n<p>For example, if you have a function called <code class=\"docutils literal notranslate\"><span class=\"pre\">get_notification_emails()</span></code> that\nreturns a list of <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> objects representing some periodic\nemail you wish to send out, you could send these emails using a single call to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages()</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">mail</span>\n\n<span class=\"n\">email_list</span> <span class=\"o\">=</span> <span class=\"n\">get_notification_emails</span><span class=\"p\">()</span>\n<span class=\"c1\"># Use the default mailer. You could substitute</span>\n<span class=\"c1\"># mail.mailers[&quot;alias&quot;] for a specific mailer.</span>\n<span class=\"n\">backend</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">mailers</span><span class=\"o\">.</span><span class=\"n\">default</span>\n<span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">(</span><span class=\"n\">email_list</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>In this example, the call to <code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages()</span></code> opens a connection on the\nbackend, sends the list of messages, and then closes the connection again.\n(This is how <a class=\"reference internal\" href=\"#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> is implemented.)</p>\n<p>The second approach is to use the <code class=\"docutils literal notranslate\"><span class=\"pre\">open()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> methods on the\nemail backend to manually control the connection. <code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages()</span></code> will not\nopen or close the connection if it is already open, so if you\nmanually open the connection, you can control when it is closed. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">mail</span>\n\n<span class=\"c1\"># Use the &quot;notifications&quot; mailer configuration.</span>\n<span class=\"n\">backend</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">mailers</span><span class=\"p\">[</span><span class=\"s2\">&quot;notifications&quot;</span><span class=\"p\">]</span>\n\n<span class=\"c1\"># Manually open the connection.</span>\n<span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">open</span><span class=\"p\">()</span>\n\n<span class=\"c1\"># Construct an email message. (Passing None as the third argument</span>\n<span class=\"c1\"># uses settings.DEFAULT_FROM_EMAIL as the &quot;From:&quot; address.)</span>\n<span class=\"n\">email1</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to1@example.com&quot;</span><span class=\"p\">])</span>\n<span class=\"c1\"># Send the email. The connection was already open, so send_messages()</span>\n<span class=\"c1\"># leaves it open after sending.</span>\n<span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">])</span>\n\n<span class=\"c1\"># Construct and send two more messages. The connection is still open.</span>\n<span class=\"n\">email2</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to2@example.com&quot;</span><span class=\"p\">])</span>\n<span class=\"n\">email3</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to3@example.com&quot;</span><span class=\"p\">])</span>\n<span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email2</span><span class=\"p\">,</span> <span class=\"n\">email3</span><span class=\"p\">])</span>\n\n<span class=\"c1\"># Because we opened it, we need to manually close the connection.</span>\n<span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">close</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>When you manually open a backend’s connection, you are responsible for ensuring\nit gets closed. The example above actually has a bug: if an exception occurs\nwhile sending the messages, the connection will not be closed. This can be\nfixed with a <code class=\"docutils literal notranslate\"><span class=\"pre\">try</span></code>-<code class=\"docutils literal notranslate\"><span class=\"pre\">finally</span></code> statement, but using the backend instance as a\ncontext manager is preferable, as it automatically calls <code class=\"docutils literal notranslate\"><span class=\"pre\">open()</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> as needed.</p>\n<p>This is equivalent to the previous example, but uses the backend as a\n<a class=\"reference external\" href=\"https://docs.python.org/3/reference/datamodel.html#context-managers\" title=\"(w Python v3.14)\"><span class=\"xref std std-ref\">context manager</span></a> to avoid leaving the connection open\non errors:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">mail</span>\n\n<span class=\"c1\"># Use mail.mailers[...] as a context manager.</span>\n<span class=\"k\">with</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">mailers</span><span class=\"p\">[</span><span class=\"s2\">&quot;notifications&quot;</span><span class=\"p\">]</span> <span class=\"k\">as</span> <span class=\"n\">backend</span><span class=\"p\">:</span>\n    <span class=\"c1\"># The backend connection is automatically opened inside the context.</span>\n    <span class=\"n\">email1</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to1@example.com&quot;</span><span class=\"p\">])</span>\n    <span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">])</span>\n\n    <span class=\"c1\"># The connection is still open, and is reused for the second send.</span>\n    <span class=\"n\">email2</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to2@example.com&quot;</span><span class=\"p\">])</span>\n    <span class=\"n\">email3</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">EmailMessage</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hi&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;Message&quot;</span><span class=\"p\">,</span> <span class=\"kc\">None</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;to3@example.com&quot;</span><span class=\"p\">])</span>\n    <span class=\"n\">backend</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email2</span><span class=\"p\">,</span> <span class=\"n\">email3</span><span class=\"p\">])</span>\n\n<span class=\"c1\"># After exiting the context (either normally or because of an error),</span>\n<span class=\"c1\"># the backend connection is automatically closed.</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"email-backends\">\n<span id=\"topic-email-backends\"></span><h2>Email backends<a class=\"heading-anchor\" href=\"#email-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The actual sending of an email is handled by the email backend.</p>\n<p>Django comes with several email backends. With the exception of the SMTP\nbackend, these are mainly useful during testing and development. If the\nbuilt-in backends don’t meet your needs there are <a class=\"reference internal\" href=\"#topic-third-party-email-backends\"><span class=\"std std-ref\">third-party packages</span></a> available. You can also subclass one of the\nbuilt-in backends to change its behavior, or even <a class=\"reference internal\" href=\"#topic-custom-email-backend\"><span class=\"std std-ref\">write your own email\nbackend</span></a>.</p>\n<section id=\"smtp-backend\">\n<span id=\"topic-email-smtp-backend\"></span><h3>SMTP backend<a class=\"heading-anchor\" href=\"#smtp-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The SMTP email backend connects to an SMTP server to send email. To use it, set\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span></code>.</p>\n<p>The SMTP backend supports these <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;host&quot;</span></code> (required): the SMTP server hostname or IP address.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;port&quot;</span></code>: the port number to connect to on the SMTP host. If omitted,\nuses the standard port for the connection protocol depending on the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_tls&quot;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_ssl&quot;</span></code> options: <code class=\"docutils literal notranslate\"><span class=\"pre\">587</span></code> for TLS, <code class=\"docutils literal notranslate\"><span class=\"pre\">465</span></code> for SSL,\nor <code class=\"docutils literal notranslate\"><span class=\"pre\">25</span></code> for an unsecured connection.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;username&quot;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;password&quot;</span></code>: set these if your server requires SMTP\nauthentication („SMTP AUTH” credentials, sometimes called SMTP login).</p>\n<p>Although the username is often an email address, it should not be confused\nwith default „From:” addresses. Those are defined by the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEFAULT_FROM_EMAIL</span></code></a> and <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-SERVER_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SERVER_EMAIL</span></code></a> settings.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_tls&quot;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_ssl&quot;</span></code>: set one of these options to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> to\nconnect to the SMTP server using a secure protocol – <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_tls&quot;</span></code> for\nexplicit TLS or <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_ssl&quot;</span></code> for SSL (implicit TLS).</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;ssl_certfile&quot;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;ssl_keyfile&quot;</span></code>: if the SMTP server’s SSL/TLS\nconnection requires client certificate authentication, use these options to\nspecify the paths to a PEM-formatted certificate chain file and private key\nfile. (The key file can be omitted if the certificate file includes the\nprivate key.)</p>\n<p>These options are not intended for use with a private certificate authority\nor self-signed SMTP server certificate. See\n<a class=\"reference internal\" href=\"#topic-email-smtp-private-ca\"><span class=\"std std-ref\">Private and self-signed SMTP server certificates</span></a> below.</p>\n<p>Note that these options don’t result in checking certificate validity. They\nare passed to the underlying SSL connection. Refer to the documentation of\nPython’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/ssl.html#ssl.SSLContext.wrap_socket\" title=\"(w Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">ssl.SSLContext.wrap_socket()</span></code></a> method for details on how the\ncertificate chain file and private key file are handled.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;timeout&quot;</span></code>: the timeout (in seconds) for connecting to the SMTP server and\nother blocking operations. If not specified, the value is obtained from\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/socket.html#socket.getdefaulttimeout\" title=\"(w Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">socket.getdefaulttimeout()</span></code></a>, which defaults to no timeout (<code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>)\nmeaning SMTP operations can block indefinitely.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fail_silently&quot;</span></code>: set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> to ignore certain errors while sending\na message. All <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#OSError\" title=\"(w Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">OSError</span></code></a>s are ignored while opening the SMTP\nconnection, and <a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#smtplib.SMTPException\" title=\"(w Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">smtplib.SMTPException</span></code></a> errors are ignored while\ncommunicating with the server. This will suppress both transient network\nglitches and also serious configuration problems. However, it does not ignore\n<em>all</em> errors, and problems with serializing the message will not fail\nsilently. (This option is available for backward compatibility but is not\nrecommended for typical use.)</p></li>\n</ul>\n<p>Przykład:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;smtp.example.net&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;use_tls&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;my-app&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"n\">os</span><span class=\"o\">.</span><span class=\"n\">environ</span><span class=\"p\">[</span><span class=\"s2\">&quot;MY_APP_SMTP_PASSWORD&quot;</span><span class=\"p\">],</span>\n            <span class=\"s2\">&quot;timeout&quot;</span><span class=\"p\">:</span> <span class=\"mi\">10</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>When the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting is not defined, Django uses the SMTP\nbackend as the default mailer (the default <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a>),\nconnecting to localhost on port 25. This behavior will be removed in Django\n7.0, which will not have a default mailer configuration.</p>\n<p>When the SMTP backend is used without <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> defined,\nthe options listed above are obtained from the deprecated\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code></a>, <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_PORT</span></code></a>, <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_USER</span></code></a>,\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_PASSWORD</span></code></a>, <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_USE_TLS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_TLS</span></code></a>,\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_USE_SSL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_SSL</span></code></a>, <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_SSL_KEYFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_KEYFILE</span></code></a>,\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_SSL_CERTFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_CERTFILE</span></code></a>, and <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_TIMEOUT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_TIMEOUT</span></code></a> settings,\nrespectively. (There is no setting equivalent to the <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fail_silently&quot;</span></code>\noption.)</p>\n</aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.backends.smtp.EmailBackend\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-prename descclassname\"><span class=\"pre\">backends.smtp.</span></span><span class=\"sig-name descname\"><span class=\"pre\">EmailBackend</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.backends.smtp.EmailBackend\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Directly instantiating an <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailBackend</span></code> class is not recommended. Use\n<a class=\"reference internal\" href=\"#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mailers</span></code></a> to obtain a backend instance.</p>\n<p>When constructed directly (without going through <a class=\"reference internal\" href=\"#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mailers</span></code></a>), the SMTP\n<code class=\"docutils literal notranslate\"><span class=\"pre\">EmailBackend</span></code> class accepts the options listed above as keyword\narguments. Default values come from the corresponding, deprecated\n<code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_*</span></code> settings. <code class=\"docutils literal notranslate\"><span class=\"pre\">host</span></code> is not required and defaults to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;localhost&quot;</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">port</span></code> defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">25</span></code> even if <code class=\"docutils literal notranslate\"><span class=\"pre\">use_tls</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">use_ssl</span></code> is True.</p>\n<p>When the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting is defined, attempting to directly\ncreate an SMTP <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailBackend</span></code> will raise an <code class=\"docutils literal notranslate\"><span class=\"pre\">AttributeError</span></code>.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>Directly constructing an instance of an <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailBackend</span></code> class will be\nunsupported in Django 7.0. Undocumented use will result in different\ndefault argument handling compared to earlier releases.</p>\n</aside>\n</dd></dl>\n\n<section id=\"private-and-self-signed-smtp-server-certificates\">\n<span id=\"topic-email-smtp-private-ca\"></span><h4>Private and self-signed SMTP server certificates<a class=\"heading-anchor\" href=\"#private-and-self-signed-smtp-server-certificates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>If the SMTP server uses an SSL certificate from a private certificate authority\n(CA), the CA’s root certificate should be added to the system CA bundle on the\nclient (where Django is running). Likewise, if the server uses a self-signed\ncertificate, it should be added to the client’s system CA bundle so it can be\ntrusted. (The SMTP backend’s <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;ssl_certfile&quot;</span></code> option cannot be used for CA\nroots or self-signed certificates.)</p>\n<p>Follow platform-specific instructions for adding to the system CA bundle. If\nmodifying the system bundle is not possible or desired, an alternative is using\nOpenSSL’s <code class=\"docutils literal notranslate\"><span class=\"pre\">SSL_CERT_FILE</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">SSL_CERT_DIR</span></code> environment variables to\nspecify a custom certificate bundle.</p>\n<p>For more complex scenarios, the SMTP backend can be subclassed to add root\ncertificates to its <code class=\"docutils literal notranslate\"><span class=\"pre\">ssl_context</span></code> using\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations\" title=\"(w Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">ssl.SSLContext.load_verify_locations()</span></code></a>.</p>\n</section>\n</section>\n<section id=\"console-backend\">\n<span id=\"topic-email-console-backend\"></span><h3>Console backend<a class=\"heading-anchor\" href=\"#console-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Instead of sending out real emails, the console backend writes the emails that\nwould be sent to the standard output. To use it, set <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.console.EmailBackend&quot;</span></code>.</p>\n<p>The console backend supports these <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;stream&quot;</span></code>: a stream-like object to write to. Defaults to <code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fail_silently&quot;</span></code>: set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> to ignore <em>all</em> errors while writing the\nmessage to the stream, including errors serializing the message. (This option\nis available for backward compatibility but is not recommended.)</p></li>\n</ul>\n<p>This backend is not intended for use in production – it is provided as a\nconvenience that can be used during development.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The settings file created by <a class=\"reference internal\" href=\"/pl/6.1/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> now defines\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> with the console backend as the default configuration.</p>\n</aside>\n</section>\n<section id=\"file-backend\">\n<span id=\"topic-email-file-backend\"></span><h3>File backend<a class=\"heading-anchor\" href=\"#file-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The file backend writes emails to a file. A new file is created for each new\nsession that is opened on this backend. To use it, set <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.filebased.EmailBackend&quot;</span></code>.</p>\n<p>The file backend supports these <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;file_path&quot;</span></code> (required): the directory to which the files are written. Can\nbe a string or a <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.Path\" title=\"(w Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">pathlib.Path</span></code></a> object. If the directory does not\nexist, the file backend will attempt to create it.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fail_silently&quot;</span></code>: set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> to ignore <em>all</em> errors while writing the\nmessage to the file – including errors serializing the message – but <em>not</em>\nerrors related to ensuring the file path directory exists. (This option is\navailable for backward compatibility but is not recommended.)</p></li>\n</ul>\n<p>This backend is not intended for use in production – it is provided as a\nconvenience that can be used during development.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>When the file backend is used without the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting\ndefined, it will get its <code class=\"docutils literal notranslate\"><span class=\"pre\">file_path</span></code> option from the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_FILE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_FILE_PATH</span></code></a> setting.</p>\n</aside>\n</section>\n<section id=\"in-memory-backend\">\n<span id=\"topic-email-memory-backend\"></span><h3>In-memory backend<a class=\"heading-anchor\" href=\"#in-memory-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">'locmem'</span></code> backend stores messages in a special attribute of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code> module. The <code class=\"docutils literal notranslate\"><span class=\"pre\">outbox</span></code> attribute is created when the first\nmessage is sent. It’s a list with an <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> instance for each\nmessage that would be sent. Messages in the outbox are annotated with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">sent_using</span></code> attribute that identifies the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> alias used to\nsend the message.</p>\n<p>To use the in-memory backend, set <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.locmem.EmailBackend&quot;</span></code>. It does not support any\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>.</p>\n<p>Django’s test runner <a class=\"reference internal\" href=\"/pl/6.1/topics/testing/tools/#topics-testing-email\"><span class=\"std std-ref\">automatically switches to this backend for testing</span></a>.</p>\n<p>This backend is not intended for use in production – it is provided as a\nconvenience that can be used during development and testing.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.1\">\n<p class=\"version-note-title\">Changed in Django 6.1</p><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">sent_using</span></code> attribute was added to messages in the outbox.</p>\n</aside>\n</section>\n<section id=\"dummy-backend\">\n<span id=\"topic-email-dummy-backend\"></span><h3>Dummy backend<a class=\"heading-anchor\" href=\"#dummy-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>As the name suggests the dummy backend does nothing with your messages. To use\nit, set <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.dummy.EmailBackend&quot;</span></code>. It does not support any\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>.</p>\n<p>This backend is not intended for use in production – it is provided as a\nconvenience that can be used during development.</p>\n</section>\n<section id=\"third-party-backends\">\n<span id=\"topic-third-party-email-backends\"></span><h3>Third-party backends<a class=\"heading-anchor\" href=\"#third-party-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition-there-are-community-maintained-solutions admonition\">\n<p class=\"admonition-title\">There are community-maintained solutions!</p>\n<p>Django has a vibrant ecosystem. There are email backends\nhighlighted on the <a class=\"reference external\" href=\"https://www.djangoproject.com/community/ecosystem/#email-notifications\">Community Ecosystem</a> page. The Django Packages\n<a class=\"reference external\" href=\"https://djangopackages.org/grids/g/email/\">Email grid</a> has even more options for you!</p>\n</aside>\n<p>Third-party email backends are available that:</p>\n<ul class=\"simple\">\n<li><p>Integrate directly with commercial email service providers» APIs (which often\nhave extra functionality not available through SMTP).</p></li>\n<li><p>Offload email sending to asynchronous task queues.</p></li>\n<li><p>Add features to other email backends, such as enforcing do-not-send lists or\nlogging sent messages.</p></li>\n<li><p>Provide development and debugging tools, such as sandbox capture and\nin-browser email previews.</p></li>\n</ul>\n</section>\n<section id=\"defining-a-custom-email-backend\">\n<span id=\"topic-custom-email-backend\"></span><h3>Defining a custom email backend<a class=\"heading-anchor\" href=\"#defining-a-custom-email-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you need to change how emails are sent you can write your own email backend.\nTo use a custom backend, set <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">BACKEND</span></code></a> to the Python\nimport path for your backend class and <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a> to\nany <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code> keyword arguments your backend supports.</p>\n<p>Custom email backends should subclass <code class=\"docutils literal notranslate\"><span class=\"pre\">BaseEmailBackend</span></code> that is located in\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.backends.base</span></code> module. A custom email backend must\nimplement the <code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages(email_messages)</span></code> method. This method receives a\nlist of <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a> instances and returns the number of successfully\ndelivered messages. If your backend has any concept of a persistent session or\nconnection, you should also implement the <code class=\"docutils literal notranslate\"><span class=\"pre\">open()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> methods.\nRefer to <code class=\"docutils literal notranslate\"><span class=\"pre\">smtp.EmailBackend</span></code> for a reference implementation.</p>\n</section>\n<section id=\"obtaining-an-instance-of-an-email-backend\">\n<span id=\"topic-mailers\"></span><h3>Obtaining an instance of an email backend<a class=\"heading-anchor\" href=\"#obtaining-an-instance-of-an-email-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">mailers</span></code> factory in <code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.core.mail</span></code> returns instances of email\nbackends.</p>\n<dl class=\"py data\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mailers\">\n<span class=\"sig-name descname\"><span class=\"pre\">mailers</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.mailers\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"6.1\">\n<p class=\"version-note-title\">New in Django 6.1</p></aside>\n<p>You can access the mailers configured in the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting\nthrough a dict-like object: <code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.mailers</span></code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">mailers</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">mailers</span><span class=\"p\">[</span><span class=\"s2\">&quot;notifications&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>If the named key is not defined, a <code class=\"docutils literal notranslate\"><span class=\"pre\">MailerDoesNotExist</span></code> error will be\nraised. Other configuration problems will raise an <code class=\"docutils literal notranslate\"><span class=\"pre\">InvalidMailer</span></code> error.</p>\n</dd></dl>\n\n<dl class=\"py data\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mailers.default\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">mailers.</span></span><span class=\"sig-name descname\"><span class=\"pre\">default</span></span><a class=\"heading-anchor\" href=\"#django.core.mail.mailers.default\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><aside class=\"version-note version-added\" data-version=\"6.1\">\n<p class=\"version-note-title\">New in Django 6.1</p></aside>\n<p>As a shortcut, the default mailer can be accessed through\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.mail.mailers.default</span></code>:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.mail</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">mailers</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">mailers</span><span class=\"o\">.</span><span class=\"n\">default</span>\n</code></pre></div>\n<p>This is equivalent to <code class=\"docutils literal notranslate\"><span class=\"pre\">mailers[&quot;default&quot;]</span></code>. If no default mailer has\nbeen configured, a <code class=\"docutils literal notranslate\"><span class=\"pre\">MailerDoesNotExist</span></code> error will be raised.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span>If the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting is not defined, <code class=\"docutils literal notranslate\"><span class=\"pre\">mailers.default</span></code>\nwill create an email backend instance from the deprecated\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a> and related settings. This supports backward\ncompatibility with Django 6.0 and earlier.</p>\n<p>This behavior (and those settings) will be removed in Django 7.0.</p>\n</aside>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.get_connection\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_connection</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">backend</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\"><span class=\"pre\">*</span></abbr></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">fail_silently</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em>, <em class=\"sig-param\"><span class=\"o\"><span class=\"pre\">**</span></span><span class=\"n\"><span class=\"pre\">kwargs</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.mail.get_connection\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>The deprecated <code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.core.mail.get_connection()</span></code> function creates\nand returns an instance of an email backend. Its behavior depends on the\n<a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting and how the function is called.</p>\n<p>If the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting <em>is</em> defined:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> with no arguments will return\n<a class=\"reference internal\" href=\"#django.core.mail.mailers.default\" title=\"django.core.mail.mailers.default\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mailers.default</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection(...)</span></code> called with only <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> or other\nkeyword arguments will create an instance of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">MAILERS[&quot;default&quot;]</span></code> with any keywords added to the\ndefault mailer’s <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection(backend,</span> <span class=\"pre\">...)</span></code> with a backend import path will raise\nan error.</p></li>\n</ul>\n<p>If the <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting is <em>not</em> defined:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> with no arguments will return an instance of the\nemail backend specified in <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a>.</p></li>\n<li><p>If you specify the <code class=\"docutils literal notranslate\"><span class=\"pre\">backend</span></code> argument, an instance of that backend will\nbe instantiated.</p></li>\n<li><p>If the keyword argument <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> is True, certain\nbackend-dependent exceptions during the email sending process will be\nsilently ignored.</p></li>\n<li><p>All other keyword arguments are passed directly to the constructor of the\nemail backend.</p></li>\n</ul>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.0: </span>Passing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> as a positional argument is deprecated.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Niezalecane od wersji 6.1: </span><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> is deprecated and will be removed in Django\n7.0. Switch to <a class=\"reference internal\" href=\"#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mailers[alias]</span></code></a>. See\n<a class=\"reference internal\" href=\"/pl/6.1/howto/mailers-migration/#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a> for migration suggestions.</p>\n</aside>\n</dd></dl>\n\n</section>\n<section id=\"email-backend-api\">\n<span id=\"topic-email-backend-api\"></span><h3>Email backend API<a class=\"heading-anchor\" href=\"#email-backend-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Instances of an email backend class have the following methods:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">open()</span></code> instantiates a long-lived email-sending connection.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> closes the current email-sending connection.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">send_messages(email_messages)</span></code> sends a list of <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">EmailMessage</span></code></a>\nobjects. If the connection is not open, this call will implicitly open the\nconnection, and close the connection afterward. If the connection is already\nopen, it will be left open after mail has been sent.</p></li>\n</ul>\n<p>A backend instance can also be used as a context manager, which will\nautomatically call <code class=\"docutils literal notranslate\"><span class=\"pre\">open()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">close()</span></code> as needed. An example is in\n<a class=\"reference internal\" href=\"#topics-sending-multiple-emails\"><span class=\"std std-ref\">Sending many messages efficiently</span></a>.</p>\n</section>\n</section>\n<section id=\"configuring-email-for-development\">\n<h2>Configuring email for development<a class=\"heading-anchor\" href=\"#configuring-email-for-development\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>There are times when you do not want Django to send emails at\nall. For example, while developing a website, you probably don’t want\nto send out thousands of emails – but you may want to validate that\nemails will be sent to the right people under the right conditions,\nand that those emails will contain the correct content.</p>\n<p>The easiest way to configure email for local development is to use the\n<a class=\"reference internal\" href=\"#topic-email-console-backend\"><span class=\"std std-ref\">console</span></a> email backend. This backend\nredirects all email to <code class=\"docutils literal notranslate\"><span class=\"pre\">stdout</span></code>, allowing you to inspect the content of mail.</p>\n<p>The <a class=\"reference internal\" href=\"#topic-email-file-backend\"><span class=\"std std-ref\">file</span></a> email backend can also be useful\nduring development – this backend dumps the contents of every SMTP connection\nto a file that can be inspected at your leisure.</p>\n<p>Another approach is to use a mocked SMTP server that receives the emails\nlocally and displays them to the terminal, but does not actually send\nanything. The <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/aiosmtpd/\">aiosmtpd</a> package provides a way to accomplish this:</p>\n<div class=\"code-block\" data-language=\"shell\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span><span class=\"s2\">&quot;aiosmtpd &gt;= 1.4.5&quot;</span>\n\npython<span class=\"w\"> </span>-m<span class=\"w\"> </span>aiosmtpd<span class=\"w\"> </span>-n<span class=\"w\"> </span>-l<span class=\"w\"> </span>localhost:8025\n</code></pre></div>\n<p>This command will start a minimal SMTP server listening on port 8025 of\nlocalhost. This server prints to standard output all email headers and the\nemail body. You then only need to set an SMTP backend’s <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;host&quot;</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;port&quot;</span></code> <a class=\"reference internal\" href=\"/pl/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">OPTIONS</span></code></a> accordingly. For a more\ndetailed discussion of SMTP server options, see the documentation of the\n<a class=\"reference external\" href=\"https://aiosmtpd.readthedocs.io/en/latest/\">aiosmtpd</a> module.</p>\n<p>For information about unit-testing the sending of emails in your application,\nsee the <a class=\"reference internal\" href=\"/pl/6.1/topics/testing/tools/#topics-testing-email\"><span class=\"std std-ref\">Email services</span></a> section of the testing documentation.</p>\n</section>","rootId":"module-django.core.mail","toc":[{"title":"Quick examples","anchor":"quick-examples","children":[]},{"title":"Configuring email","anchor":"configuring-email","children":[{"title":"Multiple mailers","anchor":"multiple-mailers","children":[]}]},{"title":"Sending messages","anchor":"sending-messages","children":[{"title":"send_mail()","anchor":"send-mail","children":[]},{"title":"send_mass_mail()","anchor":"send-mass-mail","children":[{"title":"send_mass_mail() vs. send_mail()","anchor":"send-mass-mail-vs-send-mail","children":[]}]},{"title":"mail_admins()","anchor":"mail-admins","children":[]},{"title":"mail_managers()","anchor":"mail-managers","children":[]},{"title":"The EmailMessage class","anchor":"the-emailmessage-class","children":[{"title":"Sending alternative content types","anchor":"sending-alternative-content-types","children":[{"title":"Sending multiple content versions","anchor":"sending-multiple-content-versions","children":[]},{"title":"Updating the default content type","anchor":"updating-the-default-content-type","children":[]}]}]},{"title":"Safely sending email","anchor":"safely-sending-email","children":[{"title":"Formatting email addresses","anchor":"formatting-email-addresses","children":[]},{"title":"Preventing header injection","anchor":"preventing-header-injection","children":[]}]},{"title":"Sending many messages efficiently","anchor":"sending-many-messages-efficiently","children":[]}]},{"title":"Email backends","anchor":"email-backends","children":[{"title":"SMTP backend","anchor":"smtp-backend","children":[{"title":"Private and self-signed SMTP server certificates","anchor":"private-and-self-signed-smtp-server-certificates","children":[]}]},{"title":"Console backend","anchor":"console-backend","children":[]},{"title":"File backend","anchor":"file-backend","children":[]},{"title":"In-memory backend","anchor":"in-memory-backend","children":[]},{"title":"Dummy backend","anchor":"dummy-backend","children":[]},{"title":"Third-party backends","anchor":"third-party-backends","children":[]},{"title":"Defining a custom email backend","anchor":"defining-a-custom-email-backend","children":[]},{"title":"Obtaining an instance of an email backend","anchor":"obtaining-an-instance-of-an-email-backend","children":[]},{"title":"Email backend API","anchor":"email-backend-api","children":[]}]},{"title":"Configuring email for development","anchor":"configuring-email-for-development","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Używanie Django","url":"/pl/6.1/topics/"}],"prev":{"docname":"topics/signing","title":"Cryptographic signing","url":"/pl/6.1/topics/signing/"},"next":{"docname":"topics/i18n/index","title":"Internacjonalizacja i lokalizacja","url":"/pl/6.1/topics/i18n/"},"formats":{"html":"/pl/6.1/topics/email/","markdown":"/pl/6.1/topics/email.md","json":"/pl/6.1/topics/email.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/email.txt","official":"https://docs.djangoproject.com/pl/6.1/topics/email/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}