{"title":"Migrating email to mailers","version":"6.1","locale":"zh-hans","docname":"howto/mailers-migration","url":"/zh-hans/6.1/howto/mailers-migration/","canonical":"https://djangodocs.dev/zh-hans/6.1/howto/mailers-migration/","summary":"Django 6.1 introduced the MAILERS setting, replacing EMAIL_BACKEND and several other EMAIL_* settings. It also introduced mail.mailers for obtaining configured…","html":"<span id=\"migrating-to-mailers\"></span><h1>Migrating email to mailers<a class=\"heading-anchor\" href=\"#migrating-email-to-mailers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django 6.1 introduced the <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting, replacing\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a> and several other <code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_*</span></code> settings. It also\nintroduced <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mail.mailers</span></code></a> for obtaining configured email backend\ninstances, replacing <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail.get_connection()</span></code></a>. A new <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument\nreplaces the earlier <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> in Django functions that send email.</p>\n<p>This guide provides details on migrating existing projects to the new mailers\nfunctionality.</p>\n<p>All Django projects that send email should:</p>\n<ul>\n<li><p>If using any third-party packages that send email, <a class=\"reference internal\" href=\"#migrating-to-mailers-third-party\"><span class=\"std std-ref\">verify their\ncompatibility</span></a> with <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a>\nbefore making other changes.</p></li>\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-settings\"><span class=\"std std-ref\">Update email-related settings</span></a>.</p></li>\n<li><p>Run with deprecation warnings enabled (see\n<a class=\"reference internal\" href=\"/zh-hans/6.1/howto/upgrade-version/#resolving-deprecation-warnings\"><span class=\"std std-ref\">处理过期警告</span></a>) to identify\nother code needing updates.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Running tests may not identify all relevant deprecations. Test suites\noften use a non-functional email backend (such as the memory backend that\nDjango <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/testing/tools/#topics-testing-email\"><span class=\"std std-ref\">substitutes during tests</span></a>), so can\nmiss deprecation warnings that are only issued from the production email\nconfiguration.</p>\n<p>Consider running with deprecation warnings enabled in production to catch\nthose deprecations. Or carefully review your code (including third-party\npackages) for use of any deprecated email features.</p>\n</aside>\n</li>\n</ul>\n<p>Other updates are needed only for projects or reusable Django libraries that\nuse these specific features:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-get-connection\"><span class=\"std std-ref\">Replacing get_connection() and connection arguments</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-email-backends\"><span class=\"std std-ref\">Migrating custom email backends</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-auth\"><span class=\"std std-ref\">Replacing auth_user and auth_password</span></a></p></li>\n<li><p><a class=\"reference internal\" href=\"#migrating-to-mailers-adminemailhandler\"><span class=\"std std-ref\">Updating AdminEmailHandler email_backend</span></a></p></li>\n</ul>\n<aside class=\"version-note version-deprecated\" data-version=\"6.1\">\n<p class=\"version-note-title\">Deprecated since Django 6.1</p><p><span class=\"versionmodified deprecated\">Deprecated since version 6.1: </span>The following settings and functions will be removed in Django 7.0:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_FILE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_FILE_PATH</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_PASSWORD</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_USER</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_PORT</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_SSL_CERTFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_CERTFILE</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_SSL_KEYFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_KEYFILE</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_TIMEOUT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_TIMEOUT</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_USE_SSL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_SSL</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_USE_TLS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_TLS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail.get_connection()</span></code></a> and the <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> arguments to mail\nfunctions</p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> argument to <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mail_admins\" title=\"django.core.mail.mail_admins\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail_admins()</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mail_managers\" title=\"django.core.mail.mail_managers\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail_managers()</span></code></a>,\nand <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.EmailMessage.send\" title=\"django.core.mail.EmailMessage.send\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">EmailMessage.send()</span></code></a></p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code> arguments to <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a>\nand <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a></p></li>\n<li><p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">email_backend</span></code> argument to <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/logging/#django.utils.log.AdminEmailHandler\" title=\"django.utils.log.AdminEmailHandler\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AdminEmailHandler</span></code></a></p></li>\n</ul>\n</aside>\n<section id=\"migrating-settings\">\n<span id=\"migrating-to-mailers-settings\"></span><h2>Migrating settings<a class=\"heading-anchor\" href=\"#migrating-settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Often, the only change needed to migrate to mailers is updating email-related\nsettings. In your project's settings, define a <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> dict with a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;default&quot;</span></code> entry matching the earlier <code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_*</span></code> 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><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"o\">...</span><span class=\"p\">,</span>  <span class=\"c1\"># value of EMAIL_BACKEND setting</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"c1\"># values from other deprecated EMAIL_* settings</span>\n            <span class=\"o\">...</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>For example, to update these 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><span class=\"n\">EMAIL_BACKEND</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span>\n<span class=\"n\">EMAIL_HOST</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;mail.example.net&quot;</span>\n<span class=\"n\">EMAIL_USE_TLS</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n<span class=\"n\">EMAIL_PORT</span> <span class=\"o\">=</span> <span class=\"mi\">587</span>\n<span class=\"n\">EMAIL_HOST_USER</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;user@example.net&quot;</span>\n<span class=\"n\">EMAIL_HOST_PASSWORD</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;password&quot;</span>\n</code></pre></div>\n<p>use:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;mail.example.net&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;use_tls&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n            <span class=\"c1\"># port is not needed: it defaults to 587 with use_tls True.</span>\n            <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;user@example.net&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p id=\"deprecated-email-settings\">The complete list of deprecated <code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_*</span></code> settings and where they should be\nmoved in a <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration is:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;BACKEND&quot;</span></code>. If your settings didn't\ndefine <code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code>, the default value was\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.smtp.EmailBackend&quot;</span></code> (which is also the default\nif a <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration doesn't specify the <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;BACKEND&quot;</span></code>).</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_FILE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_FILE_PATH</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;file_path&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code> (with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;BACKEND&quot;</span></code> set to <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;django.core.mail.backends.filebased.EmailBackend&quot;</span></code>).</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;host&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>. A <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;host&quot;</span></code> is\nrequired for the SMTP email backend. If your settings didn't define\n<code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code>, set <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;host&quot;</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;localhost&quot;</span></code> (the default value for the\ndeprecated setting).</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST_PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_PASSWORD</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;password&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST_USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST_USER</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;username&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>. (Note\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;username&quot;</span></code> doesn't quite follow the naming pattern for the other\ndeprecated settings.)</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_PORT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_PORT</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;port&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>. It can be omitted\nif the connection uses the default port for its security (465 for SSL, 587\nfor TLS, or 25 for an unsecured SMTP connection).</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_SSL_CERTFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_CERTFILE</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;ssl_certfile&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_SSL_KEYFILE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_SSL_KEYFILE</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;ssl_keyfile&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_TIMEOUT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_TIMEOUT</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;timeout&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_USE_SSL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_SSL</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_ssl&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_USE_TLS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_USE_TLS</span></code></a> becomes <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;use_tls&quot;</span></code> in <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code>.</p></li>\n</ul>\n<p>For third-party or custom email backends, the available <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code> depend on\nthe backend. Refer to the third-party documentation, or for custom backends see\n<a class=\"reference internal\" href=\"#migrating-to-mailers-email-backends\"><span class=\"std std-ref\">Migrating custom email backends</span></a> below.</p>\n<p>The <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#topic-email-configuration\"><span class=\"std std-ref\">Configuring email</span></a> topic has more information on the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code> setting and additional configuration examples.</p>\n</section>\n<section id=\"reusable-library-readiness\">\n<span id=\"migrating-to-mailers-third-party\"></span><h2>Reusable library readiness<a class=\"heading-anchor\" href=\"#reusable-library-readiness\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>In most cases, third-party packages that send email through Django will\ncontinue working with projects whose settings have been upgraded to use\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a>. (If they use any deprecated mail features, those will of\ncourse cause deprecation warnings.)</p>\n<p>There are two deprecated features that are <em>not</em> supported when\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> is defined in a project's settings:</p>\n<ul class=\"simple\">\n<li><p>Trying to access any of the deprecated <code class=\"docutils literal notranslate\"><span class=\"pre\">EMAIL_*</span></code> settings on\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.conf.settings</span></code> (e.g., checking <code class=\"docutils literal notranslate\"><span class=\"pre\">settings.EMAIL_BACKEND</span></code> or using\n<code class=\"docutils literal notranslate\"><span class=\"pre\">settings.EMAIL_HOST_USER</span></code>).</p></li>\n<li><p>Calling <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail.get_connection(&quot;path.to.EmailBackend&quot;)</span></code></a> with a specific backend path. (Other arguments to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> are still supported, and will simply issue deprecation\nwarnings when <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> is defined.)</p></li>\n</ul>\n<p>If a third-party package does either of those, projects that use it will not be\nable to change to the <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting until the package has been\nupdated.</p>\n<section id=\"solving-not-available-when-mailers-is-defined-errors\">\n<h3>Solving &quot;not available when MAILERS is defined&quot; errors<a class=\"heading-anchor\" href=\"#solving-not-available-when-mailers-is-defined-errors\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If either of these errors are raised within a third-party package, that\nindicates it is not compatible with the <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> setting:</p>\n<ul class=\"simple\">\n<li><p><code class=\"samp docutils literal notranslate\"><span class=\"pre\">AttributeError:</span> <span class=\"pre\">&quot;The</span> <em><span class=\"pre\">name</span></em> <span class=\"pre\">setting</span> <span class=\"pre\">is</span> <span class=\"pre\">not</span> <span class=\"pre\">available</span> <span class=\"pre\">when</span> <span class=\"pre\">MAILERS</span> <span class=\"pre\">is</span>\n<span class=\"pre\">defined&quot;</span></code> where <em>name</em> is <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_BACKEND\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_BACKEND</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code></a>,\nor one of the other deprecated settings.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">RuntimeError:</span> <span class=\"pre\">get_connection(backend,</span> <span class=\"pre\">...)</span> <span class=\"pre\">is</span> <span class=\"pre\">not</span> <span class=\"pre\">supported</span> <span class=\"pre\">with</span> <span class=\"pre\">MAILERS.</span></code></p></li>\n</ul>\n<p>If you see these errors, check if a newer version of that dependency is\navailable. If not (and if there are no alternatives to that package), you will\nneed to remove <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> from your settings and replace it with the\nequivalent <a class=\"reference internal\" href=\"#deprecated-email-settings\"><span class=\"std std-ref\">deprecated email settings</span></a> until\nthe package has been updated.</p>\n<p>If those errors are coming from your own code, see the other sections in this\nmigration guide for recommended updates.</p>\n</section>\n</section>\n<section id=\"replacing-get-connection-and-connection-arguments\">\n<span id=\"migrating-to-mailers-get-connection\"></span><h2>Replacing <code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> arguments<a class=\"heading-anchor\" href=\"#replacing-get-connection-and-connection-arguments\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The replacements for the deprecated <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.get_connection\" title=\"django.core.mail.get_connection\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail.get_connection()</span></code></a> function\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code> arguments to other mail functions depend on how and why\nthe connections are being created:</p>\n<ul>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection()</span></code> called with no arguments</p>\n<p>Replace with <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mailers.default\" title=\"django.core.mail.mailers.default\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mailers.default</span></code></a>. For example, update this 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=\"n\">connection</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">get_connection</span><span class=\"p\">()</span>\n<span class=\"n\">connection</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">,</span> <span class=\"n\">email2</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>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><span class=\"n\">connection</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">mailers</span><span class=\"o\">.</span><span class=\"n\">default</span>\n<span class=\"n\">connection</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">,</span> <span class=\"n\">email2</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>Note that <code class=\"docutils literal notranslate\"><span class=\"pre\">mailers.default</span></code> <em>is</em> the default for Django's mail-sending\nfunctions. Code like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> <span class=\"n\">connection</span><span class=\"o\">=</span><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">get_connection</span><span class=\"p\">())</span>\n</code></pre></div>\n<p>can be updated 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><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">)</span>  <span class=\"c1\"># No connection arg needed.</span>\n</code></pre></div>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection(fail_silently=True)</span></code></p>\n<p>See <a class=\"reference internal\" href=\"#migrating-to-mailers-fail-silently\"><span class=\"std std-ref\">Replacing fail_silently</span></a>.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">get_connection(...)</span></code> called with any other arguments</p>\n<p>Define a custom <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration with the desired backend\nand options. Then refer to it in the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument when sending mail,\nor obtain an email backend instance from <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mailers\" title=\"django.core.mail.mailers\"><code class=\"xref py py-data docutils literal notranslate\"><span class=\"pre\">mail.mailers</span></code></a> with the\nconfiguration name.</p>\n<p>For example, to upgrade this 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=\"n\">connection</span> <span class=\"o\">=</span> <span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">get_connection</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;path.to.custom.EmailBackend&quot;</span><span class=\"p\">,</span> <span class=\"n\">option1</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span> <span class=\"n\">option2</span><span class=\"o\">=</span><span class=\"s2\">&quot;value&quot;</span>\n<span class=\"p\">)</span>\n<span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> <span class=\"n\">connection</span><span class=\"o\">=</span><span class=\"n\">connection</span><span class=\"p\">)</span>\n<span class=\"n\">connection</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">,</span> <span class=\"n\">email2</span><span class=\"p\">])</span>\n</code></pre></div>\n<p>Define a custom <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration 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><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"o\">...</span><span class=\"p\">},</span>\n    <span class=\"s2\">&quot;custom&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;path.to.custom.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;option1&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;option2&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;value&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>And then use it like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> <span class=\"n\">using</span><span class=\"o\">=</span><span class=\"s2\">&quot;custom&quot;</span><span class=\"p\">)</span>\n<span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">mailers</span><span class=\"p\">[</span><span class=\"s2\">&quot;custom&quot;</span><span class=\"p\">]</span><span class=\"o\">.</span><span class=\"n\">send_messages</span><span class=\"p\">([</span><span class=\"n\">email1</span><span class=\"p\">,</span> <span class=\"n\">email2</span><span class=\"p\">])</span>\n</code></pre></div>\n</li>\n</ul>\n</section>\n<section id=\"replacing-fail-silently\">\n<span id=\"migrating-to-mailers-fail-silently\"></span><h2>Replacing <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code><a class=\"heading-anchor\" href=\"#replacing-fail-silently\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The deprecated <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> arguments to <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a>,\n<a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mail_admins\" title=\"django.core.mail.mail_admins\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail_admins()</span></code></a>, <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.mail_managers\" title=\"django.core.mail.mail_managers\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">mail_managers()</span></code></a>, and\n<a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.EmailMessage.send\" title=\"django.core.mail.EmailMessage.send\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">EmailMessage.send()</span></code></a> can be replaced in several ways. Existing usage\nseems to have several different expectations for the behavior, many of which\ndon't match the actual (and email backend-dependent) implementation. Consider\nremoving <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> entirely if there is not a specific need for it.</p>\n<p>Calls with <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=True</span></code> should be updated with one of these options,\ndepending on the caller's intent:</p>\n<ul>\n<li><p>To send a message if email has been configured but avoid raising an error\nif it hasn't (e.g., in a reusable library), wrap the send call in <code class=\"docutils literal notranslate\"><span class=\"pre\">try:</span></code> /\n<code class=\"docutils literal notranslate\"><span class=\"pre\">except</span> <span class=\"pre\">mail.MailerDoesNotExist:</span> <span class=\"pre\">pass</span></code>.</p></li>\n<li><p>To ignore <em>all</em> exceptions (e.g., to avoid cascading failures in an error\nhandler that sends mail), wrap the send call in <code class=\"docutils literal notranslate\"><span class=\"pre\">try:</span></code> / <code class=\"docutils literal notranslate\"><span class=\"pre\">except</span>\n<span class=\"pre\">Exception:</span> <span class=\"pre\">pass</span></code>.</p></li>\n<li><p>To ignore only SMTP-related errors, wrap the send call in <code class=\"docutils literal notranslate\"><span class=\"pre\">try</span></code> /\n<code class=\"docutils literal notranslate\"><span class=\"pre\">except</span> <span class=\"pre\">OSError:</span> <span class=\"pre\">pass</span></code>. Note that this ignores both transient network\nglitches <em>and</em> SMTP configuration problems (just like the existing SMTP\nemail backend <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> implementation).</p></li>\n<li><p>To ignore end user typos in <code class=\"docutils literal notranslate\"><span class=\"pre\">to</span></code> addresses and other delivery problems,\nremove the <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> argument. Recipient errors are not generally\ndetected at send time, so using <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> for this purpose doesn't\naccomplish anything and could mask other problems like configuration errors.</p>\n<p>(In <em>local delivery</em> configurations, SMTP servers may report some recipient\nerrors at send time. If you are using <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> specifically to\nignore those errors, consider instead intercepting\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#smtplib.SMTPRecipientsRefused\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">SMTPRecipientsRefused</span></code></a> and/or\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/smtplib.html#smtplib.SMTPResponseException\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">SMTPResponseException</span></code></a>s with particular <code class=\"docutils literal notranslate\"><span class=\"pre\">smtp_code</span></code>\nvalues as a more precise filter.)</p>\n</li>\n<li><p>To create an email configuration that ignores certain backend-dependent\nerrors and reuse it for multiple sending operations, create a custom\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration with <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;fail_silently&quot;:</span> <span class=\"pre\">True</span></code> in the\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code></a>, then refer to that configuration\nwith <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> in the send call.</p></li>\n</ul>\n<p>Calls with <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently=False</span></code> should be updated to remove the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> arg, as that is the default.</p>\n</section>\n<section id=\"migrating-custom-email-backends\">\n<span id=\"migrating-to-mailers-email-backends\"></span><h2>Migrating custom email backends<a class=\"heading-anchor\" href=\"#migrating-custom-email-backends\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Custom <code class=\"docutils literal notranslate\"><span class=\"pre\">EmailBackend</span></code> implementations may need to be updated for\ncompatibility with mailers.</p>\n<ul class=\"simple\">\n<li><p>In the backend's <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code> method, accept explicit keyword arguments for\nall configuration options that can come from <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code></a>. Backends that use custom settings for configuration can\ncontinue to do so (or not, as they choose), but keyword arguments should take\nprecedence over settings.</p></li>\n<li><p>Accept variable <code class=\"docutils literal notranslate\"><span class=\"pre\">**kwargs</span></code> and pass them to superclass init. (This will\ninclude a new <code class=\"docutils literal notranslate\"><span class=\"pre\">alias</span></code> argument which must be passed to the superclass.)\nEnsure that any <code class=\"docutils literal notranslate\"><span class=\"pre\">**kwargs</span></code> used by the backend are <em>not</em> passed to\nsuperclass init, as that would generate an <code class=\"docutils literal notranslate\"><span class=\"pre\">InvalidMailer</span></code> error for\nunknown OPTIONS.</p></li>\n<li><p>Backends must now handle <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> themselves, if they want to\nsupport it. There is no requirement to support <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code>, and\nbackends that don't offer it should eliminate that keyword argument. (Do not\npass an explicit <code class=\"docutils literal notranslate\"><span class=\"pre\">fail_silently</span></code> arg to superclass init.)</p></li>\n<li><p>Do not accept variable positional <code class=\"docutils literal notranslate\"><span class=\"pre\">*args</span></code> or pass them to superclass init.</p></li>\n</ul>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">BaseEmailBackend</span></code> superclass now initializes an <code class=\"docutils literal notranslate\"><span class=\"pre\">alias</span></code> attribute.\nThis is useful for error messages (e.g., <code class=\"docutils literal notranslate\"><span class=\"pre\">raise</span> <span class=\"pre\">InvalidMailer(f&quot;Bad</span> <span class=\"pre\">host</span>\n<span class=\"pre\">{host}&quot;,</span> <span class=\"pre\">alias=self.alias)</span></code>), but should not be used for accessing\n<code class=\"docutils literal notranslate\"><span class=\"pre\">settings.MAILERS</span></code> directly. All OPTIONS for a mailer configuration are\npassed to backend init as keyword arguments.</p>\n<p>For reusable libraries that want to support compatibility with deprecated mail\nfunctions and settings, or that support older Django versions:</p>\n<ul class=\"simple\">\n<li><p>A backend can detect it is being initialized without <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> by\nchecking if <code class=\"docutils literal notranslate\"><span class=\"pre\">self.alias</span> <span class=\"pre\">is</span> <span class=\"pre\">None</span></code>. (Django's built-in backends check this to\ndecide whether deprecated settings should be used.) Libraries supporting\nolder Django versions will need to use <code class=\"docutils literal notranslate\"><span class=\"pre\">getattr(self,</span> <span class=\"pre\">&quot;alias&quot;,</span> <span class=\"pre\">None)</span></code>.</p></li>\n<li><p>Backends that borrow Django's SMTP email settings like <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-EMAIL_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">EMAIL_HOST</span></code></a>\nmust <em>not</em> try to access them when <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> is in use (<code class=\"docutils literal notranslate\"><span class=\"pre\">self.alias</span>\n<span class=\"pre\">is</span> <span class=\"pre\">not</span> <span class=\"pre\">None</span></code>), as this will cause a &quot;not available when MAILERS is defined&quot;\n<code class=\"docutils literal notranslate\"><span class=\"pre\">AttributeError</span></code>.</p></li>\n<li><p>Libraries supporting multiple Django versions can identify support for\nmailers with either <code class=\"docutils literal notranslate\"><span class=\"pre\">hasattr(django.core.mail,</span> <span class=\"pre\">&quot;mailers&quot;)</span></code> or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.VERSION</span> <span class=\"pre\">&gt;=</span> <span class=\"pre\">(6,</span> <span class=\"pre\">1)</span></code>.</p></li>\n</ul>\n</section>\n<section id=\"replacing-auth-user-and-auth-password\">\n<span id=\"migrating-to-mailers-auth\"></span><h2>Replacing <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code><a class=\"heading-anchor\" href=\"#replacing-auth-user-and-auth-password\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The deprecated <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_user</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">auth_password</span></code> arguments to\n<a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mail\" title=\"django.core.mail.send_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mail()</span></code></a> and <a class=\"reference internal\" href=\"/zh-hans/6.1/topics/email/#django.core.mail.send_mass_mail\" title=\"django.core.mail.send_mass_mail\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">send_mass_mail()</span></code></a> can be replaced by defining a\ncustom <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;username&quot;</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;password&quot;</span></code> <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS-OPTIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">&quot;OPTIONS&quot;</span></code></a>, and\nrefer to that configuration with the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code> argument when sending mail.</p>\n<p>For example, to upgrade:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> <span class=\"n\">auth_user</span><span class=\"o\">=</span><span class=\"s2\">&quot;admin&quot;</span><span class=\"p\">,</span> <span class=\"n\">auth_password</span><span class=\"o\">=</span><span class=\"s2\">&quot;admin-password&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Add a custom <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration 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><span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># Existing default configuration.</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;smtp.example.com&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;default-user&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;default-password&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"c1\"># Duplicate the default configuration, changing the auth.</span>\n    <span class=\"s2\">&quot;admin-config&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;OPTIONS&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;host&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;smtp.example.com&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;username&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;admin&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;password&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;admin-password&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>And then refer to it when sending:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">mail</span><span class=\"o\">.</span><span class=\"n\">send_mail</span><span class=\"p\">(</span><span class=\"o\">...</span><span class=\"p\">,</span> <span class=\"n\">using</span><span class=\"o\">=</span><span class=\"s2\">&quot;admin-config&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"updating-adminemailhandler-email-backend\">\n<span id=\"migrating-to-mailers-adminemailhandler\"></span><h2>Updating AdminEmailHandler <code class=\"docutils literal notranslate\"><span class=\"pre\">email_backend</span></code><a class=\"heading-anchor\" href=\"#updating-adminemailhandler-email-backend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The deprecated <code class=\"docutils literal notranslate\"><span class=\"pre\">email_backend</span></code> argument to the logging\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/logging/#django.utils.log.AdminEmailHandler\" title=\"django.utils.log.AdminEmailHandler\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AdminEmailHandler</span></code></a> can be replaced with a custom\n<a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MAILERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MAILERS</span></code></a> configuration, referring to it with the <code class=\"docutils literal notranslate\"><span class=\"pre\">using</span></code>\nargument.</p>\n<p>For example, if your settings include:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;mail_admins&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.utils.log.AdminEmailHandler&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;email_backend&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;third.party.EmailBackend&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"c1\"># ...</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>Replace that with:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">LOGGING</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;handlers&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;mail_admins&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;class&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;django.utils.log.AdminEmailHandler&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;using&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;admin-logging&quot;</span><span class=\"p\">,</span>  <span class=\"c1\"># defined in MAILERS</span>\n        <span class=\"p\">},</span>\n    <span class=\"p\">},</span>\n    <span class=\"c1\"># ...</span>\n<span class=\"p\">}</span>\n\n<span class=\"n\">MAILERS</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;default&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"o\">...</span><span class=\"p\">},</span>\n    <span class=\"s2\">&quot;admin-logging&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;third.party.EmailBackend&quot;</span><span class=\"p\">,</span>\n    <span class=\"p\">},</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n</section>","rootId":"migrating-email-to-mailers","toc":[{"title":"Migrating settings","anchor":"migrating-settings","children":[]},{"title":"Reusable library readiness","anchor":"reusable-library-readiness","children":[{"title":"Solving \"not available when MAILERS is defined\" errors","anchor":"solving-not-available-when-mailers-is-defined-errors","children":[]}]},{"title":"Replacing get_connection() and connection arguments","anchor":"replacing-get-connection-and-connection-arguments","children":[]},{"title":"Replacing fail_silently","anchor":"replacing-fail-silently","children":[]},{"title":"Migrating custom email backends","anchor":"migrating-custom-email-backends","children":[]},{"title":"Replacing auth_user and auth_password","anchor":"replacing-auth-user-and-auth-password","children":[]},{"title":"Updating AdminEmailHandler email_backend","anchor":"updating-adminemailhandler-email-backend","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"如何引导","url":"/zh-hans/6.1/howto/"}],"prev":{"docname":"howto/custom-shell","title":"如何自定义 shell 命令","url":"/zh-hans/6.1/howto/custom-shell/"},"next":{"docname":"faq/index","title":"Django FAQ","url":"/zh-hans/6.1/faq/"},"formats":{"html":"/zh-hans/6.1/howto/mailers-migration/","markdown":"/zh-hans/6.1/howto/mailers-migration.md","json":"/zh-hans/6.1/howto/mailers-migration.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/howto/mailers-migration.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/howto/mailers-migration/","inVersions":["6.1"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}