From 69a6122716660e02e8d080f4371b5bd9a48da694 Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Thu, 20 Mar 2025 17:53:31 +0100 Subject: [PATCH 1/9] Change method name to be more descriptive --- src/imcflibs/imagej/misc.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 1ccf35cc..3ca2606b 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -173,8 +173,11 @@ def find_focus(imp): 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 automated email notification with optional details of the processed job. + + This function retrieves the sender email and SMTP server settings from + ImageJ's preferences and uses them to send an email notification with job details. Parameters ---------- From 4d4c4c89f1abde34f03cc19a07a0f8a97d197e3e Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Thu, 20 Mar 2025 17:55:07 +0100 Subject: [PATCH 2/9] Allow for user specified subject and message --- src/imcflibs/imagej/misc.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 3ca2606b..7056afa7 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -208,15 +208,21 @@ def send_notification_email(job_name, recipient, filename, total_execution_time, 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) + else: + subject = subject + + if message == "": + body = ( + "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 # Form the complete message message = ("From: {0}\nTo: {1}\nSubject: {2}\n\n{3}").format( From 45622da097c81a7a3abdad82413e3e110d56785f Mon Sep 17 00:00:00 2001 From: Rohan Girish Date: Thu, 20 Mar 2025 17:55:23 +0100 Subject: [PATCH 3/9] Change docstring to be more accurate --- src/imcflibs/imagej/misc.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 7056afa7..0c116c69 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -189,7 +189,21 @@ def send_notification_email(job_name, recipient, filename, total_execution_time, 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) and + `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 sender = prefs.Prefs.get("imcf.sender_email", "").strip() server = prefs.Prefs.get("imcf.smtpserver", "").strip() From 35ee4e85efa072e608f5ac1610b7fa3e6a52c0de Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 11:42:19 +0100 Subject: [PATCH 4/9] Auto formatting --- src/imcflibs/imagej/misc.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 0c116c69..0419b098 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -173,7 +173,9 @@ def find_focus(imp): return focused_slice -def send_notification_email(job_name, recipient, filename, total_execution_time, subject = "", message=""): +def send_notification_email( + job_name, recipient, filename, total_execution_time, subject="", message="" +): """Send an automated email notification with optional details of the processed job. This function retrieves the sender email and SMTP server settings from From 5ccdb79a3b4ee0de0d4537e8538df2730d09d73d Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 14:36:41 +0100 Subject: [PATCH 5/9] =?UTF-8?q?Imperative=20mood=20FTW=20=F0=9F=97=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/imcflibs/imagej/misc.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 2ed51dbb..e40f9631 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -188,10 +188,10 @@ def find_focus(imp): def send_notification_email( job_name, recipient, filename, total_execution_time, subject="", message="" ): - """Send an automated email notification with optional details of the processed job. + """Send an email notification with optional details of the processed job. - This function retrieves the sender email and SMTP server settings from - ImageJ's preferences and uses them to send an email notification with job details. + Retrieve the sender email and SMTP server settings from ImageJ's preferences + and use them to send an email notification with job details. Parameters ---------- From 378cf4b46c5a81c4a6a132681a35c67975886684 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 15:37:28 +0100 Subject: [PATCH 6/9] Be a bit more verbose in messages --- src/imcflibs/imagej/misc.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index e40f9631..fd74f000 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -224,15 +224,17 @@ def send_notification_email( # 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'.") 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'.") return + log.debug("Using SMTP server [%s].", server) + # 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.") return # Form the email subject and body @@ -261,7 +263,7 @@ def send_notification_email( try: smtpObj = smtplib.SMTP(server) smtpObj.sendmail(sender, recipient, message) - log.debug("Successfully sent email") + log.debug("Successfully sent email to <%s>.", recipient) except smtplib.SMTPException as err: log.warning("Error: Unable to send email: %s", err) return From cdf7ddf8237a7475fa66a0bb1d9979fa15863bfb Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 15:39:59 +0100 Subject: [PATCH 7/9] Add note on Prefs.get() call vs. dot prefix --- src/imcflibs/imagej/misc.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index fd74f000..83ba6fbf 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -219,6 +219,8 @@ def send_notification_email( """ # 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() From e14bbb332ca42248d25249f5da8ae7e7b61a8a9c Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 15:40:12 +0100 Subject: [PATCH 8/9] Minor docstring modification --- src/imcflibs/imagej/misc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/imcflibs/imagej/misc.py b/src/imcflibs/imagej/misc.py index 83ba6fbf..7b9d2630 100644 --- a/src/imcflibs/imagej/misc.py +++ b/src/imcflibs/imagej/misc.py @@ -211,8 +211,8 @@ def send_notification_email( Notes ----- - The function requires two preferences to be set in `~/.imagej/IJ_Prefs.txt`: - `imcf.sender_email` (the sender's email address) and - `imcf.smtpserver` (the SMTP server address). + - `.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. From 20275588ab7ec6e7c3c1659df6419067dde3eea2 Mon Sep 17 00:00:00 2001 From: Niko Ehrenfeuchter Date: Wed, 26 Mar 2025 15:45:19 +0100 Subject: [PATCH 9/9] Add test instructions for send_notification_email --- .../send-notification-email.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/interactive-imagej/send-notification-email.md diff --git a/tests/interactive-imagej/send-notification-email.md b/tests/interactive-imagej/send-notification-email.md new file mode 100644 index 00000000..0ad05ccb --- /dev/null +++ b/tests/interactive-imagej/send-notification-email.md @@ -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") +```