Add script to send stored mails.
authorIvan Alejandro <ivanalejandro0@gmail.com>
Wed, 22 Jan 2014 17:13:58 +0000 (14:13 -0300)
committerIvan Alejandro <ivanalejandro0@gmail.com>
Wed, 22 Jan 2014 17:29:53 +0000 (14:29 -0300)
- Add a problematic email to emails/.
- Fix error: local variable result referenced before assignment

src/emails/non-ascii-plain-subject.message [new file with mode: 0644]
src/gmail.py
src/send-mail-strings.py [new file with mode: 0755]

diff --git a/src/emails/non-ascii-plain-subject.message b/src/emails/non-ascii-plain-subject.message
new file mode 100644 (file)
index 0000000..25c44f4
--- /dev/null
@@ -0,0 +1,14 @@
+Content-Type: multipart/mixed; boundary="===============1817442041=="
+MIME-Version: 1.0
+Subject: Héllò thërê - stored message - ÿȩāḩ
+From: {FROM}
+To: {TO}
+
+--===============1817442041==
+Content-Type: text/plain; charset="utf-8"
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Howdy from python!
+The subject: Héllò thërê - stored message - ÿȩāḩ
+--===============1817442041==--
index 34579b6..4e7af04 100644 (file)
@@ -117,9 +117,29 @@ class GMail(object):
         try:
             result = self._server.sendmail(self._from, to_addr_list, message)
         except Exception as e:
-            result['Exception'] = repr(e)
+            result = {'Exception': repr(e)}
 
         if problems:
             result['Attachments'] = problems
 
         return result
+
+    def send_email_string(self, mail, to):
+        """
+        Sends an email from an already created email as a string.
+
+        :param mail: the mail to send
+        :type mail: str
+        :param to: a address to send the email.
+        :type to: str
+
+        :return: an empty dict if everything went ok or
+                 the problems result of sending the email(s).
+        :rtype: dict
+        """
+        try:
+            result = self._server.sendmail(self._from, [to], mail)
+        except Exception as e:
+            result = {'Exception': repr(e)}
+
+        return result
diff --git a/src/send-mail-strings.py b/src/send-mail-strings.py
new file mode 100755 (executable)
index 0000000..b682c90
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+# encoding: utf-8
+# import getpass
+import os
+import sys
+
+from ConfigParser import SafeConfigParser
+
+from gmail import GMail
+
+
+# Read credentials from options file
+parser = SafeConfigParser()
+parser.read('options.cfg')
+try:
+    FROM = parser.get('Credentials', 'account')
+    SECRET = parser.get('Credentials', 'password')
+    TO = [parser.get('Configs', 'to')]
+    MAX_MAILS = parser.getint('Configs', 'mails_amount')
+except Exception as e:
+    print "Problem reading options.cfg"
+    print "Exception: {0!r}".format(e)
+    sys.exit()
+
+
+# create the GMail global object
+gmail = GMail(FROM, SECRET)
+
+directory = './emails/'
+file_list = []
+
+for filename in os.listdir(directory):
+    path = os.path.join(directory, filename)
+    if os.path.isfile(path):
+        file_list.append(path)
+
+print "Sending {0} mail(s)...".format(len(file_list))
+
+for mail_file in file_list:
+    print "Sending '{0}' ... ".format(mail_file),
+    with open(mail_file) as f:
+        email = f.read()
+
+    try:
+        # replace placeholders with actual data
+        email = email.format(FROM=FROM, TO=','.join(TO))
+    except KeyError:
+        print "Warning: missing placeholder in {0}".format(mail_file)
+
+    try:
+        problems = gmail.send_email_string(email, TO)
+    except Exception as e:
+        problems = repr(e)
+
+    if problems:
+        print "Problems: {0!r}".format(problems)
+    else:
+        print 'ok.'