-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_mail.py
181 lines (161 loc) · 6.2 KB
/
create_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
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
#!/usr/bin/python
import os
import re
import sys
import datetime
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import email.utils
import mimetypes
import logging
from logging import raiseExceptions
logz=logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
class Mail():
def __init__(self):
self.textBody = None
self.htmlBody = None
self.subject = ""
self.reEmail = re.compile("^([\\w \\._]+\\<[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\>|[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)$")
self.clearRecipients()
self.clearAttachments()
def create(self):
if self.textBody is None and self.htmlBody is None:
raiseExceptions("Error - must specify at least one body type")
if len(self.to)==0:
raiseExceptions("Error - must specify at least one recipient")
if self.textBody is not None and self.htmlBody is None:
msg=MIMEText(self.textBody, "plain")
elif self.textBody is None and self.htmlBody is not None:
msg=MIMEText(self.htmlBody, "html")
else:
msg=MIMEMultipart("alternative")
msg.attach(self.textBody, "plain")
msg.attach(self.htmlBody, "html")
if len(self.attach) != 0:
tmpmsg = msg
msg = MIMEMultipart()
msg.attach(tmpmsg)
for fname,attachname in self.attach:
if not os.path.exists(fname):
print "File '%s' does not exist. Not attaching to email." % fname
continue
if not os.path.isfile(fname):
print "Attachment '%s' is not a file. Not attaching to email." % fname
continue
# Guess at encoding type
ctype, encoding = mimetypes.guess_type(fname)
if ctype is None or encoding is not None:
# No guess could be made so use a binary type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
if maintype == 'text':
fp = open(fname)
attach = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'image':
fp = open(fname, 'rb')
attach = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == 'audio':
fp = open(fname, 'rb')
attach = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open(fname, 'rb')
attach = MIMEBase(maintype, subtype)
attach.set_payload(fp.read())
fp.close()
# Encode the payload using Base64
encoders.encode_base64(attach)
# Set the filename parameter
if attachname is None:
filename = os.path.basename(fname)
else:
filename = attachname
attach.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(attach)
if self.subject is not None:
msg['Subject']=self.subject
if self.mailfrom is not None:
msg['From']=self.mailfrom
if self.to is not None:
msg['To']=", ".join(self.to)
if self.messageID is not None:
msg['Message-ID']=self.messageID
if self.date is not None:
msg['Date']=self.date
msg.preamble = "You need a MIME enabled mail reader to see this message"
return msg
def setMessageID(self):
self.messageID = email.utils.make_msgid()
def setDate(self, date):
self.date=datetime.date(date.year, date.month, date.day).strftime( "%d/%m/%Y %H:%M")
def setFrom(self, address):
"""
Set the email sender.
"""
if not self.validateEmailAddress(address):
raise Exception("Invalid email address '%s'" % address)
self.mailfrom = address
def setSubject(self, subject):
"""
Set the subject of the email message.
"""
self.subject = subject
def setTextBody(self, body):
"""
Set the plain text body of the email message.
"""
self.textBody = body
def clearRecipients(self):
"""
Remove all currently defined recipients for
the email message.
"""
self.to = []
def addRecipient(self, address):
"""
Add a new recipient to the email message.
"""
if not self.validateEmailAddress(address):
raise Exception("Invalid email address '%s'" % address)
self.to.append(address)
def clearAttachments(self):
"""
Remove all file attachments.
"""
self.attach = []
def validateEmailAddress(self, address):
"""
Validate the specified email address.
@return: True if valid, False otherwise
@rtype: Boolean
"""
if self.reEmail.search(address) is None:
return False
return True
def setHtmlBody(self, body):
"""
Set the HTML portion of the email message.
"""
self.htmlBody = body
def addAttachment(self, fname, attachname=None):
"""
Add a file attachment to this email message.
@param fname: The full path and file name of the file
to attach.
@type fname: String
@param attachname: This will be the name of the file in
the email message if set. If not set
then the filename will be taken from
the fname parameter above.
@type attachname: String
"""
if fname is None:
return
self.attach.append( (fname, attachname) )