WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Flask – Mail



A electronic application is regularly required to have an element of sending mail to the clients/customers. Flask-Mail extension makes it extremely simple to set up a simple interface with any email server.

At first, Flask-Mail extension ought to be installed with the help of pip utility.

pip install Flask-Mail 

Then Flask-Mail should be arranged by setting values of the accompanying application parameters.

Sr.No Parameters & Description
1

MAIL_SERVER

Name/IP address of email server

2

MAIL_PORT

Port number of server used

3

MAIL_USE_TLS

Enable/disable Transport Security Layer encryption

4

MAIL_USE_SSL

Enable/disable Secure Sockets Layer encryption

5

MAIL_DEBUG

Debug support. Default is Flask application’s debug status

6

MAIL_USERNAME

User name of sender

7

MAIL_PASSWORD

password of sender

8

MAIL_DEFAULT_SENDER

sets default sender

9

MAIL_MAX_EMAILS

Sets maximum mails to be sent

10

MAIL_SUPPRESS_SEND

Sending suppressed if app.testing set to true

11

MAIL_ASCII_ATTACHMENTS

If set to true, attached filenames converted to ASCII

Mail class

The flask mail module contains meanings of the following significant classes.

flask-mail.Mail(app = None)

The Constructor takes the Flask application object as a parameter.

Methods of Mail class

Sr.No Methods & Description
1

send()

Sends contents of Message class object

2

connect()

Opens connection with mail host

3

send_message()

Sends message object

Message class

It encapsulates an email message. Message class constructor has a few parameters −

flask-mail.Message(subject, recipients, body, html, sender, cc, bcc, 
   reply-to, date, charset, extra_headers, mail_options, rcpt_options)

Message class methods

attach() − adds an attachment to message. This method takes the following parameters −

  • filename − name of file to attach

  • content_type − MIME type of file

  • data − raw file data

  • disposition − content disposition, if any.

add_recipient() − adds another recipient to message

In the following example, SMTP server of Google’s gmail service is used as MAIL_SERVER for Flask-Mail configuration.

Step 1 − Import Mail and Message class from flask-mail module in the code.

from flask_mail import Mail, Message

Step 2 − Then Flask-Mail is configured as per following settings.

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'yourId@gmail.com'
app.config['MAIL_PASSWORD'] = '*****'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

Step 3 − Create an instance of Mail class.

mail = Mail(app)

Step 4 − Set up a Message object in a Python function mapped by URL rule (‘/’).

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

Step 5 − The entire code is given below. Run the following script in Python Shell and visit http://localhost:5000/.