Skip to content

Commit

Permalink
Merge pull request #14 from bosonogi/no-full-from-field
Browse files Browse the repository at this point in the history
Support the new `origfrom` message attribute
  • Loading branch information
mc706 committed Jun 16, 2016
2 parents 195c760 + 2b241d4 commit 10c1371
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ Attributes:
* `subject` : message subject line
* `time` : message delivery time
* `seconds_ago` : number of seconds between time of delivery and time of request
* `fromshort` : short from field
* `fromfull`: full from field
* `origfrom` : Original from field
* `ip` : ip address the email was sent from
* `been_read` : boolean if messsage has been opened
* `headers` : only available after `get_message()`, shows the message headers
Expand Down
11 changes: 11 additions & 0 deletions pymailinator/tests/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import unittest
import os
from collections import defaultdict

from pymailinator import wrapper


Expand Down Expand Up @@ -175,6 +177,15 @@ def tests_too_many_request(self):
mock_server = MockServerTooManyRequests(4, get_mailbox)
self.test_successful_mailbox(mailbox=mock_server.get)

def test_origfrom_field(self):
mock_data = defaultdict(str)
mock_data['origfrom'] = 'Mock Name <mock@domain.com>'
mock_message = wrapper.Message(None, mock_data)

# Backwards compatible message objects.
self.assertEqual(mock_message.fromshort, 'Mock Name')
self.assertEqual(mock_message.fromfull, 'mock@domain.com')


if __name__ == '__main__':
unittest.main()
14 changes: 12 additions & 2 deletions pymailinator/wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from time import sleep
from email.utils import parseaddr, formataddr

try:
from urllib.request import urlopen
Expand Down Expand Up @@ -44,9 +45,18 @@ def __init__(self, token, data):
self.time = data['time']
self.to = data['to']
self.seconds_ago = data['seconds_ago']
self.fromfull = data['fromfull']
self.fromshort = data['from']
self.ip = data['ip']

try:
self.origfrom = data['origfrom']
# Support old Message attributes
self.fromshort, self.fromfull = parseaddr(self.origfrom)
except KeyError:
# Try the old data model
self.fromfull = data['fromfull']
self.fromshort = data['from']
self.origfrom = formataddr((self.fromshort, self.fromfull))

self.headers = {}
self.body = ""

Expand Down

0 comments on commit 10c1371

Please sign in to comment.