-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailfile.py
86 lines (81 loc) · 3.37 KB
/
mailfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import subprocess
import sys, os, socket
import smtplib
from . import log
try:
from email.mime.multipart import MIMEMultipart
log.info('Loaded python3 MIMEMultipart')
except ModuleNotFoundError:
from email.MIMEMultipart import MIMEMultipart
log.info('Loaded python MIMEMultipart')
try:
from email.mime.text import MIMEText
except ModuleNotFoundError:
from email.MIMEText import MIMEText
try:
from email.mime.base import MIMEBase
except ModuleNotFoundError:
from email.MIMEBase import MIMEBase
from email import encoders
def send_email(msg, fromaddr, toaddr):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
try:
server.login("[email protected]", os.environ['MAILLOGIN'])
except KeyError:
log.error('password for '+fromaddr+' not found')
log.error('Please ensure $MAILLOGIN is set')
if fromaddr == '[email protected]':
log.error('Contact Narsi Anugu for password for '+fromaddr)
server.quit()
sys.exit()
except SMTPAuthenticationError:
log.error('Unable to send email!')
log.error('Gmail does not recognise your device.')
log.info('Either login to gmail for [email protected] and approve')
log.info('this device or contact Narsi Anugu or Claire Davies to do this')
log.info('for you.')
server.sendmail(fromaddr, toaddr, msg.as_string())
server.quit()
def sendSummary(toaddr,fromaddr,outFile,inDir):
"""
Emails the summary report PDF file for the reduced and calibrated
night of observations to 'addr'
"""
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
filename = outFile.split('/')[-1]
msg['Subject'] = 'MIRC-X redcal summary '+filename
if socket.gethostname() == 'mircx':
# this is where we need to change to include text from archive log
bod = []
if os.path.isfile(inDir+'/mircx_archivedata.summary.log'):
subprocess.call('cat '+inDir+'/mircx_archivedata.log | grep "ERROR" | grep -v "*** ERROR" | sed -e "y#:#_#" >> '+inDir+'/mircx_archivedata.summary.log',shell=True)
print('cat '+inDir+'/mircx_archivedata.log | grep "ERROR" | grep -v "*** ERROR" | sed -e "y#:#_#" >> '+inDir+'/mircx_archivedata.summary.log')
with open(inDir+'/mircx_archivedata.summary.log') as readin:
for line in readin:
if line.strip() not in bod:
bod.append(line.strip())
bod.append('\n\n')
else:
bod.append('File '+inDir+'/mircx_archivedata.summary.log not found')
bod.append('MIRC-X redcal summary '+filename+'\n')
body = '\n'.join(bod)
else:
body = 'MIRC-X redcal summary '+filename+'\n'
msg.attach(MIMEText(body, 'plain'))
attachment = open(outFile, 'rb')
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= %s" % filename)
msg.attach(part)
try:
send_email(msg, fromaddr, toaddr)
log.info('Emailed summary report ('+filename+') to:')
log.info(toaddr)
except smtplib.SMTPAuthenticationError:
log.error('Failed to send summary report ('+filename+') to '+toaddr)
log.error('Check with Narsi Anugu for permissions')
sys.exit()