Skip to content

Instantly share code, notes, and snippets.

@iambibhas
Last active December 10, 2015 20:58
Show Gist options
  • Save iambibhas/4491535 to your computer and use it in GitHub Desktop.
Save iambibhas/4491535 to your computer and use it in GitHub Desktop.
simple threaded mail handler
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "../apps/home/mailhandler.py", line 46, in handle
if self.send(to=[user.email], subject=subjects[mtype], text_body=plaintext, html_body=htmly):
File "../apps/home/mailhandler.py", line 55, in send
msg = EmailMultiAlternatives(subject, text_body, from_email, to)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 438, in __init__
assert group is None, "group argument must be None for now"
AssertionError: group argument must be None for now
class MailHandler:
def getTemplates(self, location):
try:
plaintext = get_template(location + '.txt')
htmly = get_template(location + '.html')
except Exception as e:
print 'Error in MainHandler', e
return False
return [plaintext, htmly]
"""
Required Parameter:
@body directory has fields type(Welcome, passwd reset), to(User Object)
"""
def handle(self, mtype, user):
# Call getTemplate and then Send
if not mtype or not user:
return False
subjects = {
'welcome': 'Welcome to test',
'verify': 'Verify your email address',
'passreset': 'Password reset'
}
(plaintext, htmly) = self.getTemplates('mails/' + mtype)
contxt = Context({ 'username': user.username })
if self.send(to=[user.email], subject=subjects[mtype], text_body=plaintext, html_body=htmly):
return True
else:
return False
def thread_run(self, msg):
msg.send()
def send(self, to=[], from_email='[email protected]', subject='', text_body='', html_body=''):
msg = EmailMultiAlternatives(subject, text_body, from_email, to)
msg.attach_alternative(html_body, "text/html")
thread = threading.Thread(target=self.thread_run, args=(msg))
response = thread.start()
print 'Thread started'
print response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment