Skip to content

Commit

Permalink
feat: Added e-mailHeaders config option (pr #143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstam authored and neilmunday committed Oct 21, 2024
1 parent bdef629 commit ae5364f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions etc/slurm-mail/slurm-mail.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ emailFromUserAddress = root
emailFromName = Slurm Admin
emailRegEx = \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
emailSubject = Job $CLUSTER.$JOB_ID: $STATE
# emailHeaders = Precedence: bulk;X-Auto-Response-Suppress: DR, OOF, AutoReply
gecosNameField = 0
validateEmail = false
datetimeFormat = %d/%m/%Y %H:%M:%S
Expand Down
19 changes: 19 additions & 0 deletions src/slurmmail/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def __init__(self) -> None:
self.email_from_address: str
self.email_from_name: Optional[str] = None
self.email_subject: str
self.email_headers: Dict[str, str] = {}
self.mail_regex: str = r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"
self.validate_email: Optional[bool] = None
self.sacct_exe: pathlib.Path
Expand Down Expand Up @@ -764,6 +765,19 @@ def __process_spool_file(
msg["From"] = options.email_from_address
msg["Date"] = email.utils.formatdate(localtime=True)
msg["Message-ID"] = email.utils.make_msgid()

# add optional headers
if options.email_headers:
for header_name, header_value in options.email_headers.items():
if header_name in msg:
logging.warning(
"Ignoring header_name %s - header is already set",
header_name
)
continue

msg[header_name] = header_value

# prefer HTML to plain text, so we add the plain text attachment first (see rfc2046 5.1.4)
msg.attach(MIMEText(body_text, "plain"))
msg.attach(MIMEText(body_html, "html"))
Expand Down Expand Up @@ -891,6 +905,11 @@ def send_mail_main():
options.email_from_name = config.get(section, "emailFromName")
options.email_subject = config.get(section, "emailSubject")

if config.has_option(section, "emailHeaders"):
for header in config.get(section, "emailHeaders").split(";"):
header_name, header_value = header.split(":", maxsplit=1)
options.email_headers[header_name.strip()] = header_value.strip()

if config.has_option(section, "gecosNameField"):
Job.GECOS_NAME_FIELD = config.getint(section, "gecosNameField")

Expand Down

0 comments on commit ae5364f

Please sign in to comment.