1. Matthew Frazier
  2. cartero

Commits

Matthew Frazier  committed 8f061c3

improvements to the encoding system

  • Participants
  • Parent commits f7bf834
  • Branches default

Comments (0)

Files changed (3)

File cartero/encoding.py

View file
  • Ignore whitespace
     mime['To'] = ', '.join(unicode(a) for a in msg.to)
     mime['From'] = unicode(msg.sender)
     mime['Subject'] = msg.subject
-    mime['Date'] = utils.formatdate()
-    mime['Message-ID'] = make_msgid(hex(id(msg)))
+    if 'Date' not in msg.headers:
+        mime['Date'] = utils.formatdate()
+    if 'Message-ID' not in msg.headers:
+        mime['Message-ID'] = make_msgid(hex(id(msg)))
     for key, value in msg.headers.items():
         mime[key] = value
 
     Inheriting the class makes these methods available, allowing
     your sender to be insulated from changes in the MIME API.
     """
-    options = Options(
-        text_encoding = Encoding()
-    )
+    options = {
+        'text_encoding': Encoding()
+    }
     
     def encode_message(self, msg):
         """

File cartero/message.py

View file
  • Ignore whitespace
     if msg.subject:
         lines.append(u'Subject: %s' % msg.subject)
     if msg.headers:
-        lines.extend(u'[Header] %s: %s' % (k, v) for (k, v) in msg.headers)
+        lines.extend(u'[Header] %s: %s' % (k, v) for (k, v)
+                                          in msg.headers.items())
     if msg.text:
         lines.append(u'\n== Text ==')
         lines.append(msg.text)

File tests/test-encoding.py

View file
  • Ignore whitespace
         assert (payload[1]['Content-Disposition'] ==
                 'attachment; filename="archivo.txt"')
         assert payload[1].get_payload() == QUOTED_PRINTABLE
+    
+    def test_explicit_date_and_msgid(self):
+        msg = Message()
+        msg.text = u'Hello, world!'
+        msg.sender = u'foo@example.com'
+        msg.to.append(u'bar@example.net')
+        msg.subject = u'Hello'
+        msg.headers['Date'] = 'right now'
+        msg.headers['Message-ID'] = '382y7ghr9uqhnuf'
+        
+        mime = cartero.encoding.build_mime(msg)
+        assert mime.get_all('Date') == ['right now']
+        assert mime.get_all('Message-ID') == ['382y7ghr9uqhnuf']
+
+
+class TestMIMEEncodingSender(object):
+    def test_sender(self):
+        text = (u'Get to Coruscant as soon as possible, and don\'t tell '
+                u'anyone, even General Cracken.')
+        msg = Message()
+        msg.sender = u'Wedge Antilles <wedge.antilles@starfighter.gffa>'
+        msg.to = [u'Tycho Celchu <tycho.celchu@starfighter.gffa>']
+        msg.subject = u'Secret Rogue Squadron plans'
+        msg.text = text
+        msg.headers['Date'] = 'right now'
+        msg.headers['Message-ID'] = '382y7ghr9uqhnuf'
+        
+        sdr = cartero.encoding.MIMEEncodingSender()
+        assert (cartero.encoding.build_mime(msg).as_string() ==
+                sdr.encode_message(msg))