1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import os
19 import smtplib
20
21 from rhn.connections import idn_puny_to_unicode
22
23 from spacewalk.common.rhnConfig import CFG, PRODUCT_NAME
24 from spacewalk.common.stringutils import to_string
25
26
27
28
30 if not isinstance(h, type({})) or not hasattr(h, "has_key"):
31
32 h = {}
33 if "Subject" not in h:
34 h["Subject"] = "%s System Mail From %s" % (PRODUCT_NAME,
35 idn_puny_to_unicode(os.uname()[1]))
36 if "To" not in h:
37 to = CFG.TRACEBACK_MAIL
38 else:
39 to = h["To"]
40 if "Content-Type" not in h:
41 h["Content-Type"] = "text/plain; charset=utf-8"
42 if isinstance(to, (type([]), type(()))):
43 toaddrs = to
44 to = ', '.join(to)
45 else:
46 toaddrs = to.split(',')
47 h["To"] = to
48 return [h, toaddrs]
49
50
51
52
53 -def send(headers, body, sender=None):
54 (headers, toaddrs) = __check_headers(headers)
55 if sender is None:
56 sender = headers["From"]
57 joined_headers = u''
58 for h in headers.keys():
59 joined_headers += u"%s: %s\n" % (h, headers[h])
60
61 server = smtplib.SMTP('localhost')
62 msg = "%s\n%s\n" % (to_string(joined_headers), to_string(body))
63 server.sendmail(sender, toaddrs, msg)
64 server.quit()
65