Email Sender using Flask-mail

Email Sender

          The email sender is a small project to learn the functionality of Flask-mail. In this, we can send mail to your Gmail account. For this, we want to install Flask and Flask-mail packages.

                                             pip install flask

                                             pip install flask-mail

intro image

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.

import image

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:

            After import, we want to create two objects for Flask and Mail. The Flask constructor takes the name of the current module and the Mail takes the object of the Flask class.

object creation image

Configuration:

            Flask-mail needs to be configured by settings values.

Configuration image

MAIL_SERVER - Name or IP address of email server.

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() :

            The index() method is our initial method. It opens the jinja2 template mail.html on the browser.

index image

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.

mymail image

request.form.get(id) - It return a request object which contain the data.

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:

            Finally, we run the Mail sender.

run image

app.run() - this method is used to run the application.

debug = True - This puts Flask in debugging mode.

Mail sender:

main.py

main image
main image


Template:

mail.html


mail image
mail image


Output:


output image

output image


output image




  



0/Post a Comment/Comments

Previous Post Next Post