1from django.core import mail
2connection = mail.get_connection() # Use default email connection
3messages = get_notification_email()
4connection.send_messages(messages)
5
1from django.core.mail import EmailMultiAlternatives
2
3subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
4text_content = 'This is an important message.'
5html_content = '<p>This is an <strong>important</strong> message.</p>'
6msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
7msg.attach_alternative(html_content, "text/html")
8msg.send()
9
1from django.core import mail
2connection = mail.get_connection()
3
4# Manually open the connection
5connection.open()
6
7# Construct an email message that uses the connection
8email1 = mail.EmailMessage(
9 'Hello',
10 'Body goes here',
11 'from@example.com',
12 ['to1@example.com'],
13 connection=connection,
14)
15email1.send() # Send the email
16
17# Construct two more messages
18email2 = mail.EmailMessage(
19 'Hello',
20 'Body goes here',
21 'from@example.com',
22 ['to2@example.com'],
23)
24email3 = mail.EmailMessage(
25 'Hello',
26 'Body goes here',
27 'from@example.com',
28 ['to3@example.com'],
29)
30
31# Send the two emails in a single call -
32connection.send_messages([email2, email3])
33# The connection was already open so send_messages() doesn't close it.
34# We need to manually close the connection.
35connection.close()
36
1msg = EmailMessage(subject, html_content, from_email, [to])
2msg.content_subtype = "html" # Main content is now text/html
3msg.send()
4
1from django.core import mail
2
3with mail.get_connection() as connection:
4 mail.EmailMessage(
5 subject1, body1, from1, [to1],
6 connection=connection,
7 ).send()
8 mail.EmailMessage(
9 subject2, body2, from2, [to2],
10 connection=connection,
11 ).send()
12
1EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
2EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
3
1from django.core.mail import EmailMessage
2
3email = EmailMessage(
4 'Hello',
5 'Body goes here',
6 'from@example.com',
7 ['to1@example.com', 'to2@example.com'],
8 ['bcc@example.com'],
9 reply_to=['another@example.com'],
10 headers={'Message-ID': 'foo'},
11)
12