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 $
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
11 def create_message(sender, recipient, subject, body, attachments=None):
12 if attachments == None:
13 mymessage = TextMessage(body)
15 mymessage = MultipartMessage()
16 mymessage.attach(TextMessage(body))
18 if isinstance(x, BaseMessage):
20 elif isinstance(x, str):
21 mymessage.attach(TextMessage(x))
23 raise portage_exception.PortageException("Can't handle type of attachment: %s" % type(x))
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")
33 def send_mail(mysettings, message):
34 mymailhost = "localhost"
38 myrecipient = "root@localhost"
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()
51 myauthdata, myconndata = mymailuri.rsplit("@", 1)
53 mymailuser,mymailpasswd = myauthdata.split(":")
55 print "!!! invalid SMTP AUTH configuration, trying unauthenticated ..."
57 myconndata = mymailuri
59 mymailhost,mymailport = myconndata.split(":")
61 mymailhost = myconndata
63 myrecipient = mysettings["PORTAGE_ELOG_MAILURI"]
65 myfrom = message.get("From")
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)
75 if int(mymailport) > 100000:
76 myconn = smtplib.SMTP(mymailhost, int(mymailport) - 100000)
79 myconn = smtplib.SMTP(mymailhost, mymailport)
80 if mymailuser != "" and mymailpasswd != "":
81 myconn.login(mymailuser, mymailpasswd)
82 myconn.sendmail(myfrom, myrecipient, message.as_string())
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?")