Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emailHeaders for extra mail headers #143

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 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,12 @@ 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():
msg[header_name] = header_value
neilmunday marked this conversation as resolved.
Show resolved Hide resolved

# 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 +898,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"):
email_headers = [ x.strip().split(":", maxsplit=1) for x in config.get(section, "emailHeaders").split(";") ]
Copy link
Owner

@neilmunday neilmunday Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To populate the options.email_headers dictionary, two loops are being used here, one to create email_headers and then another to iterate through this list. Instead this can be accomplished with one loop which is slightly easier to read also:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted based on the feedback

for email_header in email_headers:
options.email_headers[email_header[0].strip()] = email_header[1].strip()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to ensure that the user had not provided e-mail headers that are set by Slurm-Mail. These are:

  • To
  • From
  • Date
  • Message-ID

Otherwise these will get overwritten. Perhaps an array of protected headers could be added which are then checked against when processing the slurm.conf file?


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

Expand Down
Loading