Skip to content

Commit ec2bdd3

Browse files
authored
Merge pull request #52 from rohangirishrao/devel
Changes to `send_mail` function (now called `send_notification_email`). Merging, thanks a lot! πŸ’™
2 parents bf97721 + 2027558 commit ec2bdd3

2 files changed

Lines changed: 71 additions & 15 deletions

File tree

β€Žsrc/imcflibs/imagej/misc.pyβ€Ž

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,13 @@ def find_focus(imp):
185185
return focused_slice
186186

187187

188-
def send_mail(job_name, recipient, filename, total_execution_time):
189-
"""Send an email using the SMTP server and sender email configured in ImageJ's Preferences.
188+
def send_notification_email(
189+
job_name, recipient, filename, total_execution_time, subject="", message=""
190+
):
191+
"""Send an email notification with optional details of the processed job.
192+
193+
Retrieve the sender email and SMTP server settings from ImageJ's preferences
194+
and use them to send an email notification with job details.
190195
191196
Parameters
192197
----------
@@ -198,34 +203,58 @@ def send_mail(job_name, recipient, filename, total_execution_time):
198203
The name of the file to be passed in the email.
199204
total_execution_time : str
200205
The time it took to process the file in the format [HH:MM:SS:ss].
206+
subject : string, optional
207+
Subject of the email, by default says job finished.
208+
message : string, optional
209+
Message to be included in the email, by default says job processed.
210+
211+
Notes
212+
-----
213+
- The function requires two preferences to be set in `~/.imagej/IJ_Prefs.txt`:
214+
- `.imcf.sender_email`: the sender's email address
215+
- `.imcf.smtpserver`: the SMTP server address
216+
- If these preferences are not set or if required parameters are missing,
217+
the function logs a message and exits without sending an email.
218+
- In case of an SMTP error, the function logs a warning.
201219
"""
220+
202221
# Retrieve sender email and SMTP server from preferences
222+
# NOTE: the leading dot "." has to be omitted in the `Prefs.get()` call,
223+
# despite being present in the `IJ_Prefs.txt` file!
203224
sender = prefs.Prefs.get("imcf.sender_email", "").strip()
204225
server = prefs.Prefs.get("imcf.smtpserver", "").strip()
205226

206227
# Ensure the sender and server are configured from Prefs
207228
if not sender:
208-
log.info("Sender email is not configured. Please check IJ_Prefs.txt.")
229+
log.info("[.imcf.sender_email] is not configured in '~/.imagej/IJ_Prefs.txt'.")
209230
return
210231
if not server:
211-
log.info("SMTP server is not configured. Please check IJ_Prefs.txt.")
232+
log.info("[.imcf.smtpserver] is not configured in '~/.imagej/IJ_Prefs.txt'.")
212233
return
213234

235+
log.debug("Using SMTP server [%s].", server)
236+
214237
# Ensure the recipient is provided
215238
if not recipient.strip():
216-
log.info("Recipient email is required.")
239+
log.info("Recipient email is required, not sending email notification.")
217240
return
218241

219242
# Form the email subject and body
220-
subject = "Your {0} job finished successfully".format(job_name)
221-
body = (
222-
"Dear recipient,\n\n"
223-
"This is an automated message.\n"
224-
"Your dataset '{0}' has been successfully processed "
225-
"({1} [HH:MM:SS:ss]).\n\n"
226-
"Kind regards,\n"
227-
"The IMCF-team"
228-
).format(filename, total_execution_time)
243+
if subject == "":
244+
subject = "Your {0} job has finished".format(job_name)
245+
else:
246+
subject = subject
247+
248+
if message == "":
249+
body = (
250+
"Dear recipient,\n\n"
251+
"This is an automated message.\n"
252+
"Your workflow '{0}' has been processed "
253+
"({1} [HH:MM:SS:ss]).\n\n"
254+
"Kind regards.\n"
255+
).format(filename, total_execution_time)
256+
else:
257+
body = message
229258

230259
# Form the complete message
231260
message = ("From: {0}\nTo: {1}\nSubject: {2}\n\n{3}").format(
@@ -236,7 +265,7 @@ def send_mail(job_name, recipient, filename, total_execution_time):
236265
try:
237266
smtpObj = smtplib.SMTP(server)
238267
smtpObj.sendmail(sender, recipient, message)
239-
log.debug("Successfully sent email")
268+
log.debug("Successfully sent email to <%s>.", recipient)
240269
except smtplib.SMTPException as err:
241270
log.warning("Error: Unable to send email: %s", err)
242271
return
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Test the send_notification_email function
2+
3+
```Python
4+
from imcflibs.imagej.misc import send_notification_email
5+
6+
from imcflibs.log import LOG as log
7+
from imcflibs.log import enable_console_logging
8+
from imcflibs.log import set_loglevel
9+
10+
11+
enable_console_logging()
12+
set_loglevel(2)
13+
14+
15+
# see if logging works:
16+
log.warn("warn")
17+
log.debug("DEBUG")
18+
19+
send_notification_email(
20+
job_name="my job",
21+
recipient="nikolaus.ehrenfeuchter@unibas.ch",
22+
filename="magic-segmentation.py",
23+
total_execution_time="5 years",
24+
)
25+
26+
log.info("DONE")
27+
```

0 commit comments

Comments
Β (0)