95
101
>>> message['From']
96
102
'from@example.com'
104
# Regression for #13259 - Make sure that headers are not changed
105
# when calling EmailMessage.message()
106
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
107
>>> message = email.message()
110
>>> message = email.message()
114
# Regression for #11144 - When a to/from/cc header contains unicode,
115
# make sure the email addresses are parsed correctly (especially
116
# with regards to commas)
117
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Firstname Sürname" <to@example.com>','other@example.com'])
118
>>> email.message()['To']
119
'=?utf-8?q?Firstname_S=C3=BCrname?= <to@example.com>, other@example.com'
121
>>> email = EmailMessage('Subject', 'Content', 'from@example.com', ['"Sürname, Firstname" <to@example.com>','other@example.com'])
122
>>> email.message()['To']
123
'=?utf-8?q?S=C3=BCrname=2C_Firstname?= <to@example.com>, other@example.com'
125
# Regression for #6918 - When a header contains unicode,
126
# make sure headers can be set with a different encoding than utf-8
127
>>> email = EmailMessage('Message from Firstname Sürname', 'Content', 'from@example.com', ['"Sürname, Firstname" <to@example.com>','other@example.com'])
128
>>> email.encoding = 'iso-8859-1'
129
>>> email.message()['To']
130
'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>, other@example.com'
131
>>> email.message()['Subject'].encode()
132
u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
134
# Make sure headers can be set with a different encoding than utf-8 in SafeMIMEMultipart as well
135
>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
136
>>> subject, from_email, to = 'hello', 'from@example.com', '"Sürname, Firstname" <to@example.com>'
137
>>> text_content = 'This is an important message.'
138
>>> html_content = '<p>This is an <strong>important</strong> message.</p>'
139
>>> msg = EmailMultiAlternatives('Message from Firstname Sürname', text_content, from_email, [to], headers=headers)
140
>>> msg.attach_alternative(html_content, "text/html")
141
>>> msg.encoding = 'iso-8859-1'
142
>>> msg.message()['To']
143
'=?iso-8859-1?q?S=FCrname=2C_Firstname?= <to@example.com>'
144
>>> msg.message()['Subject'].encode()
145
u'=?iso-8859-1?q?Message_from_Firstname_S=FCrname?='
147
# Regression for #12791 - Encode body correctly with other encodings than utf-8
148
>>> email = EmailMessage('Subject', 'Firstname Sürname is a great guy.', 'from@example.com', ['other@example.com'])
149
>>> email.encoding = 'iso-8859-1'
150
>>> message = email.message()
151
>>> message.as_string()
152
'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\nSubject: Subject\nFrom: from@example.com\nTo: other@example.com\nDate: ...\nMessage-ID: <...>\n\nFirstname S=FCrname is a great guy.'
154
# Make sure MIME attachments also works correctly with other encodings than utf-8
155
>>> text_content = 'Firstname Sürname is a great guy.'
156
>>> html_content = '<p>Firstname Sürname is a <strong>great</strong> guy.</p>'
157
>>> msg = EmailMultiAlternatives('Subject', text_content, 'from@example.com', ['to@example.com'])
158
>>> msg.encoding = 'iso-8859-1'
159
>>> msg.attach_alternative(html_content, "text/html")
160
>>> msg.message().get_payload(0).as_string()
161
'Content-Type: text/plain; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\nFirstname S=FCrname is a great guy.'
162
>>> msg.message().get_payload(1).as_string()
163
'Content-Type: text/html; charset="iso-8859-1"\nMIME-Version: 1.0\nContent-Transfer-Encoding: quoted-printable\n\n<p>Firstname S=FCrname is a <strong>great</strong> guy.</p>'
98
165
# Handle attachments within an multipart/alternative mail correctly (#9367)
99
166
# (test is not as precise/clear as it could be w.r.t. email tree structure,
100
167
# but it's good enough.)
102
168
>>> headers = {"Date": "Fri, 09 Nov 2001 01:08:47 -0000", "Message-ID": "foo"}
103
169
>>> subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
104
170
>>> text_content = 'This is an important message.'
138
204
JVBERi0xLjQuJS4uLg==
207
# Make sure that the console backend writes to stdout by default
208
>>> connection = console.EmailBackend()
209
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
210
>>> connection.send_messages([email])
211
Content-Type: text/plain; charset="utf-8"
213
Content-Transfer-Encoding: quoted-printable
215
From: from@example.com
221
-------------------------------------------------------------------------------
224
# Test that the console backend can be pointed at an arbitrary stream
226
>>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend', stream=s)
227
>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
229
>>> print s.getvalue()
230
Content-Type: text/plain; charset="utf-8"
232
Content-Transfer-Encoding: quoted-printable
234
From: from@example.com
240
-------------------------------------------------------------------------------
242
# Make sure that dummy backends returns correct number of sent messages
243
>>> connection = dummy.EmailBackend()
244
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
245
>>> connection.send_messages([email, email, email])
248
# Make sure that locmen backend populates the outbox
250
>>> connection = locmem.EmailBackend()
251
>>> email1 = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
252
>>> email2 = EmailMessage('Subject 2', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
253
>>> connection.send_messages([email1, email2])
257
>>> mail.outbox[0].subject
259
>>> mail.outbox[1].subject
262
# Make sure that multiple locmem connections share mail.outbox
264
>>> connection1 = locmem.EmailBackend()
265
>>> connection2 = locmem.EmailBackend()
266
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
267
>>> connection1.send_messages([email])
269
>>> connection2.send_messages([email])
274
# Make sure that the file backend write to the right location
275
>>> tmp_dir = tempfile.mkdtemp()
276
>>> connection = filebased.EmailBackend(file_path=tmp_dir)
277
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
278
>>> connection.send_messages([email])
280
>>> len(os.listdir(tmp_dir))
282
>>> print open(os.path.join(tmp_dir, os.listdir(tmp_dir)[0])).read()
283
Content-Type: text/plain; charset="utf-8"
285
Content-Transfer-Encoding: quoted-printable
287
From: from@example.com
293
-------------------------------------------------------------------------------
295
>>> connection2 = filebased.EmailBackend(file_path=tmp_dir)
296
>>> connection2.send_messages([email])
298
>>> len(os.listdir(tmp_dir))
300
>>> connection.send_messages([email])
302
>>> len(os.listdir(tmp_dir))
304
>>> email.connection = filebased.EmailBackend(file_path=tmp_dir)
305
>>> connection_created = connection.open()
306
>>> num_sent = email.send()
307
>>> len(os.listdir(tmp_dir))
309
>>> num_sent = email.send()
310
>>> len(os.listdir(tmp_dir))
312
>>> connection.close()
313
>>> shutil.rmtree(tmp_dir)
315
# Make sure that get_connection() accepts arbitrary keyword that might be
316
# used with custom backends.
317
>>> c = mail.get_connection(fail_silently=True, foo='bar')
321
# Test custom backend defined in this suite.
322
>>> conn = mail.get_connection('regressiontests.mail.custombackend.EmailBackend')
323
>>> hasattr(conn, 'test_outbox')
325
>>> email = EmailMessage('Subject', 'Content', 'bounce@example.com', ['to@example.com'], headers={'From': 'from@example.com'})
326
>>> conn.send_messages([email])
328
>>> len(conn.test_outbox)
331
# Test backend argument of mail.get_connection()
332
>>> isinstance(mail.get_connection('django.core.mail.backends.smtp.EmailBackend'), smtp.EmailBackend)
334
>>> isinstance(mail.get_connection('django.core.mail.backends.locmem.EmailBackend'), locmem.EmailBackend)
336
>>> isinstance(mail.get_connection('django.core.mail.backends.dummy.EmailBackend'), dummy.EmailBackend)
338
>>> isinstance(mail.get_connection('django.core.mail.backends.console.EmailBackend'), console.EmailBackend)
340
>>> tmp_dir = tempfile.mkdtemp()
341
>>> isinstance(mail.get_connection('django.core.mail.backends.filebased.EmailBackend', file_path=tmp_dir), filebased.EmailBackend)
343
>>> shutil.rmtree(tmp_dir)
344
>>> isinstance(mail.get_connection(), locmem.EmailBackend)
347
# Test connection argument of send_mail() et al
348
>>> connection = mail.get_connection('django.core.mail.backends.console.EmailBackend')
349
>>> send_mail('Subject', 'Content', 'from@example.com', ['to@example.com'], connection=connection)
350
Content-Type: text/plain; charset="utf-8"
352
Content-Transfer-Encoding: quoted-printable
354
From: from@example.com
360
-------------------------------------------------------------------------------
364
... ('Subject1', 'Content1', 'from1@example.com', ['to1@example.com']),
365
... ('Subject2', 'Content2', 'from2@example.com', ['to2@example.com'])
366
... ], connection=connection)
367
Content-Type: text/plain; charset="utf-8"
369
Content-Transfer-Encoding: quoted-printable
371
From: from1@example.com
377
-------------------------------------------------------------------------------
378
Content-Type: text/plain; charset="utf-8"
380
Content-Transfer-Encoding: quoted-printable
382
From: from2@example.com
388
-------------------------------------------------------------------------------
391
>>> mail_admins('Subject', 'Content', connection=connection)
392
Content-Type: text/plain; charset="utf-8"
394
Content-Transfer-Encoding: quoted-printable
395
Subject: [Django] Subject
397
To: nobody@example.com
402
-------------------------------------------------------------------------------
404
>>> mail_managers('Subject', 'Content', connection=connection)
405
Content-Type: text/plain; charset="utf-8"
407
Content-Transfer-Encoding: quoted-printable
408
Subject: [Django] Subject
410
To: nobody@example.com
415
-------------------------------------------------------------------------------
417
>>> settings.ADMINS = old_admins
418
>>> settings.MANAGERS = old_managers