Add new ** for package.keywords token to bypass keyword visibility layer (trunk r5758...
[portage.git] / pym / portage_mail.py
1 # portage.py -- core Portage functionality
2 # Copyright 1998-2004 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4 # $Id: portage.py 3483 2006-06-10 21:40:40Z genone $
5
6 import portage_exception, socket, smtplib, os, sys, time
7 from email.MIMEText import MIMEText as TextMessage
8 from email.MIMEMultipart import MIMEMultipart as MultipartMessage
9 from email.MIMEBase import MIMEBase as BaseMessage
10
11 def create_message(sender, recipient, subject, body, attachments=None):
12         if attachments == None:
13                 mymessage = TextMessage(body)
14         else:
15                 mymessage = MultipartMessage()
16                 mymessage.attach(TextMessage(body))
17                 for x in attachments:
18                         if isinstance(x, BaseMessage):
19                                 mymessage.attach(x)
20                         elif isinstance(x, str):
21                                 mymessage.attach(TextMessage(x))
22                         else:
23                                 raise portage_exception.PortageException("Can't handle type of attachment: %s" % type(x))
24
25         mymessage.set_unixfrom(sender)
26         mymessage["To"] = recipient
27         mymessage["From"] = sender
28         mymessage["Subject"] = subject
29         mymessage["Date"] = time.strftime("%a, %d %b %Y %H:%M:%S %z")
30         
31         return mymessage
32
33 def send_mail(mysettings, message):
34         mymailhost = "localhost"
35         mymailport = 25
36         mymailuser = ""
37         mymailpasswd = ""
38         myrecipient = "root@localhost"
39         
40         # Syntax for PORTAGE_ELOG_MAILURI (if defined):
41         # adress [[user:passwd@]mailserver[:port]]
42         # where adress:     recipient adress
43         #       user:       username for smtp auth (defaults to none)
44         #       passwd:     password for smtp auth (defaults to none)
45         #       mailserver: smtp server that should be used to deliver the mail (defaults to localhost)
46         #                                       alternatively this can also be the absolute path to a sendmail binary if you don't want to use smtp
47         #       port:       port to use on the given smtp server (defaults to 25, values > 100000 indicate that starttls should be used on (port-100000))
48         if " " in mysettings["PORTAGE_ELOG_MAILURI"]:
49                 myrecipient, mymailuri = mysettings["PORTAGE_ELOG_MAILURI"].split()
50                 if "@" in mymailuri:
51                         myauthdata, myconndata = mymailuri.rsplit("@", 1)
52                         try:
53                                 mymailuser,mymailpasswd = myauthdata.split(":")
54                         except ValueError:
55                                 print "!!! invalid SMTP AUTH configuration, trying unauthenticated ..."
56                 else:
57                         myconndata = mymailuri
58                 if ":" in myconndata:
59                         mymailhost,mymailport = myconndata.split(":")
60                 else:
61                         mymailhost = myconndata
62         else:
63                 myrecipient = mysettings["PORTAGE_ELOG_MAILURI"]
64         
65         myfrom = message.get("From")
66                 
67         # user wants to use a sendmail binary instead of smtp
68         if mymailhost[0] == os.sep and os.path.exists(mymailhost):
69                 fd = os.popen(mymailhost+" -f "+myfrom+" "+myrecipient, "w")
70                 fd.write(message.as_string())
71                 if fd.close() != None:
72                         sys.stderr.write("!!! %s returned with a non-zero exit code. This generally indicates an error.\n" % mymailhost)
73         else:
74                 try:
75                         if int(mymailport) > 100000:
76                                 myconn = smtplib.SMTP(mymailhost, int(mymailport) - 100000)
77                                 myconn.starttls()
78                         else:
79                                 myconn = smtplib.SMTP(mymailhost, mymailport)
80                         if mymailuser != "" and mymailpasswd != "":
81                                 myconn.login(mymailuser, mymailpasswd)
82                         myconn.sendmail(myfrom, myrecipient, message.as_string())
83                         myconn.quit()
84                 except smtplib.SMTPException, e:
85                         raise portage_exception.PortageException("!!! An error occured while trying to send logmail:\n"+str(e))
86                 except socket.error, e:
87                         raise portage_exception.PortageException("!!! A network error occured while trying to send logmail:\n"+str(e)+"\nSure you configured PORTAGE_ELOG_MAILURI correctly?")
88         return
89