Make everything safe for unicode (this should fix the elog modules that
authorZac Medico <zmedico@gentoo.org>
Sun, 9 Aug 2009 23:16:50 +0000 (23:16 -0000)
committerZac Medico <zmedico@gentoo.org>
Sun, 9 Aug 2009 23:16:50 +0000 (23:16 -0000)
send mail).

svn path=/main/trunk/; revision=13965

pym/portage/mail.py

index 5f1cc11a6fcd22a7aee36920d461d17f82fbd901..9779fac86b186f8a0a70669e63b719937d493874 100644 (file)
@@ -10,6 +10,17 @@ from email.MIMEBase import MIMEBase as BaseMessage
 from email.header import Header
 
 def create_message(sender, recipient, subject, body, attachments=None):
+
+       if sys.hexversion < 0x3000000:
+               if isinstance(sender, unicode):
+                       sender = sender.encode('utf_8', 'replace')
+               if isinstance(recipient, unicode):
+                       recipient = recipient.encode('utf_8', 'replace')
+               if isinstance(subject, unicode):
+                       subject = subject.encode('utf_8', 'replace')
+               if isinstance(body, unicode):
+                       body = body.encode('utf_8', 'replace')
+
        if attachments == None:
                mymessage = TextMessage(body)
        else:
@@ -19,6 +30,8 @@ def create_message(sender, recipient, subject, body, attachments=None):
                        if isinstance(x, BaseMessage):
                                mymessage.attach(x)
                        elif isinstance(x, basestring):
+                               if sys.hexversion < 0x3000000 and isinstance(x, unicode):
+                                       x = x.encode('utf_8', 'replace')
                                mymessage.attach(TextMessage(x))
                        else:
                                raise portage.exception.PortageException("Can't handle type of attachment: %s" % type(x))
@@ -66,7 +79,21 @@ def send_mail(mysettings, message):
                myrecipient = mysettings["PORTAGE_ELOG_MAILURI"]
        
        myfrom = message.get("From")
-               
+
+       if sys.hexversion < 0x3000000:
+               if isinstance(myrecipient, unicode):
+                       myrecipient = myrecipient.encode('utf_8', 'replace')
+               if isinstance(mymailhost, unicode):
+                       mymailhost = mymailhost.encode('utf_8', 'replace')
+               if isinstance(mymailport, unicode):
+                       mymailport = mymailport.encode('utf_8', 'replace')
+               if isinstance(myfrom, unicode):
+                       myfrom = myfrom.encode('utf_8', 'replace')
+               if isinstance(mymailuser, unicode):
+                       mymailuser = mymailuser.encode('utf_8', 'replace')
+               if isinstance(mymailpasswd, unicode):
+                       mymailpasswd = mymailpasswd.encode('utf_8', 'replace')
+
        # user wants to use a sendmail binary instead of smtp
        if mymailhost[0] == os.sep and os.path.exists(mymailhost):
                fd = os.popen(mymailhost+" -f "+myfrom+" "+myrecipient, "w")