]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - modules/mailer.py
Fix StringIO usage under python 3
[projects/pld-ftp-admin.git] / modules / mailer.py
CommitLineData
972c18d7
MM
1# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3import time
4import os
5import sys
5e91d166
JR
6if sys.version_info[0] == 2:
7 import StringIO
8else:
d1cea6ea 9 import io as StringIO
972c18d7 10
59ecaf12
MM
11import config
12cval=config.value
972c18d7
MM
13
14class Message:
15 def __init__(self):
16 self.headers = {}
17 self.body = StringIO.StringIO()
59ecaf12 18 self.__set_std_headers()
972c18d7
MM
19
20 def set_header(self, n, v):
21 self.headers[n] = v
22
23 def set_headers(self, to = None, cc = None, subject = None):
24 if to != None:
25 self.set_header("To", to)
26 if cc != None:
27 self.set_header("Cc", cc)
28 if subject != None:
29 self.set_header("Subject", subject)
30
31 def write_line(self, l):
32 self.body.write("%s\n" % l)
33
34 def write(self, s):
35 self.body.write(s)
36
59ecaf12
MM
37 def send(self):
38 send_sendmail = "/usr/sbin/sendmail -t"
39 f = os.popen(send_sendmail, "w")
40 self.__write_to(f)
41 f.close()
972c18d7 42
59ecaf12 43 def __set_std_headers(self):
972c18d7
MM
44 self.headers["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())
45 self.headers["Message-ID"] = "<pld-builder.%f.%d@%s>" \
46 % (time.time(), os.getpid(), os.uname()[1])
59ecaf12
MM
47 self.headers["From"] = cval['from_field']
48 self.headers["X-PLD-Builder"] = cval['xpldbuilder']
972c18d7 49
59ecaf12 50 def __write_to(self, f):
972c18d7
MM
51 for k, v in self.headers.items():
52 f.write("%s: %s\n" % (k, v))
53 f.write("\n")
54 self.body.seek(0)
59ecaf12
MM
55 self.__sendfile(self.body, f)
56
57 def __sendfile(self, src, dst):
58 cnt = 0
59 while 1:
60 s = src.read(10000)
61 if s == "": break
62 cnt += len(s)
63 dst.write(s)
64 return cnt
65
66
This page took 1.154941 seconds and 4 git commands to generate.