comparison mailer.py @ 19:535e3327ed1b

add support for specifying From:
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sun, 31 Jan 2021 19:40:26 +0000
parents 1cef0e5f5851
children 69a494ef1a58
comparison
equal deleted inserted replaced
18:1cef0e5f5851 19:535e3327ed1b
1 #!/usr/bin/python 1 #!/usr/bin/python
2 '''Attempt at flexible mailout functionality 2 '''Attempt at flexible mailout functionality
3 Usage: mailer.py [-n] [-s] [-C cc string] [-c COLSPEC[,COLSPEC]*] [-B bcc string] [-b COLSPEC[,COLSPEC]*] [-S col[,col]*] [-a COLSPEC[,COLSPEC]*] [-p COLPAT]* [-SA file[,file]*] [-R reply-to] [-m .sigfilename] COLSPEC[,COLSPEC]* subject {addr-file|-} body-file 3 Usage: mailer.py [-n] [-s] [-C cc string] [-c COLSPEC[,COLSPEC]*] [-B bcc string] [-b COLSPEC[,COLSPEC]*] [-S col[,col]*] [-a COLSPEC[,COLSPEC]*] [-p COLPAT]* [-SA file[,file]*] [-R reply-to] [-f from] [-m .sigfilename] COLSPEC[,COLSPEC]* subject {addr-file|-} body-file
4 4
5 Sends the body as a message from me with subject to destinations per 5 Sends the body as a message from me with subject to destinations per
6 lines in the addr-file selected by COLSPECs (to:) or -c/-b COLSPECs (Cc:/Bcc:) 6 lines in the addr-file selected by COLSPECs (to:) or -c/-b COLSPECs (Cc:/Bcc:)
7 7
8 -n for dry run, prints to stdout 8 -n for dry run, prints to stdout
16 -u Use unicode for attachments 16 -u Use unicode for attachments
17 -s for substitute into body 17 -s for substitute into body
18 -S for columns to substitute as such 18 -S for columns to substitute as such
19 -p for augmentation pattern for a column 19 -p for augmentation pattern for a column
20 -R for replyTo address 20 -R for replyTo address
21 -f for fromName, defaults to $USER@$HOSTNAME
21 -m for .sig file, None for none 22 -m for .sig file, None for none
22 23
23 COLSPEC is of the form a[:n[:f[:g]]] selects from addr-file, which must be tsv 24 COLSPEC is of the form a[:n[:f[:g]]] selects from addr-file, which must be tsv
24 a gives the column for an email address 25 a gives the column for an email address
25 n (optional) gives column for a name 26 n (optional) gives column for a name
46 %(pai)s 'him'/'her' 47 %(pai)s 'him'/'her'
47 %(pgi)s 'his/her' 48 %(pgi)s 'his/her'
48 49
49 All column indices are 1-origin, as for cut''' 50 All column indices are 1-origin, as for cut'''
50 51
51 import smtplib, sys, re, os.path, codecs 52 import smtplib, sys, re, os, os.path, codecs
52 from email.mime.text import MIMEText 53 from email.mime.text import MIMEText
53 54
54 addrPat=re.compile("<([^>]*)>") 55 addrPat=re.compile("<([^>]*)>")
55 56
56 def usage(hint=None): 57 def usage(hint=None):
249 msg.attach(atm) 250 msg.attach(atm)
250 251
251 dryrun=False 252 dryrun=False
252 replyTo=None 253 replyTo=None
253 sigfile=None 254 sigfile=None
255 fromName='%s@%s'%(os.getenv('USER'),os.getenv('HOSTNAME'))
254 256
255 sys.argv.pop(0) 257 sys.argv.pop(0)
256 doSub=False 258 doSub=False
257 259
258 pats=[] 260 pats=[]
328 sys.argv.pop(0) 330 sys.argv.pop(0)
329 if sys.argv: 331 if sys.argv:
330 replyTo=sys.argv.pop(0) 332 replyTo=sys.argv.pop(0)
331 else: 333 else:
332 usage('reply to') 334 usage('reply to')
335 elif sys.argv[0]=='-f':
336 sys.argv.pop(0)
337 if sys.argv:
338 fromName=sys.argv.pop(0)
339 else:
340 usage('from name')
333 elif sys.argv[0]=='-m': 341 elif sys.argv[0]=='-m':
334 sys.argv.pop(0) 342 sys.argv.pop(0)
335 if sys.argv: 343 if sys.argv:
336 sigfile=sys.argv.pop(0) 344 sigfile=sys.argv.pop(0)
337 else: 345 else:
432 if replyTo is not None: 440 if replyTo is not None:
433 msg["Reply-To"]=replyTo 441 msg["Reply-To"]=replyTo
434 if dryrun: 442 if dryrun:
435 print recips 443 print recips
436 print msg.keys() 444 print msg.keys()
445 print 'From: %s'%fromName
437 print msg.as_string() 446 print msg.as_string()
438 exit() 447 exit()
439 print "mailing to %s"%recips 448 print "mailing to %s"%recips
440 mailer.sendmail("ht@inf.ed.ac.uk",recips,msg.as_string()) 449 mailer.sendmail(fromName,recips,msg.as_string())
441 mailer.quit() 450 mailer.quit()