From 7608f97f12887f079a210a9abd02079e85bbe180 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sun, 9 Aug 2009 23:16:50 +0000 Subject: Make everything safe for unicode (this should fix the elog modules that send mail). svn path=/main/trunk/; revision=13965 --- pym/portage/mail.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pym/portage/mail.py b/pym/portage/mail.py index 5f1cc11a6..9779fac86 100644 --- a/pym/portage/mail.py +++ b/pym/portage/mail.py @@ -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") -- cgit v1.2.3-1-g7c22