Last active
October 2, 2019 02:54
-
-
Save lorne-luo/15b60e217bbcdcb57363d4552a81b1a2 to your computer and use it in GitHub Desktop.
reuse connection to send multiple emails in django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import mail | |
connection = mail.get_connection() | |
connection.open() | |
email1 = mail.EmailMessage( | |
'That’s your subject', | |
'That’s your message body', | |
'[email protected]', | |
['[email protected]'], | |
connection=connection, | |
) | |
email1.send() | |
email2 = mail.EmailMessage( | |
'That’s your subject #2', | |
'That’s your message body #2', | |
'[email protected]', | |
['[email protected]'], | |
) | |
email3 = mail.EmailMessage( | |
'That’s your subject #3', | |
'That’s your message body #3', | |
'[email protected]', | |
['[email protected]'], | |
) | |
connection.send_messages([email2, email3]) | |
connection.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
message1 = ('That’s your subject #1', | |
'That’s your message body #1', | |
'[email protected]', | |
['[email protected]', '[email protected]']) | |
message2 = ('That’s your subject #2', | |
'That’s your message body #2', | |
'[email protected]', | |
['[email protected]']) | |
message3 = ('That’s your subject #3', | |
'That’s your message body #3', | |
'[email protected]', | |
['[email protected]']) | |
send_mass_mail((message1, message2, message3), fail_silently=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.core import mail | |
from django.test import TestCase | |
class EmailTest(TestCase): | |
def test_send_email(self): | |
mail.send_mail( | |
'That’s your subject', 'That’s your message body', | |
'[email protected]', ['[email protected]'], | |
fail_silently=False, | |
) | |
self.assertEqual(len(mail.outbox), 1) | |
self.assertEqual(mail.outbox[0].subject, 'That’s your subject') | |
self.assertEqual(mail.outbox[0].body, 'That’s your message body') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment