-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhermes.py
123 lines (95 loc) · 3.07 KB
/
hermes.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
#!/usr/bin/python
# ~*~ coding=utf-8 ~*~
import datetime # datetime.now
import mimetypes
import os # path.basename, path.isfile
import time # mktime
from email import generator # Generator
from email import encoders # encode_base64
from email import utils # formatdate
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 scripts.lib.example as example
def get_rfc2822_date():
# See https://tools.ietf.org/html/rfc2822
now = datetime.datetime.now()
time_tuple = now.timetuple()
timestamp = time.mktime(time_tuple)
return utils.formatdate(timestamp)
def add_attachment(file_path):
# Checking if attachment file exists
if os.path.isfile(file_path):
# Detecting filetype
file_type, encoding = mimetypes.guess_type(file_path)
if file_type is None or encoding is not None:
file_type = 'application/octet-stream'
type_primary, type_secondary = file_type.split('/', 1)
if type_primary == 'text':
with open(file_path) as file:
data = MIMEText(file.read(), type_secondary)
elif type_primary == 'image':
with open(file_path, 'rb') as file:
data = MIMEImage(file.read(), type_secondary)
elif type_primary == 'audio':
with open(file_path, 'rb') as file:
data = MIMEAudio(file.read(), type_secondary)
else:
with open(file_path, 'rb') as file:
data = MIMEBase(type_primary, type_secondary)
data.set_payload(file.read())
encoders.encode_base64(data)
file_name = os.path.basename(file_path)
data.add_header('Content-Disposition',
'attachment', filename=file_name)
return data
return False
def _create_mail(
is_from='info@fundevogel.de',
goes_to='',
cc='',
bcc='',
subject='',
text='',
attachments=[],
output_path='mail.eml',
):
###
# Creating `eml` file
#
# Adding message header
mail = MIMEMultipart()
mail['Subject'] = subject
mail['To'] = goes_to
mail['From'] = is_from
mail['Cc'] = cc
mail['Bcc'] = bcc
mail['Date'] = get_rfc2822_date()
# Adding message text
data = (
'<html><head></head><body>'
+ example.text +
'<p>' + text + '</p>'
+ example.signature +
'</body></html>'
)
body = MIMEText(data, 'html', 'utf-8')
mail.attach(body)
# Adding message attachments
if attachments:
for attachment in attachments:
attachment = add_attachment(attachment)
mail.attach(attachment)
# Writing message to disk
with open(output_path, 'w') as file:
output_path = generator.Generator(file)
output_path.flatten(mail)
return True
def create_mail(subject, text, output_path):
return _create_mail(
subject=subject,
text=text,
output_path=output_path
)