{"title":"Sending email","version":"6.0","locale":"el","docname":"topics/email","url":"/el/6.0/topics/email/","canonical":"https://djangodocs.dev/el/6.0/topics/email/","summary":"Although Python provides a mail sending interface via the smtplib module, Django provides a couple of light wrappers over it.","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>Although Python provides a mail sending interface via the <a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#module-smtplib\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">smtplib</code></a>\nmodule, Django provides a couple of light wrappers over it. These wrappers are\nprovided to make sending email extra quick, to help test email sending during\ndevelopment, and to provide support for platforms that can’t use SMTP.</p>\n<p>The code lives in the <code class=\"docutils literal notranslate\">django.core.mail</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\">send_mail()</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=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> send_mail\n\nsend_mail<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    fail_silently<span class=\"o\">=</span><span class=\"kc\">False</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<p>When additional email sending functionality is needed, use\n<a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\">EmailMessage</code></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\">EmailMultiAlternatives</code></a>. For example, to send\na multipart email that includes both HTML and plain text versions with a\nspecific template and custom headers, you can use the following 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=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> EmailMultiAlternatives\n<span class=\"kn\">from</span> <span class=\"nn\">django.template.loader</span> <span class=\"kn\">import</span> render_to_string\n\n<span class=\"c1\"># First, render the plain text content.</span>\ntext_content <span class=\"o\">=</span> render_to_string<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;templates/emails/my_email.txt&quot;</span><span class=\"p\">,</span>\n    context<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>\nhtml_content <span class=\"o\">=</span> render_to_string<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;templates/emails/my_email.html&quot;</span><span class=\"p\">,</span>\n    context<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>\nmsg <span class=\"o\">=</span> EmailMultiAlternatives<span class=\"p\">(</span>\n    subject<span class=\"o\">=</span><span class=\"s2\">&quot;Subject here&quot;</span><span class=\"p\">,</span>\n    body<span class=\"o\">=</span>text_content<span class=\"p\">,</span>\n    from_email<span class=\"o\">=</span><span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    to<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;to@example.com&quot;</span><span class=\"p\">],</span>\n    headers<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>\nmsg<span class=\"o\">.</span>attach_alternative<span class=\"p\">(</span>html_content<span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\nmsg<span class=\"o\">.</span>send<span class=\"p\">()</span>\n</code></pre></div>\n<p>Mail is sent using the SMTP host and port specified in the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST</code></a> and <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_PORT</code></a> settings. The\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_USER</code></a> and <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_PASSWORD</code></a> settings, if\nset, are used to authenticate to the SMTP server, and the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_USE_TLS\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_USE_TLS</code></a> and <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_USE_SSL\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_USE_SSL</code></a> settings control whether\na secure connection is used.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</p>\n<p>The character set of email sent with <code class=\"docutils literal notranslate\">django.core.mail</code> will be set to\nthe value of your <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-DEFAULT_CHARSET\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_CHARSET</code></a> setting.</p>\n</aside>\n</section>\n<section id=\"send-mail\">\n<h2><code class=\"docutils literal notranslate\">send_mail()</code><a class=\"heading-anchor\" href=\"#send-mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.send_mail\">\n<span class=\"sig-name descname\">send_mail</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">subject</em>, <em class=\"sig-param\">message</em>, <em class=\"sig-param\">from_email</em>, <em class=\"sig-param\">recipient_list</em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">auth_user<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">auth_password<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">connection<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">html_message<span class=\"o\">=</span><span class=\"default_value\">None</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>In most cases, you can send email using <code class=\"docutils literal notranslate\">django.core.mail.send_mail()</code>.</p>\n<p>The <code class=\"docutils literal notranslate\">subject</code>, <code class=\"docutils literal notranslate\">message</code>, <code class=\"docutils literal notranslate\">from_email</code> and <code class=\"docutils literal notranslate\">recipient_list</code> parameters\nare required.</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">subject</code>: A string.</p></li>\n<li><p><code class=\"docutils literal notranslate\">message</code>: A string.</p></li>\n<li><p><code class=\"docutils literal notranslate\">from_email</code>: A string. If <code class=\"docutils literal notranslate\">None</code>, Django will use the value of the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_FROM_EMAIL</code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\">recipient_list</code>: A list of strings, each an email address. Each\nmember of <code class=\"docutils literal notranslate\">recipient_list</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\">fail_silently</code>: A boolean. When it’s <code class=\"docutils literal notranslate\">False</code>, <code class=\"docutils literal notranslate\">send_mail()</code> will raise\nan <a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#smtplib.SMTPException\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">smtplib.SMTPException</code></a> if an error occurs. See the <a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#module-smtplib\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">smtplib</code></a>\ndocs for a list of possible exceptions, all of which are subclasses of\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#smtplib.SMTPException\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">SMTPException</code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">auth_user</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=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_USER</code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\">auth_password</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=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_PASSWORD</code></a> setting.</p></li>\n<li><p><code class=\"docutils literal notranslate\">connection</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\">html_message</code>: If <code class=\"docutils literal notranslate\">html_message</code> is provided, the resulting email will\nbe a <em class=\"mimetype\">multipart/alternative</em> email with <code class=\"docutils literal notranslate\">message</code> as the\n<em class=\"mimetype\">text/plain</em> content type and <code class=\"docutils literal notranslate\">html_message</code> as the\n<em class=\"mimetype\">text/html</em> content type.</p></li>\n</ul>\n<p>The return value will be the number of successfully delivered messages (which\ncan be <code class=\"docutils literal notranslate\">0</code> or <code class=\"docutils literal notranslate\">1</code> since it can only send one message).</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing <code class=\"docutils literal notranslate\">fail_silently</code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n</section>\n<section id=\"send-mass-mail\">\n<h2><code class=\"docutils literal notranslate\">send_mass_mail()</code><a class=\"heading-anchor\" href=\"#send-mass-mail\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\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\">send_mass_mail</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">datatuple</em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">auth_user<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">auth_password<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">connection<span class=\"o\">=</span><span class=\"default_value\">None</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\">django.core.mail.send_mass_mail()</code> is intended to handle mass emailing.</p>\n<p><code class=\"docutils literal notranslate\">datatuple</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>subject<span class=\"p\">,</span> message<span class=\"p\">,</span> from_email<span class=\"p\">,</span> recipient_list<span class=\"p\">)</span>\n</code></pre></div>\n<p><code class=\"docutils literal notranslate\">fail_silently</code>, <code class=\"docutils literal notranslate\">auth_user</code>, <code class=\"docutils literal notranslate\">auth_password</code> and <code class=\"docutils literal notranslate\">connection</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\">send_mail()</code></a>. They must be given as keyword arguments\nif used.</p>\n<p>Each separate element of <code class=\"docutils literal notranslate\">datatuple</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\">send_mail()</code></a>, recipients in the same <code class=\"docutils literal notranslate\">recipient_list</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>message1 <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>\nmessage2 <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>\nsend_mass_mail<span class=\"p\">((</span>message1<span class=\"p\">,</span> message2<span class=\"p\">),</span> fail_silently<span class=\"o\">=</span><span class=\"kc\">False</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\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing <code class=\"docutils literal notranslate\">fail_silently</code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<section id=\"send-mass-mail-vs-send-mail\">\n<h3><code class=\"docutils literal notranslate\">send_mass_mail()</code> vs. <code class=\"docutils literal notranslate\">send_mail()</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></h3>\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\">send_mass_mail()</code></a> and <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\">send_mail()</code></a> is\nthat <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\">send_mail()</code></a> opens a connection to the mail server each time it’s\nexecuted, 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\">send_mass_mail()</code></a> uses a single connection for all of its\nmessages. 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\">send_mass_mail()</code></a> slightly more efficient.</p>\n</section>\n</section>\n<section id=\"mail-admins\">\n<h2><code class=\"docutils literal notranslate\">mail_admins()</code><a class=\"heading-anchor\" href=\"#mail-admins\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mail_admins\">\n<span class=\"sig-name descname\">mail_admins</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">subject</em>, <em class=\"sig-param\">message</em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">connection<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">html_message<span class=\"o\">=</span><span class=\"default_value\">None</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\">django.core.mail.mail_admins()</code> is a shortcut for sending an email to the\nsite admins, as defined in the <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-ADMINS\"><code class=\"xref std std-setting docutils literal notranslate\">ADMINS</code></a> setting.</p>\n<p><code class=\"docutils literal notranslate\">mail_admins()</code> prefixes the subject with the value of the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_SUBJECT_PREFIX\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_SUBJECT_PREFIX</code></a> setting, which is <code class=\"docutils literal notranslate\">&quot;[Django] &quot;</code> by default.</p>\n<p>The «From:» header of the email will be the value of the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-SERVER_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\">SERVER_EMAIL</code></a> setting.</p>\n<p>This method exists for convenience and readability.</p>\n<p>If <code class=\"docutils literal notranslate\">html_message</code> is provided, the resulting email will be a\n<em class=\"mimetype\">multipart/alternative</em> email with <code class=\"docutils literal notranslate\">message</code> as the\n<em class=\"mimetype\">text/plain</em> content type and <code class=\"docutils literal notranslate\">html_message</code> as the\n<em class=\"mimetype\">text/html</em> content type.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing <code class=\"docutils literal notranslate\">fail_silently</code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n</section>\n<section id=\"mail-managers\">\n<h2><code class=\"docutils literal notranslate\">mail_managers()</code><a class=\"heading-anchor\" href=\"#mail-managers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.mail_managers\">\n<span class=\"sig-name descname\">mail_managers</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">subject</em>, <em class=\"sig-param\">message</em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">connection<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">html_message<span class=\"o\">=</span><span class=\"default_value\">None</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\">django.core.mail.mail_managers()</code> is just like <code class=\"docutils literal notranslate\">mail_admins()</code>, except it\nsends an email to the site managers, as defined in the <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-MANAGERS\"><code class=\"xref std std-setting docutils literal notranslate\">MANAGERS</code></a>\nsetting.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing <code class=\"docutils literal notranslate\">fail_silently</code> and later parameters as positional arguments is\ndeprecated.</p>\n</aside>\n</section>\n<section id=\"examples\">\n<h2>Examples<a class=\"heading-anchor\" href=\"#examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>This sends a single email to <a class=\"reference external\" href=\"mailto:john&#37;&#52;&#48;example&#46;com\">john<span>&#64;</span>example<span>&#46;</span>com</a> and <a class=\"reference external\" href=\"mailto:jane&#37;&#52;&#48;example&#46;com\">jane<span>&#64;</span>example<span>&#46;</span>com</a>, with them\nboth appearing in the «To:»:</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>send_mail<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>This sends a message to <code class=\"docutils literal notranslate\">john&#64;example.com</code> and <code class=\"docutils literal notranslate\">jane&#64;example.com</code>, with\nthem both receiving a separate 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>datatuple <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>\nsend_mass_mail<span class=\"p\">(</span>datatuple<span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"preventing-header-injection\">\n<h2>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></h2>\n<p><a class=\"reference external\" href=\"http://www.nyphp.org/phundamentals/8_Preventing-Email-Header-Injection.html\">Header injection</a> is a security exploit in which an attacker inserts extra\nemail headers to control the «To:» and «From:» in email messages that your\nscripts generate.</p>\n<p>The Django email functions outlined above all protect against header injection\nby forbidding newlines in header values. If any <code class=\"docutils literal notranslate\">subject</code>, <code class=\"docutils literal notranslate\">from_email</code> or\n<code class=\"docutils literal notranslate\">recipient_list</code> contains a newline (in either Unix, Windows or Mac style),\nthe email function (e.g. <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\">send_mail()</code></a>) will raise <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#ValueError\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">ValueError</code></a> and,\nhence, will not send the email. It’s your responsibility to validate all data\nbefore passing it to the email functions.</p>\n<p>If a <code class=\"docutils literal notranslate\">message</code> contains headers at the start of the string, the headers will\nbe printed as the first bit of the email message.</p>\n<p>Here’s an example view that takes a <code class=\"docutils literal notranslate\">subject</code>, <code class=\"docutils literal notranslate\">message</code> and <code class=\"docutils literal notranslate\">from_email</code>\nfrom the request’s POST data, sends that to <code class=\"docutils literal notranslate\">admin&#64;example.com</code> and redirects\nto «/contact/thanks/» when it’s done:</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=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> send_mail\n<span class=\"kn\">from</span> <span class=\"nn\">django.http</span> <span class=\"kn\">import</span> HttpResponse<span class=\"p\">,</span> HttpResponseRedirect\n\n\n<span class=\"k\">def</span> <span class=\"nf\">send_email</span><span class=\"p\">(</span>request<span class=\"p\">):</span>\n    subject <span class=\"o\">=</span> request<span class=\"o\">.</span>POST<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;subject&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">)</span>\n    message <span class=\"o\">=</span> request<span class=\"o\">.</span>POST<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;message&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">)</span>\n    from_email <span class=\"o\">=</span> request<span class=\"o\">.</span>POST<span class=\"o\">.</span>get<span class=\"p\">(</span><span class=\"s2\">&quot;from_email&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">if</span> subject <span class=\"ow\">and</span> message <span class=\"ow\">and</span> from_email<span class=\"p\">:</span>\n        <span class=\"k\">try</span><span class=\"p\">:</span>\n            send_mail<span class=\"p\">(</span>subject<span class=\"p\">,</span> message<span class=\"p\">,</span> from_email<span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"s2\">&quot;admin@example.com&quot;</span><span class=\"p\">])</span>\n        <span class=\"k\">except</span> <span class=\"ne\">ValueError</span><span class=\"p\">:</span>\n            <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Invalid header found.&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">return</span> HttpResponseRedirect<span class=\"p\">(</span><span class=\"s2\">&quot;/contact/thanks/&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"c1\"># In reality we&#39;d use a form class</span>\n        <span class=\"c1\"># to get proper validation errors.</span>\n        <span class=\"k\">return</span> HttpResponse<span class=\"p\">(</span><span class=\"s2\">&quot;Make sure all fields are entered and valid.&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"version-note version-changed\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>Older versions raised <code class=\"docutils literal notranslate\">django.core.mail.BadHeaderError</code> for some\ninvalid headers. This has been replaced with <code class=\"xref py py-exc docutils literal notranslate\">ValueError</code>.</p>\n</aside>\n</section>\n<section id=\"the-emailmessage-class\">\n<span id=\"emailmessage-and-smtpconnection\"></span><h2>The <code class=\"docutils literal notranslate\">EmailMessage</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></h2>\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\">send_mail()</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\">send_mass_mail()</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\">EmailMessage</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\">EmailMessage</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\">send_mail()</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\">EmailMessage</code></a> instances directly.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</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\">send_mail()</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\">EmailMessage</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\">EmailMessage</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\">send()</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<section id=\"emailmessage-objects\">\n<h3><code class=\"docutils literal notranslate\">EmailMessage</code> Objects<a class=\"heading-anchor\" href=\"#emailmessage-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.EmailMessage\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">EmailMessage</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\">EmailMessage</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\">send()</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\">subject</code>: The subject line of the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\">body</code>: The body text. This should be a plain text message.</p></li>\n<li><p><code class=\"docutils literal notranslate\">from_email</code>: The sender’s address. Both <code class=\"docutils literal notranslate\">fred&#64;example.com</code> and\n<code class=\"docutils literal notranslate\">&quot;Fred&quot; &lt;fred&#64;example.com&gt;</code> forms are legal. If omitted, the\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-DEFAULT_FROM_EMAIL\"><code class=\"xref std std-setting docutils literal notranslate\">DEFAULT_FROM_EMAIL</code></a> setting is used.</p></li>\n<li><p><code class=\"docutils literal notranslate\">to</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\">cc</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\">bcc</code>: A list or tuple of addresses used in the «Bcc» header when\nsending the email.</p></li>\n<li><p><code class=\"docutils literal notranslate\">reply_to</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\">attachments</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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</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\">EmailAttachment</code></a>, or a tuple with attributes\n<code class=\"docutils literal notranslate\">(filename, content, mimetype)</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"5.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 5.2</p><p>Support for <a class=\"reference internal\" href=\"#django.core.mail.EmailAttachment\" title=\"django.core.mail.EmailAttachment\"><code class=\"xref py py-class docutils literal notranslate\">EmailAttachment</code></a> items of <code class=\"docutils literal notranslate\">attachments</code> was\nadded.</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"6.0\" role=\"note\">\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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</code></a> objects in the\n<code class=\"docutils literal notranslate\">attachments</code> list was added.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEBase</code></a>\nobjects in <code class=\"docutils literal notranslate\">attachments</code> is deprecated. Use\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</code></a> instead.</p>\n</aside>\n</li>\n<li><p><code class=\"docutils literal notranslate\">headers</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\">extra_headers</code>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">connection</code>: An <a class=\"reference internal\" href=\"#topic-email-backends\"><span class=\"std std-ref\">email backend</span></a> instance.\nUse this parameter if you are sending the <code class=\"xref py py-class docutils literal notranslate\">EmailMessage</code> via\n<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\">send()</code></a> and you want to use the same connection for multiple\nmessages. If omitted, a new connection is created when <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\">send()</code></a> is\ncalled. This 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></li>\n</ul>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing all except the first four parameters as positional arguments is\ndeprecated.</p>\n</aside>\n<p>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=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> EmailMessage\n\nemail <span class=\"o\">=</span> EmailMessage<span class=\"p\">(</span>\n    subject<span class=\"o\">=</span><span class=\"s2\">&quot;Hello&quot;</span><span class=\"p\">,</span>\n    body<span class=\"o\">=</span><span class=\"s2\">&quot;Body goes here&quot;</span><span class=\"p\">,</span>\n    from_email<span class=\"o\">=</span><span class=\"s2\">&quot;from@example.com&quot;</span><span class=\"p\">,</span>\n    to<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    bcc<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;bcc@example.com&quot;</span><span class=\"p\">],</span>\n    reply_to<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;another@example.com&quot;</span><span class=\"p\">],</span>\n    headers<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\">send</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</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. If a connection was specified when the email was\nconstructed, that connection will be used. Otherwise, an instance of\nthe default backend will be instantiated and used. If the keyword\nargument <code class=\"docutils literal notranslate\">fail_silently</code> is <code class=\"docutils literal notranslate\">True</code>, exceptions raised while sending\nthe message will be quashed. An empty list of recipients will not raise\nan exception. It will return <code class=\"docutils literal notranslate\">1</code> if the message was sent\nsuccessfully, otherwise <code class=\"docutils literal notranslate\">0</code>.</p>\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\">message</span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">policy<span class=\"o\">=</span><span class=\"default_value\">email.policy.default</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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">email.message.EmailMessage</code></a>\nobject representing the message to be sent.</p>\n<p>The keyword argument <code class=\"docutils literal notranslate\">policy</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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-mod docutils literal notranslate\">email.policy.Policy</code></a> object. Defaults to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.default\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-data docutils literal notranslate\">email.policy.default</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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-data docutils literal notranslate\">SMTP</code></a>, <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.SMTPUTF8\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-data docutils literal notranslate\">SMTPUTF8</code></a> or a custom\npolicy. For example,\n<a class=\"reference internal\" href=\"#django.core.mail.backends.smtp.EmailBackend\" title=\"django.core.mail.backends.smtp.EmailBackend\"><code class=\"xref py py-class docutils literal notranslate\">django.core.mail.backends.smtp.EmailBackend</code></a> uses the\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.policy.html#email.policy.SMTP\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-data docutils literal notranslate\">SMTP</code></a> policy to ensure <code class=\"docutils literal notranslate\">\\r\\n</code> line endings as\nrequired 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\">EmailMessage</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\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 6.0</p><p>The <code class=\"docutils literal notranslate\">policy</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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">EmailMessage</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\">recipients</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\">to</code>, <code class=\"docutils literal notranslate\">cc</code> or <code class=\"docutils literal notranslate\">bcc</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\">attach</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">filename</em>, <em class=\"sig-param\">content</em>, <em class=\"sig-param\">mimetype</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\">attach</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">mimepart</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\">attach()</code>:</p>\n<ul>\n<li><p>You can pass it three arguments: <code class=\"docutils literal notranslate\">filename</code>, <code class=\"docutils literal notranslate\">content</code> and\n<code class=\"docutils literal notranslate\">mimetype</code>. <code class=\"docutils literal notranslate\">filename</code> is the name of the file attachment as it\nwill appear in the email, <code class=\"docutils literal notranslate\">content</code> is the data that will be\ncontained inside the attachment and <code class=\"docutils literal notranslate\">mimetype</code> is the optional MIME\ntype for the attachment. If you omit <code class=\"docutils literal notranslate\">mimetype</code>, the MIME content\ntype will be guessed from the filename of the attachment.</p>\n<p>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>message<span class=\"o\">.</span>attach<span class=\"p\">(</span><span class=\"s2\">&quot;design.png&quot;</span><span class=\"p\">,</span> img_data<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\">mimetype</code> of <em class=\"mimetype\">message/rfc822</em>,\n<code class=\"docutils literal notranslate\">content</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\">django.core.mail.EmailMessage</code></a> or\nPython’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.EmailMessage\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">email.message.EmailMessage</code></a> or\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.compat32-message.html#email.message.Message\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">email.message.Message</code></a>.</p>\n<p>For a <code class=\"docutils literal notranslate\">mimetype</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\">attach()</code> a single Python\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</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=\"nn\">email.utils</span>\n<span class=\"kn\">from</span> <span class=\"nn\">email.message</span> <span class=\"kn\">import</span> MIMEPart\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> EmailMultiAlternatives\n\nmessage <span class=\"o\">=</span> EmailMultiAlternatives<span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>\nimage_data_bytes <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>\ncid <span class=\"o\">=</span> email<span class=\"o\">.</span>utils<span class=\"o\">.</span>make_msgid<span class=\"p\">()</span>\ninline_image <span class=\"o\">=</span> email<span class=\"o\">.</span>message<span class=\"o\">.</span>MIMEPart<span class=\"p\">()</span>\ninline_image<span class=\"o\">.</span>set_content<span class=\"p\">(</span>\n    image_data_bytes<span class=\"p\">,</span>\n    maintype<span class=\"o\">=</span><span class=\"s2\">&quot;image&quot;</span><span class=\"p\">,</span>\n    subtype<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    disposition<span class=\"o\">=</span><span class=\"s2\">&quot;inline&quot;</span><span class=\"p\">,</span>\n    cid<span class=\"o\">=</span>cid<span class=\"p\">,</span>\n<span class=\"p\">)</span>\nmessage<span class=\"o\">.</span>attach<span class=\"p\">(</span>inline_image<span class=\"p\">)</span>\n<span class=\"c1\"># Refer to Content-ID in HTML without angle brackets</span>\nmessage<span class=\"o\">.</span>attach_alternative<span class=\"p\">(</span><span class=\"sa\">f</span><span class=\"s1\">&#39;… &lt;img src=&quot;cid:</span><span class=\"si\">{</span>cid<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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-meth docutils literal notranslate\">email.contentmanager.set_content()</code></a> documentation\ndescribes the supported arguments for <code class=\"docutils literal notranslate\">MIMEPart.set_content()</code>.</p>\n<aside class=\"version-note version-changed\" data-version=\"6.0\" role=\"note\">\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=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</code></a> attachments was\nadded.</p>\n</aside>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Support for <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.mime.html#email.mime.base.MIMEBase\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">email.mime.base.MIMEBase</code></a> attachments is\ndeprecated. Use <a class=\"reference external\" href=\"https://docs.python.org/3/library/email.message.html#email.message.MIMEPart\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-class docutils literal notranslate\">MIMEPart</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\">attach_file</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">path</em>, <em class=\"sig-param\">mimetype<span class=\"o\">=</span><span class=\"default_value\">None</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>message<span class=\"o\">.</span>attach_file<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\">attach()</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\">class</span> </em><span class=\"sig-name descname\">EmailAttachment</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><aside class=\"version-note version-added\" data-version=\"5.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 5.2</p></aside>\n<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\">filename</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">content</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">mimetype</code></p></li>\n</ul>\n</dd></dl>\n\n</section>\n<section id=\"sending-alternative-content-types\">\n<h3>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></h3>\n<section id=\"sending-multiple-content-versions\">\n<h4>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></h4>\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\">EmailMultiAlternatives</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\">class</span> </em><span class=\"sig-name descname\">EmailMultiAlternatives</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\">EmailMessage</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\">attach_alternative()</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\">EmailMessage</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\">alternatives</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\">EmailAlternative</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>assertEqual<span class=\"p\">(</span><span class=\"nb\">len</span><span class=\"p\">(</span>msg<span class=\"o\">.</span>alternatives<span class=\"p\">),</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>msg<span class=\"o\">.</span>alternatives<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>content<span class=\"p\">,</span> html_content<span class=\"p\">)</span>\n<span class=\"bp\">self</span><span class=\"o\">.</span>assertEqual<span class=\"p\">(</span>msg<span class=\"o\">.</span>alternatives<span class=\"p\">[</span><span class=\"mi\">0</span><span class=\"p\">]</span><span class=\"o\">.</span>mimetype<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\">attach_alternative()</code></a>\nmethod, or passed to the constructor.</p>\n<aside class=\"version-note version-changed\" data-version=\"5.2\" role=\"note\">\n<p class=\"version-note-title\">Changed in Django 5.2</p><p>In older versions, <code class=\"docutils literal notranslate\">alternatives</code> was a list of regular tuples,\nas opposed to <a class=\"reference internal\" href=\"#django.core.mail.EmailAlternative\" title=\"django.core.mail.EmailAlternative\"><code class=\"xref py py-class docutils literal notranslate\">EmailAlternative</code></a> named tuples.</p>\n</aside>\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\">attach_alternative</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">content</em>, <em class=\"sig-param\">mimetype</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=\"nn\">django.core.mail</span> <span class=\"kn\">import</span> EmailMultiAlternatives\n\nsubject <span class=\"o\">=</span> <span class=\"s2\">&quot;hello&quot;</span>\nfrom_email <span class=\"o\">=</span> <span class=\"s2\">&quot;from@example.com&quot;</span>\nto <span class=\"o\">=</span> <span class=\"s2\">&quot;to@example.com&quot;</span>\ntext_content <span class=\"o\">=</span> <span class=\"s2\">&quot;This is an important message.&quot;</span>\nhtml_content <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>\nmsg <span class=\"o\">=</span> EmailMultiAlternatives<span class=\"p\">(</span>subject<span class=\"p\">,</span> text_content<span class=\"p\">,</span> from_email<span class=\"p\">,</span> <span class=\"p\">[</span>to<span class=\"p\">])</span>\nmsg<span class=\"o\">.</span>attach_alternative<span class=\"p\">(</span>html_content<span class=\"p\">,</span> <span class=\"s2\">&quot;text/html&quot;</span><span class=\"p\">)</span>\nmsg<span class=\"o\">.</span>send<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\">body_contains</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">text</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><aside class=\"version-note version-added\" data-version=\"5.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 5.2</p></aside>\n<p>Returns a boolean indicating whether the provided <code class=\"docutils literal notranslate\">text</code> is\ncontained in the email <code class=\"docutils literal notranslate\">body</code> and in all attached MIME type\n<code class=\"docutils literal notranslate\">text/*</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=\"nf\">test_contains_email_content</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n    subject <span class=\"o\">=</span> <span class=\"s2\">&quot;Hello World&quot;</span>\n    from_email <span class=\"o\">=</span> <span class=\"s2\">&quot;from@example.com&quot;</span>\n    to <span class=\"o\">=</span> <span class=\"s2\">&quot;to@example.com&quot;</span>\n    msg <span class=\"o\">=</span> EmailMultiAlternatives<span class=\"p\">(</span>subject<span class=\"p\">,</span> <span class=\"s2\">&quot;I am content.&quot;</span><span class=\"p\">,</span> from_email<span class=\"p\">,</span> <span class=\"p\">[</span>to<span class=\"p\">])</span>\n    msg<span class=\"o\">.</span>attach_alternative<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>assertIs<span class=\"p\">(</span>msg<span class=\"o\">.</span>body_contains<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>assertIs<span class=\"p\">(</span>msg<span class=\"o\">.</span>body_contains<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\">class</span> </em><span class=\"sig-name descname\">EmailAlternative</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><aside class=\"version-note version-added\" data-version=\"5.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 5.2</p></aside>\n<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\">content</code></p></li>\n<li><p><code class=\"docutils literal notranslate\">mimetype</code></p></li>\n</ul>\n</dd></dl>\n\n</section>\n<section id=\"updating-the-default-content-type\">\n<h4>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></h4>\n<p>By default, the MIME type of the <code class=\"docutils literal notranslate\">body</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\">EmailMessage</code></a>\nis <code class=\"docutils literal notranslate\">&quot;text/plain&quot;</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\">content_subtype</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\">EmailMessage</code></a> class to change the main content type.\nThe major type will always be <code class=\"docutils literal notranslate\">&quot;text&quot;</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>msg <span class=\"o\">=</span> EmailMessage<span class=\"p\">(</span>subject<span class=\"p\">,</span> html_content<span class=\"p\">,</span> from_email<span class=\"p\">,</span> <span class=\"p\">[</span>to<span class=\"p\">])</span>\nmsg<span class=\"o\">.</span>content_subtype <span class=\"o\">=</span> <span class=\"s2\">&quot;html&quot;</span>  <span class=\"c1\"># Main content is now text/html</span>\nmsg<span class=\"o\">.</span>send<span class=\"p\">()</span>\n</code></pre></div>\n</section>\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>The email backend class has the following methods:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">open()</code> instantiates a long-lived email-sending connection.</p></li>\n<li><p><code class=\"docutils literal notranslate\">close()</code> closes the current email-sending connection.</p></li>\n<li><p><code class=\"docutils literal notranslate\">send_messages(email_messages)</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\">EmailMessage</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>It can also be used as a context manager, which will automatically call\n<code class=\"docutils literal notranslate\">open()</code> and <code class=\"docutils literal notranslate\">close()</code> as needed:</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=\"nn\">django.core</span> <span class=\"kn\">import</span> mail\n\n<span class=\"k\">with</span> mail<span class=\"o\">.</span>get_connection<span class=\"p\">()</span> <span class=\"k\">as</span> connection<span class=\"p\">:</span>\n    mail<span class=\"o\">.</span>EmailMessage<span class=\"p\">(</span>\n        subject1<span class=\"p\">,</span>\n        body1<span class=\"p\">,</span>\n        from1<span class=\"p\">,</span>\n        <span class=\"p\">[</span>to1<span class=\"p\">],</span>\n        connection<span class=\"o\">=</span>connection<span class=\"p\">,</span>\n    <span class=\"p\">)</span><span class=\"o\">.</span>send<span class=\"p\">()</span>\n    mail<span class=\"o\">.</span>EmailMessage<span class=\"p\">(</span>\n        subject2<span class=\"p\">,</span>\n        body2<span class=\"p\">,</span>\n        from2<span class=\"p\">,</span>\n        <span class=\"p\">[</span>to2<span class=\"p\">],</span>\n        connection<span class=\"o\">=</span>connection<span class=\"p\">,</span>\n    <span class=\"p\">)</span><span class=\"o\">.</span>send<span class=\"p\">()</span>\n</code></pre></div>\n<section id=\"obtaining-an-instance-of-an-email-backend\">\n<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 <a class=\"reference internal\" href=\"#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\">get_connection()</code></a> function in <code class=\"docutils literal notranslate\">django.core.mail</code> returns an\ninstance of the email backend that you can use.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.core.mail.get_connection\">\n<span class=\"sig-name descname\">get_connection</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">backend<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"keyword-only-separator o\"><abbr title=\"Keyword-only parameters separator (PEP 3102)\">*</abbr></span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</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></dd></dl>\n\n<p>By default, a call to <code class=\"docutils literal notranslate\">get_connection()</code> will return an instance of the\nemail backend specified in <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_BACKEND</code></a>. If you specify the\n<code class=\"docutils literal notranslate\">backend</code> argument, an instance of that backend will be instantiated.</p>\n<p>The keyword-only <code class=\"docutils literal notranslate\">fail_silently</code> argument controls how the backend should\nhandle errors. If <code class=\"docutils literal notranslate\">fail_silently</code> is True, exceptions during the email\nsending process will be silently ignored.</p>\n<p>All other keyword arguments are passed directly to the constructor of the\nemail backend.</p>\n<p>Django ships with several email sending backends. With the exception of the\nSMTP backend (which is the default), these backends are only useful during\ntesting and development. If you have special email sending requirements, you\ncan <a class=\"reference internal\" href=\"#topic-custom-email-backend\"><span class=\"std std-ref\">write your own email backend</span></a>.</p>\n<aside class=\"version-note version-deprecated\" data-version=\"6.0\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 6.0</p><p><span class=\"versionmodified deprecated\">Αποσύρθηκε στην έκδοση 6.0: </span>Passing <code class=\"docutils literal notranslate\">fail_silently</code> as positional argument is deprecated.</p>\n</aside>\n<section id=\"smtp-backend\">\n<span id=\"topic-email-smtp-backend\"></span><h4>SMTP backend<a class=\"heading-anchor\" href=\"#smtp-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\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\">class</span> </em><span class=\"sig-prename descclassname\">backends.smtp.</span><span class=\"sig-name descname\">EmailBackend</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">host<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">port<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">username<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">password<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">use_tls<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">fail_silently<span class=\"o\">=</span><span class=\"default_value\">False</span></em>, <em class=\"sig-param\">use_ssl<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">timeout<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">ssl_keyfile<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\">ssl_certfile<span class=\"o\">=</span><span class=\"default_value\">None</span></em>, <em class=\"sig-param\"><span class=\"o\">**</span>kwargs</em><span class=\"sig-paren\">)</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>This is the default backend. Email will be sent through a SMTP server.</p>\n<p>The value for each argument is retrieved from the matching setting if the\nargument is <code class=\"docutils literal notranslate\">None</code>:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">host</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">port</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_PORT</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">username</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_USER</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">password</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST_PASSWORD</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">use_tls</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_USE_TLS\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_USE_TLS</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">use_ssl</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_USE_SSL\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_USE_SSL</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">timeout</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_TIMEOUT\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_TIMEOUT</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">ssl_keyfile</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_SSL_KEYFILE\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_SSL_KEYFILE</code></a></p></li>\n<li><p><code class=\"docutils literal notranslate\">ssl_certfile</code>: <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_SSL_CERTFILE\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_SSL_CERTFILE</code></a></p></li>\n</ul>\n<p>The SMTP backend is the default configuration inherited by Django. If you\nwant to specify it explicitly, put the following in your settings:</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>EMAIL_BACKEND <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span>\n</code></pre></div>\n<p>If unspecified, the default <code class=\"docutils literal notranslate\">timeout</code> will be the one provided by\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/socket.html#socket.getdefaulttimeout\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-func docutils literal notranslate\">socket.getdefaulttimeout()</code></a>, which defaults to <code class=\"docutils literal notranslate\">None</code> (no timeout).</p>\n</dd></dl>\n\n</section>\n<section id=\"console-backend\">\n<span id=\"topic-email-console-backend\"></span><h4>Console backend<a class=\"heading-anchor\" href=\"#console-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Instead of sending out real emails the console backend just writes the\nemails that would be sent to the standard output. By default, the console\nbackend writes to <code class=\"docutils literal notranslate\">stdout</code>. You can use a different stream-like object by\nproviding the <code class=\"docutils literal notranslate\">stream</code> keyword argument when constructing the connection.</p>\n<p>To specify this backend, put the following in your settings:</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>EMAIL_BACKEND <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.console.EmailBackend&quot;</span>\n</code></pre></div>\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=\"file-backend\">\n<span id=\"topic-email-file-backend\"></span><h4>File backend<a class=\"heading-anchor\" href=\"#file-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\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. The directory to which the files are\nwritten is either taken from the <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_FILE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_FILE_PATH</code></a> setting or from the\n<code class=\"docutils literal notranslate\">file_path</code> keyword when creating a connection with <a class=\"reference internal\" href=\"#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\">get_connection()</code></a>.</p>\n<p>To specify this backend, put the following in your settings:</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>EMAIL_BACKEND <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.filebased.EmailBackend&quot;</span>\nEMAIL_FILE_PATH <span class=\"o\">=</span> <span class=\"s2\">&quot;/tmp/app-messages&quot;</span>  <span class=\"c1\"># change this to a proper location</span>\n</code></pre></div>\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=\"in-memory-backend\">\n<span id=\"topic-email-memory-backend\"></span><h4>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></h4>\n<p>The <code class=\"docutils literal notranslate\">'locmem'</code> backend stores messages in a special attribute of the\n<code class=\"docutils literal notranslate\">django.core.mail</code> module. The <code class=\"docutils literal notranslate\">outbox</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\">EmailMessage</code></a> instance for each\nmessage that would be sent.</p>\n<p>To specify this backend, put the following in your settings:</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>EMAIL_BACKEND <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.locmem.EmailBackend&quot;</span>\n</code></pre></div>\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<p>Django’s test runner <a class=\"reference internal\" href=\"/el/6.0/topics/testing/tools/#topics-testing-email\"><span class=\"std std-ref\">automatically uses this backend for testing</span></a>.</p>\n</section>\n<section id=\"dummy-backend\">\n<span id=\"topic-email-dummy-backend\"></span><h4>Dummy backend<a class=\"heading-anchor\" href=\"#dummy-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>As the name suggests the dummy backend does nothing with your messages. To\nspecify this backend, put the following in your settings:</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>EMAIL_BACKEND <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.dummy.EmailBackend&quot;</span>\n</code></pre></div>\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=\"admonition-there-are-community-maintained-solutions-too admonition\" role=\"note\">\n<p class=\"admonition-title\">There are community-maintained solutions too!</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-and-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</section>\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\nbackend. The <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_BACKEND</code></a> setting in your settings file is then\nthe Python import path for your backend class.</p>\n<p>Custom email backends should subclass <code class=\"docutils literal notranslate\">BaseEmailBackend</code> that is located in\nthe <code class=\"docutils literal notranslate\">django.core.mail.backends.base</code> module. A custom email backend must\nimplement the <code class=\"docutils literal notranslate\">send_messages(email_messages)</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\">EmailMessage</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\">open()</code> and <code class=\"docutils literal notranslate\">close()</code> methods.\nRefer to <code class=\"docutils literal notranslate\">smtp.EmailBackend</code> for a reference implementation.</p>\n</section>\n<section id=\"sending-multiple-emails\">\n<span id=\"topics-sending-multiple-emails\"></span><h3>Sending multiple emails<a class=\"heading-anchor\" href=\"#sending-multiple-emails\"><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 you tell an email backend to reuse a connection.</p>\n<p>Firstly, you can use the <code class=\"docutils literal notranslate\">send_messages()</code> method on a connection. This takes\na 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\">EmailMessage</code></a> (or subclass) instances, and sends them all\nusing that single connection. As a consequence, any <a class=\"reference internal\" href=\"#django.core.mail.EmailMessage\" title=\"django.core.mail.EmailMessage\"><code class=\"xref py py-class docutils literal notranslate\">connection</code></a> set on an individual message is ignored.</p>\n<p>For example, if you have a function called <code class=\"docutils literal notranslate\">get_notification_email()</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\">EmailMessage</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\">send_messages()</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=\"nn\">django.core</span> <span class=\"kn\">import</span> mail\n\nconnection <span class=\"o\">=</span> mail<span class=\"o\">.</span>get_connection<span class=\"p\">()</span>  <span class=\"c1\"># Use default email connection</span>\nmessages <span class=\"o\">=</span> get_notification_email<span class=\"p\">()</span>\nconnection<span class=\"o\">.</span>send_messages<span class=\"p\">(</span>messages<span class=\"p\">)</span>\n</code></pre></div>\n<p>In this example, the call to <code class=\"docutils literal notranslate\">send_messages()</code> opens a connection on the\nbackend, sends the list of messages, and then closes the connection again.</p>\n<p>The second approach is to use the <code class=\"docutils literal notranslate\">open()</code> and <code class=\"docutils literal notranslate\">close()</code> methods on the\nemail backend to manually control the connection. <code class=\"docutils literal notranslate\">send_messages()</code> will not\nmanually open 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=\"nn\">django.core</span> <span class=\"kn\">import</span> mail\n\nconnection <span class=\"o\">=</span> mail<span class=\"o\">.</span>get_connection<span class=\"p\">()</span>\n\n<span class=\"c1\"># Manually open the connection</span>\nconnection<span class=\"o\">.</span>open<span class=\"p\">()</span>\n\n<span class=\"c1\"># Construct an email message that uses the connection</span>\nemail1 <span class=\"o\">=</span> mail<span class=\"o\">.</span>EmailMessage<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Hello&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Body goes here&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;to1@example.com&quot;</span><span class=\"p\">],</span>\n    connection<span class=\"o\">=</span>connection<span class=\"p\">,</span>\n<span class=\"p\">)</span>\nemail1<span class=\"o\">.</span>send<span class=\"p\">()</span>  <span class=\"c1\"># Send the email</span>\n\n<span class=\"c1\"># Construct two more messages</span>\nemail2 <span class=\"o\">=</span> mail<span class=\"o\">.</span>EmailMessage<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Hello&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Body goes here&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;to2@example.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\nemail3 <span class=\"o\">=</span> mail<span class=\"o\">.</span>EmailMessage<span class=\"p\">(</span>\n    <span class=\"s2\">&quot;Hello&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Body goes here&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;to3@example.com&quot;</span><span class=\"p\">],</span>\n<span class=\"p\">)</span>\n\n<span class=\"c1\"># Send the two emails in a single call -</span>\nconnection<span class=\"o\">.</span>send_messages<span class=\"p\">([</span>email2<span class=\"p\">,</span> email3<span class=\"p\">])</span>\n<span class=\"c1\"># The connection was already open so send_messages() doesn&#39;t close it.</span>\n<span class=\"c1\"># We need to manually close the connection.</span>\nconnection<span class=\"o\">.</span>close<span class=\"p\">()</span>\n</code></pre></div>\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\">stdout</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 «dumb» 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 -m pip install <span class=\"s2\">&quot;aiosmtpd &gt;= 1.4.5&quot;</span>\n\npython -m aiosmtpd -n -l 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 the <a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_HOST</code></a> and\n<a class=\"reference internal\" href=\"/el/6.0/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\">EMAIL_PORT</code></a> accordingly. For a more detailed discussion of SMTP\nserver options, see the documentation of the <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=\"/el/6.0/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":"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":"Examples","anchor":"examples","children":[]},{"title":"Preventing header injection","anchor":"preventing-header-injection","children":[]},{"title":"The EmailMessage class","anchor":"the-emailmessage-class","children":[{"title":"EmailMessage Objects","anchor":"emailmessage-objects","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":"Email backends","anchor":"email-backends","children":[{"title":"Obtaining an instance of an email backend","anchor":"obtaining-an-instance-of-an-email-backend","children":[{"title":"SMTP backend","anchor":"smtp-backend","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":"Defining a custom email backend","anchor":"defining-a-custom-email-backend","children":[]},{"title":"Sending multiple emails","anchor":"sending-multiple-emails","children":[]}]},{"title":"Configuring email for development","anchor":"configuring-email-for-development","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/el/6.0/topics/"}],"prev":{"docname":"topics/signing","title":"Cryptographic signing","url":"/el/6.0/topics/signing/"},"next":{"docname":"topics/i18n/index","title":"Internationalization and localization","url":"/el/6.0/topics/i18n/"},"formats":{"html":"/el/6.0/topics/email/","markdown":"/el/6.0/topics/email.md","json":"/el/6.0/topics/email.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/email.txt","official":"https://docs.djangoproject.com/el/6.0/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"]}