Пример #1
0
 def test_email_ssl_attempts_ssl_connection(self):
     backend = smtp.EmailBackend()
     self.assertTrue(backend.use_ssl)
     try:
         self.assertRaises(SSLError, backend.open)
     finally:
         backend.close()
Пример #2
0
    def __init__(self, host, username, password, file_excel):

        self.file_excel = file_excel
        self.columns = ["to", "attachments", "subject", "messages"]
        self.smtp_backend = smtp.EmailBackend(
            host=host, port=587, username=username, password=password,
            use_tls=True, fail_silently=True, timeout=5)
Пример #3
0
 def test_email_tls_attempts_starttls(self):
     backend = smtp.EmailBackend()
     self.assertTrue(backend.use_tls)
     try:
         self.assertRaisesMessage(SMTPException,
             'STARTTLS extension not supported by server.', backend.open)
     finally:
         backend.close()
Пример #4
0
 def test_server_stopped(self):
     """
     Test that closing the backend while the SMTP server is stopped doesn't
     raise an exception.
     """
     backend = smtp.EmailBackend(username='', password='')
     backend.open()
     self.server.stop()
     backend.close()
Пример #5
0
 def test_server_open(self):
     """
     Test that open() tells us whether it opened a connection.
     """
     backend = smtp.EmailBackend(username='', password='')
     self.assertFalse(backend.connection)
     opened = backend.open()
     backend.close()
     self.assertTrue(opened)
Пример #6
0
 def test_auth_attempted(self):
     """
     Test that opening the backend with non empty username/password tries
     to authenticate against the SMTP server.
     """
     backend = smtp.EmailBackend(
         username='******', password='******')
     self.assertRaisesMessage(SMTPException,
         'SMTP AUTH extension not supported by server.', backend.open)
Пример #7
0
 def test_server_stopped(self):
     """
     Test that closing the backend while the SMTP server is stopped doesn't
     raise an exception.
     """
     backend = smtp.EmailBackend(username='', password='')
     backend.open()
     self.server.stop()
     try:
         backend.close()
     except Exception as e:
         self.fail("close() unexpectedly raised an exception: %s" % e)
Пример #8
0
 def send_emails(self, messages=()):
     """Sends all emails in a single session. This ensures that the system does not need to connect to the smtp
     server many times when sending multiple emails, which should save on bandwidth and cpu cycles."""
     if len(messages) is not 0:
         if messages != self.__mail_list:
             self.__mail_list = messages
         else:
             self.__mail_list = tuple(self.__mail_list)
         server = smtp.EmailBackend(host=self.__smtp_server, port=self.__smtp_port, username=self.__email,
                                    password=self.__smtp_pass, use_tls=True)  # Set up a secure connection.
         #print server
         server.send_messages(self.__mail_list)  # Send all emails in one session.
         server.close()  #Close the session
     self.__mail_list = []
Пример #9
0
    def handle(self, *args, **options):
        subject = options.get('subject')

        to_email = options.get('to')
        if options.get('litmus'):
            to_list = [to_email, '*****@*****.**']
        else:
            to_list = [to_email]

        base_template = options.get('template')

        user = None
        try:
            UserModel = get_user_model()
            user = UserModel.objects.get(email=to_email)
        except:
            pass

        context = {
            'site': Site.objects.get_current(),
            'protocol': 'http',
            'user': user,
        }

        json_template = '%s.json' % base_template
        try:
            context_json = json.loads(render_to_string(json_template))
            context.update(context_json)
        except TemplateDoesNotExist:
            pass

        context_json = options.get('context')
        context_json = json.loads(context_json)
        context.update(context_json)

        backend = smtp.EmailBackend(host='smtp.mandrillapp.com',
                                    port=587,
                                    username='******',
                                    password='******')

        send_html_email(base_template,
                        context,
                        subject,
                        settings.DEFAULT_FROM_EMAIL,
                        to_list,
                        fail_silently=False,
                        connection=backend)

        print 'Email sent to %s' % ', '.join(to_list)
Пример #10
0
 def test_email_authentication_override_settings(self):
     backend = smtp.EmailBackend(username='******', password='******')
     self.assertEqual(backend.username, 'username')
     self.assertEqual(backend.password, 'password')
Пример #11
0
 def test_email_disabled_authentication(self):
     backend = smtp.EmailBackend(username='', password='')
     self.assertEqual(backend.username, '')
     self.assertEqual(backend.password, '')
Пример #12
0
 def test_email_ssl_keyfile_use_settings(self):
     backend = smtp.EmailBackend()
     self.assertEqual(backend.ssl_keyfile, 'foo')
Пример #13
0
 def test_email_authentication_use_settings(self):
     backend = smtp.EmailBackend()
     self.assertEqual(backend.username, 'not empty username')
     self.assertEqual(backend.password, 'not empty password')
Пример #14
0
 def test_email_ssl_override_settings(self):
     backend = smtp.EmailBackend(use_ssl=False)
     self.assertFalse(backend.use_ssl)
Пример #15
0
 def test_email_ssl_default_disabled(self):
     backend = smtp.EmailBackend()
     self.assertFalse(backend.use_ssl)
Пример #16
0
 def test_email_ssl_use_settings(self):
     backend = smtp.EmailBackend()
     self.assertTrue(backend.use_ssl)
Пример #17
0
 def test_email_ssl_keyfile_override_settings(self):
     backend = smtp.EmailBackend(ssl_keyfile='bar')
     self.assertEqual(backend.ssl_keyfile, 'bar')
Пример #18
0
 def test_email_ssl_keyfile_default_disabled(self):
     backend = smtp.EmailBackend()
     self.assertEqual(backend.ssl_keyfile, None)
Пример #19
0
#        host='localhost',
#        port=465,
#        username='******',
#        password='',
#        use_tls=False,
#        fail_silently=False,
#        use_ssl=True,
#        timeout=30,
#        ssl_keyfile=None,
#        ssl_certfile=None, )
backend = smtp.EmailBackend(
    host='smtp.yandex.ru',
    port=465,
    username=proj.settings.EMAIL_HOST_USER,
    password=proj.settings.EMAIL_HOST_PASSWORD,
    use_tls=False,
    fail_silently=False,
    use_ssl=True,
    timeout=30,
    ssl_keyfile=None,
    ssl_certfile=None,
)


def get_and_render_template(template_name, **kwargs):
    try:
        template = EmailTemplate.objects.get(name=template_name, )
        html_content = template.get_template()
        t = Template(html_content)
        c = Context(kwargs)
        return t.render(c)
    except EmailTemplate.DoesNotExist:
Пример #20
0
 def test_email_timeout_override_settings(self):
     backend = smtp.EmailBackend()
     self.assertEqual(backend.timeout, 10)