4.4 Flask-mail
In the development process, many applications need to notify the user via mail, Flask extension Package flask-mail by wrapping Python built-in Smtplib package, can be used in the Flask program to send mail.
The Flask-mail connects to the Simple Mail protocol (easy Mail Transfer protocol,smtp) server and sends the message to the server.
The following example sends a message by turning on the QQ mailbox SMTP service settings.
fromFlaskImportFlask fromFlask_mailImportMail, Messageapp= Flask (__name__)#Configure mail: Server/port/Transport Layer Security Protocol/mailbox name/passwordapp.config.update (DEBUG=True, Mail_server='smtp.qq.com', Mail_prot=465, Mail_use_tls=True, Mail_username='[email protected]', Mail_password='Goyubxohbtzfbidd',) Mail=Mail (APP) @app. Route ('/')defindex ():#Sender sender, recipients receiver listmsg = Message ("This is a test", sender='[email protected]', recipients=['[email protected]','[email protected]']) #Message ContentMsg.body ="Flask Test Mail" #Send mailmail.send (msg)Print "Mail sent" return "Sent succeed"if __name__=="__main__": App.run ()
Flask Message Extensions