-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincoming_mail.py
123 lines (83 loc) · 3.66 KB
/
incoming_mail.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
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
import logging, re, email
from incoming_image import IncomingImageHandler
class IncomingMailHandler():
# mailWithLabel: string - total string representing the sender mail address and (if found) his name
# attachments: list of tuple, 1st element of the tuple: filename, 2nd element of tuple: filecontent
def __init__(self, mailWithLabel, attachments):
self.mailWithLabel = mailWithLabel
self.attachments = attachments
def getNumberOfAttachments(self):
numberOfAttachments = len(self.attachments)
return numberOfAttachments
def extractMailAndLabel(self):
mailWithLabelTuple = email.header.decode_header(self.mailWithLabel)[0]
mailWithLabelEncoded = mailWithLabelTuple[0]
mailWithLabelEncoding = mailWithLabelTuple[1]
if(mailWithLabelEncoding):
mailWithLabelDecoded = mailWithLabelEncoded.decode(mailWithLabelEncoding)
else:
mailWithLabelDecoded = mailWithLabelEncoded.decode()
mailRegexMatch = re.search(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}', self.mailWithLabel)
mail = mailRegexMatch.group()
labelRegex = re.compile("<"+mail+">")
label = labelRegex.sub("",mailWithLabelDecoded)
mailAndLabel = (mail,label)
return mailAndLabel
def getVcardNameAndContent(self):
vcard = None
for attachment in self.attachments:
filename = attachment[0]
UTF8Filename = ""
# According the RFC2047
# Example: =?ISO-8859-1?Q?Rapha=EBl_Labb=E9=2Evcf?=
if isinstance(filename, str):
listWithEncodedFilenameAndEncoding = email.header.decode_header(filename)
tupleWithEncodedFilenameAndEncoding = listWithEncodedFilenameAndEncoding[0]
# After this operation we have a tuple like this
# [('Rapha\xebl Labb\xe9.vcf', 'iso-8859-1')]
encodedFilename = tupleWithEncodedFilenameAndEncoding[0]
encoding = tupleWithEncodedFilenameAndEncoding[1]
if(encoding):
UTF8Filename = encodedFilename.decode(encoding)
else:
UTF8Filename = encodedFilename.decode()
# According the RFC2231
# Example: ('utf-8', '', 'C\xc3\xa9dric Deltheil.vcf')
else:
tupleWithEncodedFilenameAndEncoding = filename
encoding = tupleWithEncodedFilenameAndEncoding[0]
encodedFilename = tupleWithEncodedFilenameAndEncoding[2]
UTF8Filename = encodedFilename.decode(encoding)
filetype = UTF8Filename[-4:].lower()
if(filetype == ".vcf"):
vcardName = UTF8Filename
logging.info(vcardName)
encodedVcardData = attachment[1]
vcardContent = encodedVcardData.decode()
vcard = (vcardName, vcardContent)
return vcard
def getImageContent(self):
imageContent = None
for attachment in self.attachments:
filename = attachment[0]
UTF8Filename = ""
# According the RF2047
if isinstance(filename, str):
listWithEncodedFilenameAndEncoding = email.header.decode_header(filename)
tupleWithEncodedFilenameAndEncoding = listWithEncodedFilenameAndEncoding[0]
encodedFilename = tupleWithEncodedFilenameAndEncoding[0]
encoding = tupleWithEncodedFilenameAndEncoding[1]
if(encoding):
UTF8Filename = encodedFilename.decode(encoding)
else:
UTF8Filename = encodedFilename
filetype = UTF8Filename[-4:].lower()
if((filetype == ".gif") or (filetype == "jpeg") or (filetype == ".jpg") or (filetype == ".png")):
encodedImageData = attachment[1]
imagePayload = encodedImageData.payload
imageEncoding = encodedImageData.encoding
if(imageEncoding):
decodedImageData = imagePayload.decode(imageEncoding)
incomingImageHandler = IncomingImageHandler(decodedImageData)
imageContent = incomingImageHandler.processImage()
return imageContent