Email Sender
pip install flask
pip install flask-mail
Import:
First import all
required packages. flask is a Python framework for developing web
applications. flask-mail is an extension of the flask framework. It provides an SMTP interface
to flask applications. Usually, we want to import two classes, Mail and Message
from flask-mail.
Flask – It is a
class of flask package.
render_template –
It is a Flask function. It is used to generate output from the jinja2 template file
that is found in the application’s templates folder.
request – It
creates a Request object based on the environment it received from the WSGI
server.
make_response – It
is a function in flask.helpers module of the flask framework. It is used to
adding additional HTTP headers to response within a view’s code.
Mail – It is a
class, manages messaging requirements.
Message – It is
used to encapsulates an email message.
Object creation:
Configuration:
Flask-mail needs to be configured by settings values.
MAIL_PORT - Port number of server used. The port number should be between 0 and 65535.
MAIL_USERNAME - Username of the sender email account.
MAIL_PASSWORD - Password of the sender email account.
MAIL_USE_TLS - Transport Security Layer encryption. (In our mail sender we enable the SSL encryption)
MAIL_USE_SSL - SSL encryption. (In our mail sender we disable the SSL encryption)
index() :
route() - Used for define URL of desired page. The route is called decorator.
mymail() :
In this method, we perform an email sending operation. First, we check whether the HTTP method is POST or others. If the method is POST, then we get all the required data from the user. and send the message to the recipient email.
Message() - Here the Message class contains the subject, sender email id, and recipient email id.
message.body - It contains the message which we want to send to the recipient.
mail.send(message) - send the message to the recipient.
return "<p>Message sent<p>" - return a confirmation message(Message sent) to the mail.html page.
Run the application:
app.run() - this method is used to run the application.
debug = True - This puts Flask in debugging mode.
Post a Comment