Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an email notification for reactivation, basic, but a start

  • Loading branch information...
commit b0d9fb24d9e4392c23b4133ae4ac81432596500c 1 parent c83c4a9
@mitechie authored
Showing with 144 additions and 0 deletions.
  1. +130 −0 bookie/lib/message.py
  2. +10 −0 bookie/views/auth.py
  3. +4 −0 fabfile/sample.ini
View
130 bookie/lib/message.py
@@ -0,0 +1,130 @@
+"""Create and send messages to users
+
+"""
+import logging
+import os
+import smtplib
+
+from email.mime.multipart import MIMEMultipart
+from email.mime.text import MIMEText
+from pyramid.settings import asbool
+
+
+LOG = logging.getLogger(__name__)
+# notification statuses
+# might have pending, sent, failed
+MSG_STATUS = {
+ 'pending': 0,
+ 'sent': 1,
+ 'failed': 2,
+ 'not_sent': 3,
+ 'error': 4,
+}
+
+
+def sendmail(to, from_addr, subject, body):
+ sendmail_location = "/usr/sbin/sendmail"
+ p = os.popen("{0} -t".format(sendmail_location), "w")
+ p.write("From: {0}\n".format(from_addr))
+ p.write("To: {0}\n".format(to))
+ p.write("Subject: {0}\n".format(subject))
+ p.write("\n") # blank line separating headers from body
+ p.write(body)
+ status = p.close()
+ if status != 0:
+ LOG.debug("SENDMAIL FAIL: " + str(status))
+ return False
+ else:
+ return True
+
+
+class Message(object):
+ """This is a base email message we can then tweak"""
+
+ def __init__(self, to, subject, settings):
+ """Start up a basic message"""
+ self.to = to
+ self.subject = subject
+ self.settings = settings
+
+ self.from_addr = settings.get('email.from', None)
+
+ # need ot setup/override in the extending classes
+ self.message_file = None
+
+ def _get_message_body(self, template_file, message_data):
+ """Return the completed message template body
+
+ """
+ return "Test email message from bookie"
+ # lookup = config['pylons.app_globals'].mako_lookup
+ # template = lookup.get_template(template_file)
+
+ # # template vars are a combo of the obj dict and the extra dict
+ # template_vars = {'data': message_data}
+ # return template.render(**template_vars)
+
+ def send(self, message_data=None):
+ """Send the message with the given subject
+
+ body can be sent as part of send() or it can be set to the object as
+ msg.body = "xxx"
+
+ """
+ self.body = self._get_message_body(self.message_file, message_data)
+
+ msg = MIMEMultipart('related')
+ msg['Subject'] = self.subject
+ msg['From'] = self.from_addr
+
+ msg['To'] = self.to
+
+ plain_text = MIMEText(self.body, 'plain', _charset="UTF-8")
+ msg.attach(plain_text)
+
+ is_live = asbool(self.settings.get('email.enable', False))
+
+ if not is_live:
+ print msg.as_string()
+ return MSG_STATUS['sent']
+ else:
+ try:
+ all_emails = msg['To']
+ smtp_server = self.settings.get('email.host')
+
+ if smtp_server == 'sendmail':
+ sendmail(msg['From'], msg['To'], msg['Subject'], self.body)
+ else:
+ mail_server = smtplib.SMTP(smtp_server)
+ mail_server.sendmail(msg['From'],
+ all_emails,
+ msg.as_string())
+ mail_server.quit()
+ return MSG_STATUS['sent']
+
+ except smtplib.SMTPException, exc:
+ LOG.error("SMTP Error sending notice for: {0} ".format(
+ str(self.message_obj)))
+ return MSG_STATUS['error']
+
+
+class ReactivateMsg(Message):
+ """Send an email for a reactivation email"""
+
+ def _get_message_body(self, template_file, message_data):
+ """Return the completed message template body
+
+ """
+ return """
+Reactivate your Bookie account with the following url:
+
+{0}
+
+---
+The Bookie Team""".format(message_data)
+ # lookup = config['pylons.app_globals'].mako_lookup
+ # template = lookup.get_template(template_file)
+
+ # # template vars are a combo of the obj dict and the extra dict
+ # template_vars = {'data': message_data}
+ # return template.render(**template_vars)
View
10 bookie/views/auth.py
@@ -10,6 +10,7 @@
from pyramid.view import view_config
from bookie.lib.applog import AuthLog
+from bookie.lib.message import ReactivateMsg
from bookie.models.auth import UserMgr
from bookie.models.auth import ActivationMgr
@@ -125,6 +126,15 @@ def reactivate(request):
# and then send an email notification
# @todo the email side of things
+ settings = request.registry.settings
+ msg = ReactivateMsg("rharding@mitechie.com",
+ "Activate your Bookie account",
+ settings)
+
+ msg.send(request.route_url('reset',
+ username=user.username,
+ reset_key=user.activation.code))
+
return {
'success': True,
'message': """Your account has been marked for reactivation. Please
View
4 fabfile/sample.ini
@@ -11,6 +11,10 @@ sqlalchemy.url = sqlite:///bookie.db
auth.secret=PLEASECHANGEME
+email.enable=true
+email.from=rharding@mitechie.com
+email.host=sendmail
+
[pipeline:main]
pipeline =
egg:WebError#evalerror
Please sign in to comment.
Something went wrong with that request. Please try again.