2020import pwd
2121import grp
2222import shlex
23+ import smtplib
24+ from email .message import EmailMessage
2325from subprocess import Popen , PIPE
2426from os import path as op
2527import time
116118 'PGBATCH' : '' , # current batch service name, SLURM or PBS
117119 'PGBINDIR' : '' ,
118120 'SLMTIME' : 604800 , # max runtime for SLURM bath job, (7x24x60x60 seconds)
119- 'PBSTIME' : 86400 , # max runtime for SLURM bath job, (7x24x60x60 seconds)
121+ 'PBSTIME' : 86400 , # max runtime for PBS bath job, (24x60x60 seconds)
120122 'MSSGRP' : None , # set if set to different HPSS group
121123 'RDAGRP' : "decs" ,
122124 'EMLSEND' : None , # path to sendmail, None if not exists
131133 'ERR2STD' : [], # if non-empty reference to array of strings, change stderr to stdout if match
132134 'STD2ERR' : [], # if non-empty reference to array of strings, change stdout to stderr if match
133135 'MISSFILE' : "No such file or directory" ,
134- 'GITHUB' : "https://github.com" # github server
136+ 'GITHUB' : "https://github.com" , # github server
137+ 'EMLSRVR' : "ndir.ucar.edu" , # UCAR email server and port
138+ 'EMLPORT' : 25
135139}
136140
137141HOSTTYPES = {
@@ -270,30 +274,42 @@ def send_customized_email(logmsg, emlmsg, logact = 0):
270274 if entries ['cc' ][2 ]: logmsg += " Cc'd " + entries ['cc' ][2 ]
271275 logmsg += " Subject: " + entries ['sb' ][2 ]
272276
273- if pgsystem (PGLOG ['EMLSEND' ], logact , 4 , emlmsg ):
277+ ret = FAILURE
278+ if PGLOG ['EMLSEND' ]: ret = pgsystem (PGLOG ['EMLSEND' ], logact , 4 , emlmsg )
279+ if not ret : ret = send_python_email (entries ['sb' ][2 ], entries ['to' ][2 ], emlmsg , entries ['fr' ][2 ], entries ['cc' ][2 ], logact )
280+
281+ if ret :
274282 log_email (emlmsg )
275283 if logact : pglog (logmsg , logact & (~ EXITLG ))
276- return SUCCESS
277284 else :
278285 errmsg = "Error sending email: " + logmsg
279- return pglog (logmsg , (logact | ERRLOG )& ~ EXITLG )
286+ pglog (errmsg , (logact | ERRLOG )& ~ EXITLG )
287+
288+ return ret
280289
281290#
282- # send an email, if empty msg; send email message saved in PGLOG['EMLMSG'] instead
291+ # send an email; if empty msg send email message saved in PGLOG['EMLMSG'] instead
283292#
284293def send_email (subject = None , receiver = None , msg = None , sender = None , logact = 0 ):
285294
295+ return send_python_email (subject , receiver , msg , sender , None , logact )
296+
297+ #
298+ # send an email via python module smtplib; if empty msg send email message saved in PGLOG['EMLMSG'] instead
299+ #
300+ def send_python_email (subject = None , receiver = None , msg = None , sender = None , cc = None , logact = 0 ):
301+
286302 if not msg :
287303 if PGLOG ['EMLMSG' ]:
288304 msg = PGLOG ['EMLMSG' ]
289305 PGLOG ['EMLMSG' ] = ''
290306 else :
291307 return ''
292308
293- docc = 0
309+ docc = False if cc else True
294310 if not sender :
295311 sender = PGLOG ['CURUID' ]
296- if sender != PGLOG ['RDAUSER' ]: docc = 1
312+ if sender != PGLOG ['RDAUSER' ]: docc = False
297313 if sender == PGLOG ['RDAUSER' ]: sender = PGLOG ['RDAEMAIL' ]
298314 if sender .find ('@' ) == - 1 : sender += "@ucar.edu"
299315 if not receiver :
@@ -302,26 +318,31 @@ def send_email(subject = None, receiver = None, msg = None, sender = None, logac
302318 if receiver .find ('@' ) == - 1 : receiver += "@ucar.edu"
303319
304320 if docc and not re .match (PGLOG ['RDAUSER' ], sender ): add_carbon_copy (sender , 1 )
305-
306- emlmsg = "From: {}\n To: {}\n " .format (sender , receiver )
321+ emlmsg = EmailMessage ()
322+ emlmsg .set_content (msg )
323+ emlmsg ['From' ] = sender
324+ emlmsg ['To' ] = receiver
307325 logmsg = "Email " + receiver
308- if PGLOG ['CCDADDR' ]:
309- emlmsg += "Cc: {}\n " .format (PGLOG ['CCDADDR' ])
310- logmsg += " Cc'd " + PGLOG ['CCDADDR' ]
326+ if not cc : cc = PGLOG ['CCDADDR' ]
327+ if cc :
328+ emlmsg ['Cc' ] = cc
329+ logmsg += " Cc'd " + cc
311330 if not subject : subject = "Message from {}-{}" .format (PGLOG ['HOSTNAME' ], get_command ())
312331 if not re .search (r'!$' , subject ): subject += '!'
313- emlmsg += "Subject: {} \n {} \n " . format ( subject , msg )
332+ emlmsg [ 'Subject' ] = subject
314333 if CPID ['CPID' ]: logmsg += " in " + CPID ['CPID' ]
315334 logmsg += ", Subject: {}\n " .format (subject )
316-
317- if pgsystem (PGLOG ['EMLSEND' ], logact , 4 , emlmsg ):
318- log_email (emlmsg )
335+ try :
336+ eml = smtplib .SMTP (PGLOG ['EMLSRVR' ], PGLOG ['EMLPORT' ])
337+ eml .send_message (emlmsg )
338+ except smtplib .SMTPException as err :
339+ errmsg = f"Error sending email:\n { err } \n { logmsg } "
340+ return pglog (errmsg , (logact | ERRLOG )& ~ EXITLG )
341+ finally :
342+ eml .quit ()
343+ log_email (str (emlmsg ))
319344 if logact : pglog (logmsg , logact & ~ EXITLG )
320- return logmsg
321- else :
322- errmsg = "Error sending email: " + logmsg
323- pglog (logmsg , (logact | ERRLOG )& ~ EXITLG )
324- return errmsg
345+ return SUCCESS
325346
326347#
327348# log email sent
0 commit comments