-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
executable file
·95 lines (78 loc) · 3.18 KB
/
send_email.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
#!/usr/bin/env python
# encoding: utf-8
from cortexutils.responder import Responder
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from enum import Enum
import re
import case_task
import case_task_log
class Mailer(Responder):
severity_dict = {1: "LOW", 2: "MEDIUM", 3: "HIGH"}
def __init__(self):
Responder.__init__(self)
self.smtp_host = self.get_param(
'config.smtp_host', 'localhost')
self.mail_from = self.get_param(
'config.from', None, 'Missing sender email address')
def verify_addr(self, addresses):
# Verify the list of recepients
for addr in addresses:
server = smtplib.SMTP('webmail.amadeus.com')
result = server.verify(addr)
if result != 250:
# Address is not valid, report an error
self.error('one of the recipient address is not valid')
def case_header(self):
content = ""
title = self.get_param('data.title', None, 'title is missing')
description = self.get_param('data.description', None, 'description is missing')
severity = self.get_param('data.severity', None, 'severity is missing')
status = self.get_param('data.status', None, 'status is missing')
owner = self.get_param('data.owner', None, 'owner is missing')
content = title + "\n"
content = content + "Description: " + description + "\n"
content = content + "Severity: " + Mailer.severity_dict[severity] + "\n"
content = content + "Status: " + status + "\n"
content = content + "Owner: " + owner + "\n"
return content
def run(self):
Responder.run(self)
mail_to = None
if self.data_type == 'thehive:case':
title = self.get_param('data.title', None, 'title is missing')
# Search recipient address in tags
tags = self.get_param('data.tags', None, 'recipient address not found in tags')
mail_tags = [t[5:] for t in tags if t.startswith('mail:')]
if mail_tags:
mail_to = mail_tags.pop()
else:
self.error('recipient address not found among tags')
# Build the email body
content = "Case: "
header = self.case_header()
content = content + header
elif self.data_type == 'thehive:case_task':
# Parse the task
fields = case_task.get_fields(self)
elif self.data_type == 'thehive:case_task_log':
# Parse the log
fields = case_task_log.get_fields(self)
else:
self.error('Invalid dataType')
msg = MIMEMultipart()
msg['Subject'] = fields['subject']
msg['From'] = self.mail_from
#self.verify_addr(fields['addresses'])
msg['To'] = ', '.join(fields['addresses'])
msg.attach(MIMEText(fields['body'], 'html'))
s = smtplib.SMTP(self.smtp_host)
s.sendmail(self.mail_from, fields['addresses'], msg.as_string())
s.quit()
self.report({'message': 'Your message has been sent!'})
def operations(self, raw):
return [self.build_operation('AddTagToCase', tag='mail sent')]
if __name__ == '__main__':
Mailer().run()