本文整理汇总了Python中django.core.mail.backends.smtp.EmailBackend方法的典型用法代码示例。如果您正苦于以下问题:Python smtp.EmailBackend方法的具体用法?Python smtp.EmailBackend怎么用?Python smtp.EmailBackend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.mail.backends.smtp
的用法示例。
在下文中一共展示了smtp.EmailBackend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def clean(self):
cleaned_data = super().clean()
smtp_email_backend = EmailBackend(
host=cleaned_data.get('smtp_host'),
port=cleaned_data.get('smtp_port'),
username=cleaned_data.get('smtp_username'),
password=cleaned_data.get('smtp_password'),
use_tls=cleaned_data.get('smtp_use_tls'),
fail_silently=False,
use_ssl=cleaned_data.get('smtp_use_ssl'),
timeout=cleaned_data.get('smtp_timeout'),
ssl_keyfile=cleaned_data.get('smtp_ssl_keyfile'),
ssl_certfile=cleaned_data.get('smtp_ssl_certfile')
)
try:
smtp_email_backend.open()
except ConnectionRefusedError:
raise ValidationError(_('Connection refused'), code='connection_refused')
except SMTPAuthenticationError as err:
raise ValidationError(str(err), code='auth_error')
return cleaned_data
示例2: test_backend_arg
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_backend_arg(self):
"""Test backend argument of mail.get_connection()"""
self.assertIsInstance(mail.get_connection('django.core.mail.backends.smtp.EmailBackend'), smtp.EmailBackend)
self.assertIsInstance(
mail.get_connection('django.core.mail.backends.locmem.EmailBackend'),
locmem.EmailBackend
)
self.assertIsInstance(mail.get_connection('django.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend)
self.assertIsInstance(
mail.get_connection('django.core.mail.backends.console.EmailBackend'),
console.EmailBackend
)
with tempfile.TemporaryDirectory() as tmp_dir:
self.assertIsInstance(
mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir),
filebased.EmailBackend
)
self.assertIsInstance(mail.get_connection(), locmem.EmailBackend)
示例3: test_console_stream_kwarg
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_console_stream_kwarg(self):
"""
The console backend can be pointed at an arbitrary stream.
"""
s = StringIO()
connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
message = s.getvalue().split('\n' + ('-' * 79) + '\n')[0].encode()
self.assertMessageHasHeaders(message, {
('MIME-Version', '1.0'),
('Content-Type', 'text/plain; charset="utf-8"'),
('Content-Transfer-Encoding', '7bit'),
('Subject', 'Subject'),
('From', 'from@example.com'),
('To', 'to@example.com')})
self.assertIn(b'\nDate: ', message)
示例4: email_backend
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def email_backend(self):
return EmailBackend(**self.email_conf['connection'])
示例5: get_smtp_connection
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def get_smtp_connection(self):
"""Get SMTP connection.
:return: SMTP connection
"""
if not self.smtp_host:
return None
options = {
'use_ssl': self.smtp_use_ssl
}
# add host / port infos
if ':' in self.smtp_host:
host, port = self.smtp_host.split(':')
options['host'] = host
options['port'] = int(port)
else:
options['host'] = self.smtp_host
# add cred infos
if self.smtp_username:
options['username'] = self.smtp_username
if self.smtp_password:
options['password'] = self.smtp_password
return EmailBackend(**options)
示例6: test_dummy_backend
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_dummy_backend(self):
"""
Make sure that dummy backends returns correct number of sent messages
"""
connection = dummy.EmailBackend()
email = EmailMessage(
'Subject', 'Content', 'bounce@example.com', ['to@example.com'],
headers={'From': 'from@example.com'},
)
self.assertEqual(connection.send_messages([email, email, email]), 3)
示例7: test_custom_backend
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_custom_backend(self):
"""Test custom backend defined in this suite."""
conn = mail.get_connection('mail.custombackend.EmailBackend')
self.assertTrue(hasattr(conn, 'test_outbox'))
email = EmailMessage(
'Subject', 'Content', 'bounce@example.com', ['to@example.com'],
headers={'From': 'from@example.com'},
)
conn.send_messages([email])
self.assertEqual(len(conn.test_outbox), 1)
示例8: test_connection_arg
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_connection_arg(self):
"""Test connection argument to send_mail(), et. al."""
mail.outbox = []
# Send using non-default connection
connection = mail.get_connection('mail.custombackend.EmailBackend')
send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
self.assertEqual(mail.outbox, [])
self.assertEqual(len(connection.test_outbox), 1)
self.assertEqual(connection.test_outbox[0].subject, 'Subject')
connection = mail.get_connection('mail.custombackend.EmailBackend')
send_mass_mail([
('Subject1', 'Content1', 'from1@example.com', ['to1@example.com']),
('Subject2', 'Content2', 'from2@example.com', ['to2@example.com']),
], connection=connection)
self.assertEqual(mail.outbox, [])
self.assertEqual(len(connection.test_outbox), 2)
self.assertEqual(connection.test_outbox[0].subject, 'Subject1')
self.assertEqual(connection.test_outbox[1].subject, 'Subject2')
connection = mail.get_connection('mail.custombackend.EmailBackend')
mail_admins('Admin message', 'Content', connection=connection)
self.assertEqual(mail.outbox, [])
self.assertEqual(len(connection.test_outbox), 1)
self.assertEqual(connection.test_outbox[0].subject, '[Django] Admin message')
connection = mail.get_connection('mail.custombackend.EmailBackend')
mail_managers('Manager message', 'Content', connection=connection)
self.assertEqual(mail.outbox, [])
self.assertEqual(len(connection.test_outbox), 1)
self.assertEqual(connection.test_outbox[0].subject, '[Django] Manager message')
示例9: test_locmem_shared_messages
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_locmem_shared_messages(self):
"""
Make sure that the locmen backend populates the outbox.
"""
connection = locmem.EmailBackend()
connection2 = locmem.EmailBackend()
email = EmailMessage(
'Subject', 'Content', 'bounce@example.com', ['to@example.com'],
headers={'From': 'from@example.com'},
)
connection.send_messages([email])
connection2.send_messages([email])
self.assertEqual(len(mail.outbox), 2)
示例10: test_email_authentication_use_settings
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_email_authentication_use_settings(self):
backend = smtp.EmailBackend()
self.assertEqual(backend.username, 'not empty username')
self.assertEqual(backend.password, 'not empty password')
示例11: test_email_authentication_override_settings
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_email_authentication_override_settings(self):
backend = smtp.EmailBackend(username='username', password='password')
self.assertEqual(backend.username, 'username')
self.assertEqual(backend.password, 'password')
示例12: test_email_disabled_authentication
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_email_disabled_authentication(self):
backend = smtp.EmailBackend(username='', password='')
self.assertEqual(backend.username, '')
self.assertEqual(backend.password, '')
示例13: test_auth_attempted
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_auth_attempted(self):
"""
Opening the backend with non empty username/password tries
to authenticate against the SMTP server.
"""
backend = smtp.EmailBackend(
username='not empty username', password='not empty password')
with self.assertRaisesMessage(SMTPException, 'SMTP AUTH extension not supported by server.'):
with backend:
pass
示例14: test_server_open
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_server_open(self):
"""
open() returns whether it opened a connection.
"""
backend = smtp.EmailBackend(username='', password='')
self.assertIsNone(backend.connection)
opened = backend.open()
backend.close()
self.assertIs(opened, True)
示例15: test_server_login
# 需要导入模块: from django.core.mail.backends import smtp [as 别名]
# 或者: from django.core.mail.backends.smtp import EmailBackend [as 别名]
def test_server_login(self):
"""
Even if the Python SMTP server doesn't support authentication, the
login process starts and the appropriate exception is raised.
"""
class CustomEmailBackend(smtp.EmailBackend):
connection_class = FakeAUTHSMTPConnection
backend = CustomEmailBackend(username='username', password='password')
with self.assertRaises(SMTPAuthenticationError):
with backend:
pass