Ticket #5746: return_path.diff

File return_path.diff, 6.7 KB (added by wreese@…, 17 years ago)

Patch to add support for setting email return-path

  • django/core/mail.py

     
    182182        if not email_message.to:
    183183            return False
    184184        try:
    185             self.connection.sendmail(email_message.from_email,
     185            self.connection.sendmail(email_message.return_path,
    186186                    email_message.recipients(),
    187187                    email_message.message().as_string())
    188188        except:
     
    200200    encoding = None     # None => use settings default
    201201
    202202    def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
    203             connection=None, attachments=None, headers=None):
     203            connection=None, attachments=None, headers=None, return_path=None):
    204204        """
    205205        Initialise a single email message (which can be sent to multiple
    206206        recipients).
     
    212212        self.to = to or []
    213213        self.bcc = bcc or []
    214214        self.from_email = from_email or settings.DEFAULT_FROM_EMAIL
     215        self.return_path = return_path or self.from_email
    215216        self.subject = subject
    216217        self.body = body
    217218        self.attachments = attachments or []
  • tests/modeltests/test_client/views.py

     
    151151        ['first@example.com', 'second@example.com']).send()
    152152    return HttpResponse("Mail sent")
    153153
     154def mail_sending_view_with_return_path(request):
     155    EmailMessage(
     156        "Test message",
     157        "This is a test email",
     158        "from@example.com",
     159        ['first@example.com', 'second@example.com'],
     160        return_path='return_path@example.com').send()
     161    return HttpResponse("Mail sent")
     162
    154163def mass_mail_sending_view(request):
    155164    m1 = EmailMessage(
    156165        'First Test message',
     
    161170        'Second Test message',
    162171        'This is the second test email',
    163172        'from@example.com',
    164         ['second@example.com', 'third@example.com'])
     173        ['second@example.com', 'third@example.com'],
     174        return_path='return_path@example.com')
    165175
    166176    c = SMTPConnection()
    167177    c.send_messages([m1,m2])
  • tests/modeltests/test_client/models.py

     
    331331        self.assertEqual(mail.outbox[0].subject, 'Test message')
    332332        self.assertEqual(mail.outbox[0].body, 'This is a test email')
    333333        self.assertEqual(mail.outbox[0].from_email, 'from@example.com')
     334        self.assertEqual(mail.outbox[0].return_path, 'from@example.com')
    334335        self.assertEqual(mail.outbox[0].to[0], 'first@example.com')
    335336        self.assertEqual(mail.outbox[0].to[1], 'second@example.com')
    336337
     338    def test_mail_sending_with_return_path(self):
     339        "Test that mail is redirected to a dummy outbox during test setup (with return_path set correctly)"
     340   
     341        response = self.client.get('/test_client/mail_sending_view_with_return_path/')
     342        self.assertEqual(response.status_code, 200)
     343   
     344        self.assertEqual(len(mail.outbox), 1)
     345        self.assertEqual(mail.outbox[0].subject, 'Test message')
     346        self.assertEqual(mail.outbox[0].body, 'This is a test email')
     347        self.assertEqual(mail.outbox[0].from_email, 'from@example.com')
     348        self.assertEqual(mail.outbox[0].return_path, 'return_path@example.com')
     349        self.assertEqual(mail.outbox[0].to[0], 'first@example.com')
     350        self.assertEqual(mail.outbox[0].to[1], 'second@example.com')
     351
    337352    def test_mass_mail_sending(self):
    338353        "Test that mass mail is redirected to a dummy outbox during test setup"
    339354       
     
    344359        self.assertEqual(mail.outbox[0].subject, 'First Test message')
    345360        self.assertEqual(mail.outbox[0].body, 'This is the first test email')
    346361        self.assertEqual(mail.outbox[0].from_email, 'from@example.com')
     362        self.assertEqual(mail.outbox[0].return_path, 'from@example.com')
    347363        self.assertEqual(mail.outbox[0].to[0], 'first@example.com')
    348364        self.assertEqual(mail.outbox[0].to[1], 'second@example.com')
    349365
    350366        self.assertEqual(mail.outbox[1].subject, 'Second Test message')
    351367        self.assertEqual(mail.outbox[1].body, 'This is the second test email')
    352368        self.assertEqual(mail.outbox[1].from_email, 'from@example.com')
     369        self.assertEqual(mail.outbox[1].return_path, 'return_path@example.com')
    353370        self.assertEqual(mail.outbox[1].to[0], 'second@example.com')
    354371        self.assertEqual(mail.outbox[1].to[1], 'third@example.com')
    355372       
  • tests/modeltests/test_client/urls.py

     
    1717    (r'^session_view/$', views.session_view),
    1818    (r'^broken_view/$', views.broken_view),
    1919    (r'^mail_sending_view/$', views.mail_sending_view),
     20    (r'^mail_sending_view_with_return_path/$', views.mail_sending_view_with_return_path),
    2021    (r'^mass_mail_sending_view/$', views.mass_mail_sending_view)
    2122)
  • docs/email.txt

     
    6565    * ``auth_password``: The optional password to use to authenticate to the
    6666      SMTP server. If this isn't provided, Django will use the value of the
    6767      ``EMAIL_HOST_PASSWORD`` setting.
     68    * ``return_path``: An optional string used to specifiy the SMTP envelope
     69      sender. If this isn't provided, Django will use ``from_email`` instead.
    6870
    6971.. _smtplib docs: http://www.python.org/doc/current/lib/module-smtplib.html
    7072
     
    8183
    8284    (subject, message, from_email, recipient_list)
    8385
    84 ``fail_silently``, ``auth_user`` and ``auth_password`` have the same functions
    85 as in ``send_mail()``.
     86``fail_silently``, ``auth_user``, ``auth_password`` and ``return_path`` have the
     87same functions as in ``send_mail()``.
    8688
    8789Each separate element of ``datatuple`` results in a separate e-mail message.
    8890As in ``send_mail()``, recipients in the same ``recipient_list`` will all see
     
    245247      caller to ensure header names and values are in the correct format for
    246248      an e-mail message.
    247249
     250    * ``return_path``: A string used to specifiy the SMTP envelope
     251              sender. If this isn't provided, Django will use ``from_email`` instead.
     252
    248253For example::
    249254
    250255    email = EmailMessage('Hello', 'Body goes here', 'from@example.com',
Back to Top