bug: treat mails as binary files, not as utf-8
authorStefan Huber <shuber@sthu.org>
Wed, 12 Mar 2014 12:41:17 +0000 (13:41 +0100)
committerStefan Huber <shuber@sthu.org>
Wed, 12 Mar 2014 12:41:17 +0000 (13:41 +0100)
If a mail is not in utf-8 then decoding errors may be thrown by python.
Read/write mails from stdin/files as binary files, without interpreting
them as utf-8.

smailq

diff --git a/smailq b/smailq
index 7d5286a..def0a0a 100755 (executable)
--- a/smailq
+++ b/smailq
@@ -163,8 +163,8 @@ class MailQueue:
         info['to'] = ""
         info['subject'] = ""
 
-        with open(mailfn, "r") as f:
-            mail = f.readlines()
+        with open(mailfn, "rb") as f:
+            mail = f.read().decode('utf8', 'replace').splitlines()
 
             for l in mail:
                 if l.startswith("Subject:"):
@@ -234,7 +234,7 @@ class MailQueue:
 
         # Read the mail
         mailfn = self.conf.getmailfn(id)
-        mailf = open(mailfn, "r")
+        mailf = open(mailfn, "rb")
 
         # Read the options
         msaargsfn = self.conf.getmsaargsfn(id)
@@ -284,7 +284,7 @@ class MailQueue:
 
         # Write the mail
         mailfn = self.conf.getmailfn(id)
-        with open(mailfn, "w") as f:
+        with open(mailfn, "wb") as f:
             f.write(mail)
 
         # Write the options
@@ -458,7 +458,7 @@ if __name__ == "__main__":
 
             mq = MailQueue(conf)
             if cmd == "--send":
-                mail = sys.stdin.read()
+                mail = sys.stdin.buffer.read()
                 mq.sendmail(mail, nooptargs)
             elif cmd == "--list":
                 mq.listqueue()