Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
59 changes: 44 additions & 15 deletions src/imcflibs/imagej/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@
return focused_slice


def send_mail(job_name, recipient, filename, total_execution_time):
"""Send an email using the SMTP server and sender email configured in ImageJ's Preferences.
def send_notification_email(
job_name, recipient, filename, total_execution_time, subject="", message=""
):
"""Send an email notification with optional details of the processed job.

Retrieve the sender email and SMTP server settings from ImageJ's preferences
and use them to send an email notification with job details.

Parameters
----------
Expand All @@ -198,34 +203,58 @@
The name of the file to be passed in the email.
total_execution_time : str
The time it took to process the file in the format [HH:MM:SS:ss].
subject : string, optional
Subject of the email, by default says job finished.
message : string, optional
Message to be included in the email, by default says job processed.

Notes
-----
- The function requires two preferences to be set in `~/.imagej/IJ_Prefs.txt`:
- `.imcf.sender_email`: the sender's email address
- `.imcf.smtpserver`: the SMTP server address
- If these preferences are not set or if required parameters are missing,
the function logs a message and exits without sending an email.
- In case of an SMTP error, the function logs a warning.
"""

# Retrieve sender email and SMTP server from preferences
# NOTE: the leading dot "." has to be omitted in the `Prefs.get()` call,
# despite being present in the `IJ_Prefs.txt` file!
sender = prefs.Prefs.get("imcf.sender_email", "").strip()
server = prefs.Prefs.get("imcf.smtpserver", "").strip()

# Ensure the sender and server are configured from Prefs
if not sender:
log.info("Sender email is not configured. Please check IJ_Prefs.txt.")
log.info("[.imcf.sender_email] is not configured in '~/.imagej/IJ_Prefs.txt'.")

Check warning on line 229 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L229

Added line #L229 was not covered by tests
return
if not server:
log.info("SMTP server is not configured. Please check IJ_Prefs.txt.")
log.info("[.imcf.smtpserver] is not configured in '~/.imagej/IJ_Prefs.txt'.")

Check warning on line 232 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L232

Added line #L232 was not covered by tests
return

log.debug("Using SMTP server [%s].", server)

Check warning on line 235 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L235

Added line #L235 was not covered by tests

# Ensure the recipient is provided
if not recipient.strip():
log.info("Recipient email is required.")
log.info("Recipient email is required, not sending email notification.")

Check warning on line 239 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L239

Added line #L239 was not covered by tests
return

# Form the email subject and body
subject = "Your {0} job finished successfully".format(job_name)
body = (
"Dear recipient,\n\n"
"This is an automated message.\n"
"Your dataset '{0}' has been successfully processed "
"({1} [HH:MM:SS:ss]).\n\n"
"Kind regards,\n"
"The IMCF-team"
).format(filename, total_execution_time)
if subject == "":
subject = "Your {0} job has finished".format(job_name)

Check warning on line 244 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L243-L244

Added lines #L243 - L244 were not covered by tests
else:
subject = subject

Check warning on line 246 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L246

Added line #L246 was not covered by tests

if message == "":
body = (

Check warning on line 249 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L248-L249

Added lines #L248 - L249 were not covered by tests
"Dear recipient,\n\n"
"This is an automated message.\n"
"Your workflow '{0}' has been processed "
"({1} [HH:MM:SS:ss]).\n\n"
"Kind regards.\n"
).format(filename, total_execution_time)
else:
body = message

Check warning on line 257 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L257

Added line #L257 was not covered by tests

# Form the complete message
message = ("From: {0}\nTo: {1}\nSubject: {2}\n\n{3}").format(
Expand All @@ -236,7 +265,7 @@
try:
smtpObj = smtplib.SMTP(server)
smtpObj.sendmail(sender, recipient, message)
log.debug("Successfully sent email")
log.debug("Successfully sent email to <%s>.", recipient)

Check warning on line 268 in src/imcflibs/imagej/misc.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/misc.py#L268

Added line #L268 was not covered by tests
except smtplib.SMTPException as err:
log.warning("Error: Unable to send email: %s", err)
return
Expand Down
27 changes: 27 additions & 0 deletions tests/interactive-imagej/send-notification-email.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Test the send_notification_email function

```Python
from imcflibs.imagej.misc import send_notification_email

from imcflibs.log import LOG as log
from imcflibs.log import enable_console_logging
from imcflibs.log import set_loglevel


enable_console_logging()
set_loglevel(2)


# see if logging works:
log.warn("warn")
log.debug("DEBUG")

send_notification_email(
job_name="my job",
recipient="nikolaus.ehrenfeuchter@unibas.ch",
filename="magic-segmentation.py",
total_execution_time="5 years",
)

log.info("DONE")
```
Loading