Refactored the Email encoding code.

Sat, 20 Feb 2010 09:06:07 +0000

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 20 Feb 2010 09:06:07 +0000
changeset 120
c0ef4b5e679a
parent 119
071ac17b8179
child 121
2ce3ab9417e6

Refactored the Email encoding code.

Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric5.UI.EmailDialog.html file | annotate | diff | comparison | revisions
UI/EmailDialog.py file | annotate | diff | comparison | revisions
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Mon Feb 15 18:47:35 2010 +0000
+++ b/Documentation/Help/source.qhp	Sat Feb 20 09:06:07 2010 +0000
@@ -3753,6 +3753,8 @@
       <keyword name="EmailDialog (Constructor)" id="EmailDialog (Constructor)" ref="eric5.UI.EmailDialog.html#EmailDialog.__init__" />
       <keyword name="EmailDialog.__createMultipartMail" id="EmailDialog.__createMultipartMail" ref="eric5.UI.EmailDialog.html#EmailDialog.__createMultipartMail" />
       <keyword name="EmailDialog.__createSimpleMail" id="EmailDialog.__createSimpleMail" ref="eric5.UI.EmailDialog.html#EmailDialog.__createSimpleMail" />
+      <keyword name="EmailDialog.__encodedHeader" id="EmailDialog.__encodedHeader" ref="eric5.UI.EmailDialog.html#EmailDialog.__encodedHeader" />
+      <keyword name="EmailDialog.__encodedText" id="EmailDialog.__encodedText" ref="eric5.UI.EmailDialog.html#EmailDialog.__encodedText" />
       <keyword name="EmailDialog.__sendmail" id="EmailDialog.__sendmail" ref="eric5.UI.EmailDialog.html#EmailDialog.__sendmail" />
       <keyword name="EmailDialog.attachFile" id="EmailDialog.attachFile" ref="eric5.UI.EmailDialog.html#EmailDialog.attachFile" />
       <keyword name="EmailDialog.keyPressEvent" id="EmailDialog.keyPressEvent" ref="eric5.UI.EmailDialog.html#EmailDialog.keyPressEvent" />
--- a/Documentation/Source/eric5.UI.EmailDialog.html	Mon Feb 15 18:47:35 2010 +0000
+++ b/Documentation/Source/eric5.UI.EmailDialog.html	Sat Feb 20 09:06:07 2010 +0000
@@ -66,6 +66,12 @@
 <td><a href="#EmailDialog.__createSimpleMail">__createSimpleMail</a></td>
 <td>Private method to create a simple mail message.</td>
 </tr><tr>
+<td><a href="#EmailDialog.__encodedHeader">__encodedHeader</a></td>
+<td>Private method to create a correctly encoded mail header.</td>
+</tr><tr>
+<td><a href="#EmailDialog.__encodedText">__encodedText</a></td>
+<td>Private method to create a MIMEText message with correct encoding.</td>
+</tr><tr>
 <td><a href="#EmailDialog.__sendmail">__sendmail</a></td>
 <td>Private method to actually send the message.</td>
 </tr><tr>
@@ -130,6 +136,36 @@
 <dd>
 string containing the mail message
 </dd>
+</dl><a NAME="EmailDialog.__encodedHeader" ID="EmailDialog.__encodedHeader"></a>
+<h4>EmailDialog.__encodedHeader</h4>
+<b>__encodedHeader</b>(<i>txt</i>)
+<p>
+        Private method to create a correctly encoded mail header.
+</p><dl>
+<dt><i>txt</i></dt>
+<dd>
+header text to encode (string)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+encoded header (email.header.Header)
+</dd>
+</dl><a NAME="EmailDialog.__encodedText" ID="EmailDialog.__encodedText"></a>
+<h4>EmailDialog.__encodedText</h4>
+<b>__encodedText</b>(<i>txt</i>)
+<p>
+        Private method to create a MIMEText message with correct encoding.
+</p><dl>
+<dt><i>txt</i></dt>
+<dd>
+text to be put into the MIMEText object (string)
+</dd>
+</dl><dl>
+<dt>Returns:</dt>
+<dd>
+MIMEText object
+</dd>
 </dl><a NAME="EmailDialog.__sendmail" ID="EmailDialog.__sendmail"></a>
 <h4>EmailDialog.__sendmail</h4>
 <b>__sendmail</b>(<i>msg</i>)
