source: branches/testScripts/NBemail.py @ 618

Last change on this file since 618 was 618, checked in by jpfasano, 13 years ago

emailed messages now contain some information about the host machine (name
and OS).
userConfig variables are conditionally set based on hostname

  • Property svn:executable set to *
File size: 3.1 KB
RevLine 
[617]1#! /usr/bin/env python
2
3import os
4import sys
[618]5from socket import gethostname
[617]6import smtplib
7
8import NBuserConfig
9import NBprojectConfig
10import NBlogMessages
11
12#------------------------------------------------------------------------
13#
14# This file contains the functions that deal with email
15#
16#------------------------------------------------------------------------
17
18#------------------------------------------------------------------------
19# Send email typically about an error.
20#  project: coin project name
21#  cmd: command being exucuted. perhaps: "svn update", "./configure",
22#       "make".
23#  cmdMsgs: the messages generated by cmd.  This will typically contain
24#       errors issued by cmd.
25#------------------------------------------------------------------------
26def sendCmdMsgs(project,cmdMsgs,cmd):
27  curDir = os.getcwd()
28 
29  toAddrs = [unscrambleAddress(NBuserConfig.MY_EMAIL_ADDR)]
30  if NBprojectConfig.PROJECT_EMAIL_ADDRS.has_key(project) \
31     and \
32     NBuserConfig.SEND_MAIL_TO_PROJECT_MANAGER:
33    scrambledEmailAddress=NBprojectConfig.PROJECT_EMAIL_ADDRS[project]
34    unscrambledEmailAddress=unscrambleAddress(scrambledEmailAddress)
35    toAddrs.append(unscrambledEmailAddress)
36
37  subject = project + " build problem when running '" + cmd +"'"
38
39  emailMsg  = "'" + cmd + "' from directory " + curDir + " failed.\n\n"
[618]40
41  emailMsg += "Operating System: "+sys.platform+" "+os.name+"\n"
42  emailMsg += "Host name: "+gethostname()+"\n\n"
43
[617]44  emailMsg += "'" + cmd + "' messages are:\n" 
45  emailMsg += cmdMsgs
46  send(toAddrs,subject,emailMsg)
47  NBlogMessages.writeMessage( "  email sent regarding "+project+" running '"+cmd+"'" )
48
49#------------------------------------------------------------------------
50# Send email
51#------------------------------------------------------------------------
52def send(toAddrs,subject,message):
53
54  sender = unscrambleAddress(NBuserConfig.SENDER_EMAIL_ADDR) 
55  msgWHeader = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
56       % (sender, ", ".join(toAddrs), subject))
57  msgWHeader += message
58 
59  # Get smpt server password
60  if os.path.isfile(NBuserConfig.SMTP_PASSWORD_FILENAME) :
61    pwFilePtr = open(NBuserConfig.SMTP_PASSWORD_FILENAME,'r')
62    smtppass  = pwFilePtr.read().strip()
63    pwFilePtr.close()
64  else :
65    NBlogMessages.writeMessage( "Failure reading pwFileName=" + NBuserConfig.SMTP_PASSWORD_FILENAME )
66    sys.exit(1)
67   
68  session = smtplib.SMTP(NBuserConfig.SMTP_SERVER_NAME,NBuserConfig.SMTP_SERVER_PORT)
69  #session.set_debuglevel(1)
70  if NBuserConfig.SMTP_SSL_SERVER==1 :
71    session.ehlo('x')
72    session.starttls()
73    session.ehlo('x') 
74  session.login(unscrambleAddress(NBuserConfig.SMTP_USER_NAME),smtppass)
75
76  rc = session.sendmail(sender,toAddrs,msgWHeader)
77  if rc!={} :
78    NBlogMessages.writeMessage( 'session.sendmail rc='  )
79    NBlogMessages.writeMessage( rc )
80  session.quit()
81
82#------------------------------------------------------------------------
83# Decrypt email address
84#------------------------------------------------------------------------
85def unscrambleAddress( scrambledEmailAddress ) :
86  retVal = scrambledEmailAddress
87  retVal = retVal.replace(' _AT_ ','@')
88  retVal = retVal.replace(' _DOT_ ','.')
89  return retVal
90
Note: See TracBrowser for help on using the repository browser.