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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import base64
import email
import json
import re
import urllib
import uuid
from email.policy import default as email_policy_default
from email.utils import getaddresses
from functools import partial
from trytond.config import config
from trytond.model import ModelSQL, ModelView, fields, sequence_ordered
from trytond.pool import Pool
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.url import http_host
if config.getboolean('inbound_email', 'filestore', default=True):
file_id = 'data_id'
store_prefix = config.get('inbound_email', 'store_prefix', default=None)
else:
file_id = store_prefix = None
class Inbox(ModelSQL, ModelView):
"Inbound Email Inbox"
__name__ = 'inbound.email.inbox'
name = fields.Char("Name", required=True)
identifier = fields.Char("Identifier", readonly=True)
endpoint = fields.Function(
fields.Char(
"Endpoint",
help="The URL where the emails must be posted."),
'on_change_with_endpoint')
rules = fields.One2Many(
'inbound.email.rule', 'inbox', "Rules",
help="The action of the first matching line is run.")
@classmethod
def __setup__(cls):
super().__setup__()
cls._buttons.update(
new_identifier={
'icon': 'tryton-refresh',
},
)
@fields.depends('identifier')
def on_change_with_endpoint(self, name=None):
if self.identifier:
url_part = {
'identifier': self.identifier,
'database_name': Transaction().database.name,
}
return http_host() + (
urllib.parse.quote(
'/%(database_name)s/inbound_email/inbox/%(identifier)s'
% url_part))
@classmethod
@ModelView.button
def new_identifier(cls, inboxes):
for inbox in inboxes:
if inbox.identifier:
inbox.identifier = None
else:
inbox.identifier = uuid.uuid4().hex
cls.save(inboxes)
def process(self, email_):
assert email_.inbox == self
for rule in self.rules:
if rule.match(email_.as_dict()):
email_.rule = rule
rule.run(email_)
return
def _email_text(message, type_='plain'):
if message.get_content_maintype() != 'multipart':
return message.get_payload()
for part in message.walk():
if part.get_content_type() == f'text/{type_}':
return part.get_payload()
def _email_attachments(message):
if message.get_content_maintype() != 'multipart':
return
for i, part in enumerate(message.walk()):
if part.get_content_maintype() == 'multipart':
continue
if 'attachment' not in part.get('Content-Disposition', '').lower():
continue
filename = part.get_filename()
yield {
'filename': filename,
'type': part.get_content_type(),
'data': part.get_payload(decode=True),
}
class Email(ModelSQL, ModelView):
"Inbound Email"
__name__ = 'inbound.email'
inbox = fields.Many2One(
'inbound.email.inbox', "Inbox",
required=True, readonly=True, ondelete='CASCADE')
data = fields.Binary(
"Data", file_id=file_id, store_prefix=store_prefix,
required=True, readonly=True)
data_id = fields.Char("Data ID", readonly=True)
data_type = fields.Selection([
('mailchimp', "Mailchimp"),
('mailpace', "MailPace"),
('postmark', "Postmark"),
('raw', "Raw"),
('sendgrid', "SendGrid"),
], "Data Type", required=True, readonly=True)
rule = fields.Many2One('inbound.email.rule', "Rule", readonly=True)
result = fields.Reference("Result", selection='get_models', readonly=True)
@classmethod
def __setup__(cls):
super().__setup__()
cls._order = [('id', 'DESC')]
cls._buttons.update(
process={
'readonly': Eval('rule'),
'depends': ['rule'],
},
)
@classmethod
def get_models(cls):
return [(None, "")] + Pool().get('ir.model').get_name_items()
@classmethod
def from_webhook(cls, inbox, data, data_type):
emails = []
if data_type in {'raw', 'mailpace', 'sendgrid', 'postmark'}:
emails.append(cls(inbox=inbox, data=data, data_type=data_type))
elif data_type == 'mailchimp':
payload = json.loads(data)
for event in payload['mandrill_events']:
if event['event'] == 'inbound':
emails.append(cls(
inbox=inbox,
data=json.dumps(event).encode('utf-8'),
data_type=data_type))
return emails
@classmethod
@ModelView.button
def process(cls, emails):
for email_ in emails:
if not email_.rule:
email_.inbox.process(email_)
cls.save(emails)
def as_dict(self):
value = {}
if self.data_type == 'raw':
value.update(self._as_dict(self.data))
elif self.data_type == 'mailchimp':
event = json.loads(self.data)
value.update(self._as_dict(event['raw_msg']))
elif self.data_type == 'mailpace':
payload = json.loads(self.data)
value.update(self._as_dict(payload['raw']))
elif self.data_type == 'postmark':
payload = json.loads(self.data)
value.update(self._as_dict_postmark(payload))
elif self.data_type == 'sendgrid':
payload = json.loads(self.data)
value.update(self._as_dict(payload['email']))
return value
def _as_dict(self, raw):
value = {}
if isinstance(raw, str):
message = email.message_from_string(
raw, policy=email_policy_default)
else:
message = email.message_from_bytes(
raw, policy=email_policy_default)
if 'From' in message:
value['from'] = getaddresses([message.get('From')])[0][1]
for key in ['To', 'Cc', 'Bcc']:
if key in message:
value[key.lower()] = [
a for _, a in getaddresses(message.get_all(key))]
if 'Subject' in message:
value['subject'] = message['Subject']
text = _email_text(message)
if text is not None:
value['text'] = text
html = _email_text(message, 'html')
if html is not None:
value['html'] = html
value['attachments'] = list(_email_attachments(message))
value['headers'] = dict(message.items())
return value
def _as_dict_postmark(self, payload):
value = {}
if 'FromFull' in payload:
value['from'] = payload['FromFull']['Email']
for key in ['To', 'Cc', 'Bcc']:
if f'{key}Full' in payload:
value[key.lower()] = [
a['Email'] for a in payload[f'{key}Full']]
if 'Subject' in payload:
value['subject'] = payload['Subject']
if 'TextBody' in payload:
value['text'] = payload['TextBody']
if 'HtmlBody' in payload:
value['html'] = payload['HtmlBody']
if 'Attachments' in payload:
value['attachments'] = [{
'filename': a['Name'],
'type': a['ContentType'],
'data': base64.b64decode(a['Content']),
} for a in payload['Attachments']]
if 'Headers' in payload:
value['headers'] = {
h['Name']: h['Value'] for h in payload['Headers']}
return value
class Rule(sequence_ordered(), ModelSQL, ModelView):
"Inbound Email Rule"
__name__ = 'inbound.email.rule'
inbox = fields.Many2One(
'inbound.email.inbox', "Inbox", required=True, ondelete='CASCADE')
origin = fields.Char(
"Origin",
help="A regular expression to match the sender email address.")
destination = fields.Char(
"Destination",
help="A regular expression to match any receiver email addresses.")
subject = fields.Char(
"Subject",
help="A regular expression to match the subject.")
attachment_name = fields.Char(
"Attachment Name",
help="A regular expression to match any attachment name.")
headers = fields.One2Many('inbound.email.rule.header', 'rule', "Headers")
action = fields.Selection([
(None, ""),
], "Action")
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('inbox')
cls._order.insert(0, ('inbox.id', 'DESC'))
def match(self, email_):
flags = re.IGNORECASE
search = partial(re.search, flags=flags)
compile_ = partial(re.compile, flags=flags)
if self.origin:
if not search(self.origin, email_.get('from', '')):
return False
if self.destination:
destinations = [
*email_.get('to', []),
*email_.get('cc', []),
*email_.get('bcc', []),
]
pattern = compile_(self.destination)
if not any(pattern.search(d) for d in destinations):
return False
if self.subject:
if not search(self.subject, email_.get('subject', '')):
return False
if self.attachment_name:
pattern = compile_(self.attachment_name)
if not any(
pattern.search(a.get('filename', ''))
for a in email_.get('attachments', [])):
return False
if self.headers:
for header in self.headers:
if not search(
header.value,
email_.get('headers', {}).get(header.name, '')):
return False
return True
def run(self, email_):
pool = Pool()
if self.action:
model, method = self.action.split('|')
Model = pool.get(model)
email_.result = getattr(Model, method)(email_, self)
class RuleHeader(ModelSQL, ModelView):
"Inbound Email Rule Header"
__name__ = 'inbound.email.rule.header'
rule = fields.Many2One(
'inbound.email.rule', "Rule", required=True, ondelete='CASCADE')
name = fields.Char(
"Name", required=True,
help="The name of the header.")
value = fields.Char(
"Value",
help="A regular expression to match the header value.")
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('rule')
|