Package backend :: Package common :: Module rhnMail
[hide private]
[frames] | no frames]

Source Code for Module backend.common.rhnMail

 1  # 
 2  # Copyright (c) 2008--2016 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU General Public License, 
 5  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 6  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 7  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 8  # along with this software; if not, see 
 9  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
10  # 
11  # Red Hat trademarks are not licensed under GPLv2. No permission is 
12  # granted to use or replicate Red Hat trademarks that are incorporated 
13  # in this software or its documentation. 
14  # 
15  # this module implements the send mail support 
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  # check if the headers have the minimum required fields 
27   
28   
29 -def __check_headers(h):
30 if not isinstance(h, type({})) or not hasattr(h, "has_key"): 31 # does not look like a dictionary 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 # check the headers for sanity cases and send the mail 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