diff --git a/django/core/mail/backends/smtp.py b/django/core/mail/backends/smtp.py index bb184ab3125..3ee283b5f1f 100644 --- a/django/core/mail/backends/smtp.py +++ b/django/core/mail/backends/smtp.py @@ -18,8 +18,14 @@ class EmailBackend(BaseEmailBackend): super(EmailBackend, self).__init__(fail_silently=fail_silently) self.host = host or settings.EMAIL_HOST self.port = port or settings.EMAIL_PORT - self.username = username or settings.EMAIL_HOST_USER - self.password = password or settings.EMAIL_HOST_PASSWORD + if username is None: + self.username = settings.EMAIL_HOST_USER + else: + self.username = username + if password is None: + self.password = settings.EMAIL_HOST_PASSWORD + else: + self.password = password if use_tls is None: self.use_tls = settings.EMAIL_USE_TLS else: diff --git a/tests/regressiontests/mail/tests.py b/tests/regressiontests/mail/tests.py index 5d5f52b6cdc..08eb7b9f455 100644 --- a/tests/regressiontests/mail/tests.py +++ b/tests/regressiontests/mail/tests.py @@ -635,3 +635,24 @@ class SMTPBackendTests(BaseEmailBackendTests, TestCase): def get_mailbox_content(self): return self.server.get_sink() + + @override_settings(EMAIL_HOST_USER="not empty username", + EMAIL_HOST_PASSWORD="not empty password") + def test_email_authentication_use_settings(self): + backend = smtp.EmailBackend() + self.assertEqual(backend.username, 'not empty username') + self.assertEqual(backend.password, 'not empty password') + + @override_settings(EMAIL_HOST_USER="not empty username", + EMAIL_HOST_PASSWORD="not empty password") + def test_email_authentication_override_settings(self): + backend = smtp.EmailBackend(username='username', password='password') + self.assertEqual(backend.username, 'username') + self.assertEqual(backend.password, 'password') + + @override_settings(EMAIL_HOST_USER="not empty username", + EMAIL_HOST_PASSWORD="not empty password") + def test_email_disabled_authentication(self): + backend = smtp.EmailBackend(username='', password='') + self.assertEqual(backend.username, '') + self.assertEqual(backend.password, '')