Exemple #1
0
    def test_file_sessions(self):
        """Make sure opening a connection creates a new file"""
        msg = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
        connection = mail.get_connection()
        connection.send_messages([msg])

        self.assertEqual(len(os.listdir(self.tmp_dir)), 1)
        with open(os.path.join(self.tmp_dir, os.listdir(self.tmp_dir)[0])) as fp:
            message = email.message_from_file(fp)
        self.assertEqual(message.get_content_type(), 'text/plain')
        self.assertEqual(message.get('subject'), 'Subject')
        self.assertEqual(message.get('from'), '*****@*****.**')
        self.assertEqual(message.get('to'), '*****@*****.**')

        connection2 = mail.get_connection()
        connection2.send_messages([msg])
        self.assertEqual(len(os.listdir(self.tmp_dir)), 2)

        connection.send_messages([msg])
        self.assertEqual(len(os.listdir(self.tmp_dir)), 2)

        msg.connection = mail.get_connection()
        self.assertTrue(connection.open())
        msg.send()
        self.assertEqual(len(os.listdir(self.tmp_dir)), 3)
        msg.send()
        self.assertEqual(len(os.listdir(self.tmp_dir)), 3)

        connection.close()
Exemple #2
0
    def test_connection_arg(self):
        """Test connection argument to send_mail(), et. al."""
        mail.outbox = []

        # Send using non-default connection
        connection = mail.get_connection('regressiontests.mail.custombackend.EmailBackend')
        send_mail('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], 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('regressiontests.mail.custombackend.EmailBackend')
        send_mass_mail([
                ('Subject1', 'Content1', '*****@*****.**', ['*****@*****.**']),
                ('Subject2', 'Content2', '*****@*****.**', ['*****@*****.**']),
            ], 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('regressiontests.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('regressiontests.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')
Exemple #3
0
 def test_message_cc_header(self):
     """
     Regression test for #7722
     """
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], cc=['*****@*****.**'])
     mail.get_connection().send_messages([email])
     message = self.get_the_message()
     self.assertStartsWith(message.as_string(), 'Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: [email protected]\nTo: [email protected]\nCc: [email protected]\nDate: ')
Exemple #4
0
 def test_custom_backend(self):
     """Test custom backend defined in this suite."""
     conn = mail.get_connection('regressiontests.mail.custombackend.EmailBackend')
     self.assertTrue(hasattr(conn, 'test_outbox'))
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], headers={'From': '*****@*****.**'})
     conn.send_messages([email])
     self.assertEqual(len(conn.test_outbox), 1)
Exemple #5
0
 def test_arbitrary_keyword(self):
     """
     Make sure that get_connection() accepts arbitrary keyword that might be
     used with custom backends.
     """
     c = mail.get_connection(fail_silently=True, foo='bar')
     self.assertTrue(c.fail_silently)
Exemple #6
0
 def test_console_stream_kwarg(self):
     """
     Test that the console backend can be pointed at an arbitrary stream.
     """
     s = StringIO()
     connection = mail.get_connection('djangocg.core.mail.backends.console.EmailBackend', stream=s)
     send_mail('Subject', 'Content', '*****@*****.**', ['*****@*****.**'], connection=connection)
     self.assertTrue(s.getvalue().startswith('Content-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nSubject: Subject\nFrom: [email protected]\nTo: [email protected]\nDate: '))
Exemple #7
0
 def test_send_many(self):
     email1 = EmailMessage('Subject', 'Content1', '*****@*****.**', ['*****@*****.**'])
     email2 = EmailMessage('Subject', 'Content2', '*****@*****.**', ['*****@*****.**'])
     num_sent = mail.get_connection().send_messages([email1, email2])
     self.assertEqual(num_sent, 2)
     messages = self.get_mailbox_content()
     self.assertEqual(len(messages), 2)
     self.assertEqual(messages[0].get_payload(), "Content1")
     self.assertEqual(messages[1].get_payload(), "Content2")
Exemple #8
0
 def test_send(self):
     email = EmailMessage('Subject', 'Content', '*****@*****.**', ['*****@*****.**'])
     num_sent = mail.get_connection().send_messages([email])
     self.assertEqual(num_sent, 1)
     message = self.get_the_message()
     self.assertEqual(message["subject"], "Subject")
     self.assertEqual(message.get_payload(), "Content")
     self.assertEqual(message["from"], "*****@*****.**")
     self.assertEqual(message.get_all("to"), ["*****@*****.**"])
Exemple #9
0
 def test_backend_arg(self):
     """Test backend argument of mail.get_connection()"""
     self.assertTrue(isinstance(mail.get_connection('djangocg.core.mail.backends.smtp.EmailBackend'), smtp.EmailBackend))
     self.assertTrue(isinstance(mail.get_connection('djangocg.core.mail.backends.locmem.EmailBackend'), locmem.EmailBackend))
     self.assertTrue(isinstance(mail.get_connection('djangocg.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend))
     self.assertTrue(isinstance(mail.get_connection('djangocg.core.mail.backends.console.EmailBackend'), console.EmailBackend))
     tmp_dir = tempfile.mkdtemp()
     try:
         self.assertTrue(isinstance(mail.get_connection('djangocg.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir), filebased.EmailBackend))
     finally:
         shutil.rmtree(tmp_dir)
     self.assertTrue(isinstance(mail.get_connection(), locmem.EmailBackend))
Exemple #10
0
def mass_mail_sending_view(request):
    m1 = mail.EmailMessage(
        "First Test message",
        "This is the first test email",
        "*****@*****.**",
        ["*****@*****.**", "*****@*****.**"],
    )
    m2 = mail.EmailMessage(
        "Second Test message",
        "This is the second test email",
        "*****@*****.**",
        ["*****@*****.**", "*****@*****.**"],
    )

    c = mail.get_connection()
    c.send_messages([m1, m2])

    return HttpResponse("Mail sent")
Exemple #11
0
 def get_connection(self, fail_silently=False):
     from djangocg.core.mail import get_connection
     if not self.connection:
         self.connection = get_connection(fail_silently=fail_silently)
     return self.connection