source: branches/testScripts/NBemail.py @ 617

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

Moved email functions to their own file

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