--- a/UI/EmailDialog.py	Mon Feb 15 18:47:35 2010 +0000
+++ b/UI/EmailDialog.py	Sat Feb 20 09:06:07 2010 +0000
@@ -157,32 +157,51 @@
                     pass
             self.accept()
         
+    def __encodedText(self, txt):
+        """
+        Private method to create a MIMEText message with correct encoding.
+        
+        @param txt text to be put into the MIMEText object (string)
+        @return MIMEText object
+        """
+        try:
+            txt.encode("us-ascii")
+            return MIMEText(txt)
+        except UnicodeEncodeError:
+            coding = Preferences.getSystem("StringEncoding")
+            return MIMEText(txt.encode(coding), _charset = coding)
+        
+    def __encodedHeader(self, txt):
+        """
+        Private method to create a correctly encoded mail header.
+        
+        @param txt header text to encode (string)
+        @return encoded header (email.header.Header)
+        """
+        try:
+            txt.encode("us-ascii")
+            return Header(txt)
+        except UnicodeEncodeError:
+            coding = Preferences.getSystem("StringEncoding")
+            return Header(txt, coding)
+        
     def __createSimpleMail(self):
         """
         Private method to create a simple mail message.
         
         @return string containing the mail message
         """
-        coding = Preferences.getSystem("StringEncoding")
         msgtext = "%s\r\n----\r\n%s----\r\n%s----\r\n%s" % \
             (self.message.toPlainText(), 
              Utilities.generateVersionInfo("\r\n"), 
              Utilities.generatePluginsVersionInfo("\r\n"), 
              Utilities.generateDistroInfo("\r\n"))
         
-        try:
-            msgtext.encode("us-ascii")
-            msg = MIMEText(msgtext)
-        except UnicodeEncodeError:
-            msg = MIMEText(msgtext.encode(coding), _charset = coding)
+        msg = self.__encodedText(msgtext)
         msg['From']    = Preferences.getUser("Email")
         msg['To']      = self.__toAddress
         subject = '[eric5] %s' % self.subject.text()
-        try:
-            subject.encode("us-ascii")
-            msg['Subject'] = subject
-        except UnicodeEncodeError:
-            msg['Subject'] = Header(subject, coding)
+        msg['Subject'] = self.__encodedHeader(subject)
         
         return msg.as_string()
         
@@ -192,7 +211,6 @@
         
         @return string containing the mail message
         """
-        coding = Preferences.getSystem("StringEncoding")
         mpPreamble = ("This is a MIME-encoded message with attachments. "
             "If you see this message, your mail client is not "
             "capable of displaying the attachments.")
@@ -208,20 +226,12 @@
         msg['From']    = Preferences.getUser("Email")
         msg['To']      = self.__toAddress
         subject = '[eric5] %s' % self.subject.text()
-        try:
-            subject.encode("us-ascii")
-            msg['Subject'] = subject
-        except UnicodeEncodeError:
-            msg['Subject'] = Header(subject, coding)
+        msg['Subject'] = self.__encodedHeader(subject)
         msg.preamble = mpPreamble
         msg.epilogue = ''
         
         # second part is intended to be read
-        try:
-            msgtext.encode("us-ascii")
-            att = MIMEText(msgtext)
-        except UnicodeEncodeError:
-            att = MIMEText(msgtext.encode(coding), _charset = coding)
+        att = self.__encodedText(msgtext)
         msg.attach(att)
         
         # next parts contain the attachments

eric ide

mercurial