Skip to content

Commit 1b619a0

Browse files
authored
Merge pull request #37 from NCAR/hua-work-common
send_python_email()
2 parents c71e91b + 59ea821 commit 1b619a0

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "rda_python_common"
7-
version = "1.0.32"
7+
version = "1.0.33"
88
authors = [
99
{ name="Zaihua Ji", email="zji@ucar.edu" },
1010
]

src/rda_python_common/PgDBI.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,21 +1849,24 @@ def build_customized_email(table, field, condition, subject, logact = 0):
18491849
estat = PgLOG.FAILURE
18501850
msg = PgLOG.get_email()
18511851
if not msg: return estat
1852-
1852+
18531853
sender = PgLOG.PGLOG['CURUID'] + "@ucar.edu"
18541854
receiver = PgLOG.PGLOG['EMLADDR'] if PgLOG.PGLOG['EMLADDR'] else (PgLOG.PGLOG['CURUID'] + "@ucar.edu")
18551855
if receiver.find(sender) < 0: PgLOG.add_carbon_copy(sender, 1)
1856-
ebuf = "From: {}\nTo: {}\n".format(sender, receiver)
1857-
if PgLOG.PGLOG['CCDADDR']: ebuf += "Cc: {}\n".format(PgLOG.PGLOG['CCDADDR'])
1856+
cc = PgLOG.PGLOG['CCDADDR']
18581857
if not subject: subject = "Message from {}-{}".format(PgLOG.PGLOG['HOSTNAME'], PgLOG.get_command())
1859-
ebuf += "Subject: {}!\n\n{}\n".format(subject, msg)
1860-
1861-
if PgLOG.PGLOG['EMLSEND']:
1862-
estat = PgLOG.send_customized_email(f"{table}.{condition}", ebuf, logact)
1858+
estat = PgLOG.send_python_email(subject, receiver, msg, sender, cc, logact)
18631859
if estat != PgLOG.SUCCESS:
1864-
estat = cache_customized_email(table, field, condition, ebuf, 0)
1865-
if estat and logact:
1866-
PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact)
1860+
ebuf = "From: {}\nTo: {}\n".format(sender, receiver)
1861+
if cc: ebuf += "Cc: {}\n".format(cc)
1862+
ebuf += "Subject: {}!\n\n{}\n".format(subject, msg)
1863+
1864+
if PgLOG.PGLOG['EMLSEND']:
1865+
estat = PgLOG.send_customized_email(f"{table}.{condition}", ebuf, logact)
1866+
if estat != PgLOG.SUCCESS:
1867+
estat = cache_customized_email(table, field, condition, ebuf, 0)
1868+
if estat and logact:
1869+
PgLOG.pglog("Email {} cached to '{}.{}' for {}, Subject: {}".format(receiver, table, field, condition, subject), logact)
18671870

18681871
return estat
18691872

src/rda_python_common/PgLOG.py

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import pwd
2121
import grp
2222
import shlex
23+
import smtplib
24+
from email.message import EmailMessage
2325
from subprocess import Popen, PIPE
2426
from os import path as op
2527
import time
@@ -116,7 +118,7 @@
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
@@ -131,7 +133,9 @@
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

137141
HOSTTYPES = {
@@ -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
#
284293
def 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: {}\nTo: {}\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

src/rda_python_common/PgOPT.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ def send_request_email_notice(pgrqst, errmsg, fcount, rstat, readyfile = None, p
16921692
tbl = "dsrqst"
16931693
cnd = "rindex = {}".format(pgrqst['rindex'])
16941694

1695-
if PgLOG.PGLOG['EMLSEND'] and PgLOG.send_customized_email(f"{tbl}.{cnd}", ebuf, 0):
1695+
if PgLOG.send_customized_email(f"{tbl}.{cnd}", ebuf, 0):
16961696
if errmsg:
16971697
PgLOG.pglog("Error Email sent to {} for {}.{}:\n{}".format(einfo['SENDER'], tbl, cnd, errmsg), PGOPT['errlog'])
16981698
readyfile = None

0 commit comments

Comments
 (0)