"Fossies" - the Fresh Open Source Software Archive 
Member "svnchecker-0.3/handlers/Mail.py" (9 Jul 2008, 2495 Bytes) of package /linux/privat/old/svnchecker-0.3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "Mail.py" see the
Fossies "Dox" file reference documentation.
1 # Copyright 2008 German Aerospace Center (DLR)
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """ Send the message as E-Mail. """
16
17 import smtplib
18 import socket
19 import datetime
20
21
22 class MailHandler:
23 def __init__(self, transaction, config, check, msg):
24 """ Main function. """
25 self.transaction = transaction
26 self.config = config
27 self.check = check
28 self.msg = msg
29
30 def getLogSubject(self):
31 fromID = self.transaction.getUserID()
32 return "SVN update by %s at %s" % (fromID, datetime.datetime.now().strftime("%H:%M - %d.%m.%Y"))
33
34 def getErrorSubject(self):
35 fromID = self.transaction.getUserID()
36 return "Checkin error by '%s' in check '%s'" % (fromID, self.check)
37
38 def getLogAddresses(self):
39 return self.config.getArray("%s.SuccessAddresses" % self.check)
40
41 def getErrorAddresses(self):
42 return self.config.getArray("%s.FailureAddresses" % self.check)
43
44 def createMail(self, fromAddress, toAddress, subject, content):
45 """ Creates the content of the mail. """
46 return ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromAddress, toAddress, subject)) + content
47
48 def sendMail(self, subject, toAddresses):
49 """ Actually send the message. """
50 fromAddress = self.transaction.getUserID() + "@" + socket.gethostname()
51 server = smtplib.SMTP('localhost')
52 server.set_debuglevel(0)
53 for toAddress in toAddresses:
54 mail = self.createMail(fromAddress, toAddress, subject, self.msg)
55 server.sendmail(fromAddress, toAddress, mail)
56 server.quit()
57
58
59 def run(transaction, config, check, msg, exitCode):
60 handler = MailHandler(transaction, config, check, msg)
61
62 if exitCode == 0:
63 subject = handler.getLogSubject()
64 addresses = handler.getLogAddresses()
65 else:
66 subject = handler.getErrorSubject()
67 addresses = handler.getErrorAddresses()
68
69 handler.sendMail(subject, addresses)