Skip to content

Commit 394c689

Browse files
Merge pull request #1372 from TheHive-Project/mailer-improvements-1
Mailer - Tiny fixes & improvements
2 parents 6eb288e + e73a9f4 commit 394c689

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

responders/Mailer/mailer.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
from cortexutils.responder import Responder
77
from email.mime.multipart import MIMEMultipart
88
from email.mime.text import MIMEText
9+
from email.utils import formatdate, make_msgid
910

1011

1112
class Mailer(Responder):
1213
def __init__(self):
1314
Responder.__init__(self)
1415
self.smtp_host = self.get_param("config.smtp_host", "localhost")
15-
self.smtp_port = self.get_param("config.smtp_port", "25")
16+
self.smtp_port = int(self.get_param("config.smtp_port", "25"))
1617
self.mail_from = self.get_param(
1718
"config.from", None, "Missing sender email address"
1819
)
@@ -36,6 +37,10 @@ def run(self):
3637

3738
mail_to = None
3839
if self.data_type == "thehive:case":
40+
# Add case number to title
41+
case_number = self.get_param("data.caseId", None)
42+
if case_number:
43+
title = f"[Case #{case_number}] {title}"
3944
# Search recipient address in case tags
4045
tags = self.get_param(
4146
"data.tags", None, "recipient address not found in tags"
@@ -49,6 +54,10 @@ def run(self):
4954
self.error("recipient address not found in tags")
5055

5156
elif self.data_type == "thehive:case_task":
57+
# Add case number to title
58+
case_number = self.get_param("data.case.caseId", None)
59+
if case_number:
60+
title = f"[Case #{case_number}] {title}"
5261
# Search recipient address in tasks description
5362
descr_array = description.splitlines()
5463
if "mailto:" in descr_array[0]:
@@ -62,23 +71,36 @@ def run(self):
6271

6372
elif self.data_type == "thehive:alert":
6473
# Search recipient address in artifacts
65-
artifacts = self.get_param(
66-
"data.artifacts", None, "recipient address not found in observables"
74+
# artifacts = self.get_param(
75+
# "data.artifacts", None, "recipient address not found in observables"
76+
# )
77+
# mail_artifacts = [
78+
# a["data"]
79+
# for a in artifacts
80+
# if a.get("dataType") == "mail" and "data" in a
81+
# ]
82+
# if mail_artifacts:
83+
# mail_to = mail_artifacts.pop()
84+
# else:
85+
# self.error("recipient address not found in observables")
86+
# Search recipient address in case tags
87+
tags = self.get_param(
88+
"data.tags", None, "recipient address not found in tags"
6789
)
68-
mail_artifacts = [
69-
a["data"]
70-
for a in artifacts
71-
if a.get("dataType") == "mail" and "data" in a
90+
mail_tags = [
91+
t[5:] for t in tags if t.startswith("mail=") or t.startswith("mail:")
7292
]
73-
if mail_artifacts:
74-
mail_to = mail_artifacts.pop()
93+
if mail_tags:
94+
mail_to = mail_tags.pop()
7595
else:
76-
self.error("recipient address not found in observables")
96+
self.error("recipient address not found in tags")
7797

7898
msg = MIMEMultipart()
7999
msg["Subject"] = title
80100
msg["From"] = self.mail_from
81101
msg["To"] = mail_to
102+
msg["Date"] = formatdate(localtime=True)
103+
msg["Message-ID"] = make_msgid()
82104
msg.attach(MIMEText(description, "plain", "utf-8"))
83105

84106
if self.smtp_user and self.smtp_pwd:

0 commit comments

Comments
 (0)