forked from xenda/remail-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inbound.py
43 lines (37 loc) · 1.47 KB
/
inbound.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging, email, yaml
from models import store_email_and_attachment
from django.utils import simplejson as json
from google.appengine.ext import webapp, deferred
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.api.urlfetch import fetch
from google.appengine.api.urlfetch import Error as FetchError
settings = yaml.load(open('settings.yaml'))
def callback(message):
result = {'email':
{ 'raw': message.original.as_string(True),
'sender':message.sender,
'subject':message.subject,
'to': message.to,
'date':message.date
}
}
email = store_email_and_attachment(message, is_from_external=True)
if not email:
logging.error("Failed to save message: %s",
message.original.as_string(True))
response = fetch(settings['outbound_url'],
payload=json.dumps(result),
method="POST",
headers={
'Authorization': settings['api_key'],
'Content-Type': 'application/json'
},
deadline=10
)
logging.info(response.status_code)
if response.status_code != 200:
raise FetchError()
class InboundHandler(InboundMailHandler):
def receive(self, message):
logging.info("Received a message from: " + message.sender)
deferred.defer(callback, message, _queue='inbound')