#******************************************************************** # # Copyright (C) 2005-2006 Hari Krishna Dara # # This file is part of p4admin. # # p4admin is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # p4admin is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # #******************************************************************* # Import smtplib for the actual sending function import smtplib # Import the email modules we'll need from email.MIMEText import MIMEText import logging log = logging.getLogger(__name__) def sendError(message, includeLog=True): import config import utils log.error(message) if includeLog: if config.logFileShared != None: message += "\n\nPlease see the logfile for details: "+\ config.logFileShared # TODO: Configurable number. Do only if logging is enabled. last100Lines = utils.execute('tail -100 '+config.logFilePath, verbosity=2) message = message+"\n\nHere are the last 100 lines of the log file:\n"+last100Lines sendMail('Failure notification', message, config.fromAddr, config.toAddrs, config.ccAddrs, config.smtpServer) def sendWarning(message): import config sendMail('Warning notification', message, config.fromAddr, config.toAddrs, config.ccAddrs, config.smtpServer) def sendInfo(subject, message): import config sendMail(subject, message, config.fromAddr, config.toAddrs, config.ccAddrs, config.smtpServer) def sendMail(subject, message, fromAddr, toAddrs, ccAddrs, smtpServer): log.info(subject+":"+message) # Create a text/plain message msg = MIMEText(message) # me == the sender's email address # you == the recipient's email address msg['Subject'] = 'p4admin: '+subject msg['From'] = fromAddr msg['To'] = ', '.join(toAddrs) msg['Cc'] = ', '.join(ccAddrs) # Send the message via our own SMTP server, but don't include the # envelope header. s = smtplib.SMTP() if log.getEffectiveLevel() <= logging.DEBUG: s.set_debuglevel(1) s.connect(smtpServer) s.sendmail(fromAddr, toAddrs+ccAddrs, msg.as_string()) s.close()