summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 14:13:58 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-01-22 14:29:53 -0300
commit38b56cd07fdd5c841f014656b8e67379692f78f0 (patch)
tree72aff9d3f8800ae65c7d75aa3dc910ea3e712f20
parent619056df37c58ec7c1198d3164aac50926274a23 (diff)
Add script to send stored mails.
- Add a problematic email to emails/. - Fix error: local variable result referenced before assignment
-rw-r--r--src/emails/non-ascii-plain-subject.message14
-rw-r--r--src/gmail.py22
-rwxr-xr-xsrc/send-mail-strings.py58
3 files changed, 93 insertions, 1 deletions
diff --git a/src/emails/non-ascii-plain-subject.message b/src/emails/non-ascii-plain-subject.message
new file mode 100644
index 0000000..25c44f4
--- /dev/null
+++ b/src/emails/non-ascii-plain-subject.message
@@ -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==--
diff --git a/src/gmail.py b/src/gmail.py
index 34579b6..4e7af04 100644
--- a/src/gmail.py
+++ b/src/gmail.py
@@ -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
index 0000000..b682c90
--- /dev/null
+++ b/src/send-mail-strings.py
@@ -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.'