Flask Snippets

Flask-Mail with google apps

By kfk filed in Utilities

A simple example on how to use flask-mail with google or google apps email accounts. Other sources:

-Flask-mail docs: http://packages.python.org/flask-mail/

-Google support: http://support.google.com/mail/bin/answer.py?hl=en&answer=78799

@kfk

from flask import Flask
from flaskext.mail import Mail, Message

app =Flask(__name__)
mail=Mail(app)

app.config.update(
	DEBUG=True,
	#EMAIL SETTINGS
	MAIL_SERVER='smtp.gmail.com',
	MAIL_PORT=465,
	MAIL_USE_SSL=True,
	MAIL_USERNAME = 'you@google.com',
	MAIL_PASSWORD = 'GooglePasswordHere'
	)

mail=Mail(app)

@app.route("/")
def index():
	msg = Message(
              'Hello',
	       sender='you@dgoogle.com',
	       recipients=
               ['recipient@recipient_domain.com'])
	msg.body = "This is the email body"
	mail.send(msg)
	return "Sent"

if __name__ == "__main__":
    app.run()

This snippet by kfk can be used freely for anything you like. Consider it public domain.

Comments