Ticket #17444: set_mail_to_header.2.diff

File set_mail_to_header.2.diff, 2.4 KB (added by Ethan Jucovy, 13 years ago)

Allow custom To headers

  • tests/regressiontests/mail/tests.py

     
    9696        message = email.message()
    9797        self.assertEqual(message['From'], 'from@example.com')
    9898
     99    def test_to_header(self):
     100        """
     101        Make sure we can manually set the To header (#17444)
     102        """
     103        email = EmailMessage('Subject', 'Content', 'bounce@example.com',
     104                             ['list-subscriber@example.com', 'list-subscriber2@example.com'],
     105                             headers={'To': 'mailing-list@example.com'})
     106        message = email.message()
     107        self.assertEqual(message['To'], 'mailing-list@example.com')
     108        self.assertEqual(email.to, ['list-subscriber@example.com', 'list-subscriber2@example.com'])
     109
     110        # If we don't set the To header manually, it should default to the `to` argument to the constructor
     111        email = EmailMessage('Subject', 'Content', 'bounce@example.com',
     112                             ['list-subscriber@example.com', 'list-subscriber2@example.com'])
     113        message = email.message()
     114        self.assertEqual(message['To'], 'list-subscriber@example.com, list-subscriber2@example.com')
     115        self.assertEqual(email.to, ['list-subscriber@example.com', 'list-subscriber2@example.com'])
     116
    99117    def test_multiple_message_call(self):
    100118        """
    101119        Regression for #13259 - Make sure that headers are not changed when
  • django/core/mail/message.py

     
    215215        msg = self._create_message(msg)
    216216        msg['Subject'] = self.subject
    217217        msg['From'] = self.extra_headers.get('From', self.from_email)
    218         msg['To'] = ', '.join(self.to)
     218        msg['To'] = self.extra_headers.get('To', ', '.join(self.to))
    219219        if self.cc:
    220220            msg['Cc'] = ', '.join(self.cc)
    221221
     
    227227        if 'message-id' not in header_names:
    228228            msg['Message-ID'] = make_msgid()
    229229        for name, value in self.extra_headers.items():
    230             if name.lower() == 'from':  # From is already handled
     230            if name.lower() in ('from', 'to'):  # From and To are already handled
    231231                continue
    232232            msg[name] = value
    233233        return msg
Back to Top