Skip to content

Instantly share code, notes, and snippets.

@puentesarrin
Created April 5, 2013 18:14
Show Gist options
  • Save puentesarrin/5321409 to your computer and use it in GitHub Desktop.
Save puentesarrin/5321409 to your computer and use it in GitHub Desktop.
TornadoMail demo using a tornado.template.Loader.
<html>
<head>
<title>TornadoMail Demo</title>
</head>
<body>
<form method="post" action="/">
<input type="text" name="subject"/>
<input type="text" name="email"/>
<input type="submit" value="Send message"/>
</form>
</body>
</html>
<h1>Hello {{ name }}!<h1>
# -*- coding: utf-8 *-*
from tornado import ioloop, template, web
from tornadomail.message import EmailFromTemplate
from tornadomail.backends.smtp import EmailBackend
class Application(web.Application):
@property
def mail_connection(self):
return EmailBackend(
'smtp.gmail.com', 587, '<your google mail>',
'<your google password>', True,
template_loader=template.Loader('.')
)
class MainHandler(web.RequestHandler):
@property
def mail_connection(self):
return self.application.mail_connection
def get(self):
self.render("index.html")
def post(self):
message = EmailFromTemplate(
self.get_argument('subject'),
'mailtemplate.html',
params={'name': 'Jorge'},
from_email='<your google mail>',
to=[self.get_argument('email')],
connection=self.mail_connection
)
message.send()
self.render("index.html")
if __name__ == '__main__':
application = Application([
(r'/', MainHandler),
],
template_path='.'
)
application.listen(8888)
ioloop.IOLoop.instance().start()
@soiitaire
Copy link

Thanks. This helps me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment