Open your replit account: https://replit.com/~
First, the Flask-Mail extension should be installed with the help of the pip utility.
pip install Flask-Mail
You then need to configure the Flask-Mail by setting the values for the following application parameters.
MAIL_SERVER
MAIL_USE_TLS
MAIL_USE_SSL
MAIL_DEBUG
MAIL_USERNAME
MAIL_PASSWORD
MAIL_DEFAULT_SENDER
MAIL_MAX_EMAILS
MAIL_SUPPRESS_SEND
MAIL_ASCII_ATTACHMENTS
The flask-mail module contains definitions of the following important classes.
The Mail class manages email messaging requirements.The class constructor takes the form of:
flask-mail.Mail(app = None)
Mail class methods include: send(), connect() and send_message().
from flask import Flask
from flask_mail import Mail, Message
app = Flask(__name__)
mail= Mail(app)
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
mail = Mail(app)
@app.route("/")
def index():
msg = Message('Hello', sender = 'yourId@gmail.com', recipients = ['someone1@gmail.com'])
msg.body = "Hello Flask message sent from Flask-Mail"
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Setup your new gmail:
main.py
from flask import Flask, request, render_template
from flask_mail import Mail, Message
app = Flask(__name__)
# configuration of mail
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USERNAME'] = 'YOUREMAIL@gmail.com'
# use the app password created
app.config['MAIL_PASSWORD'] = 'YOURPASSWORD'
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
# instantiating the mail service only after the 'app.config' to avoid error
mail = Mail(app)
@app.route("/", methods=['GET', 'POST'])
def home():
# try and except method
#to avoid the app crashing if the message does not go through due to network or something
try:
# if it is a post request
if request.method == 'POST':
#getting the html inputs and referencing them
sender = request.form['sender']
recipient = request.form['recipient']
message = request.form['message']
subject = request.form['title']
# inputing the message in the correct order
msg = Message(subject,sender=sender,recipients =[recipient] )
msg.body = message
with app.open_resource("static/1.png") as fp:
msg.attach("1.png", "static/1.png", fp.read())
mail.send(msg)
return "message Sent"
return render_template('mail.html')
except Exception as e:
return f'<p>{e} </p>'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
mail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=, initial-scale=1.0">
<title>Document</title>
<!-- bootstrap -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container mt-3">
<h1 class="text-center">Flask Mail Example</h1>
<form method="post" action="" class="mt-5">
<!-- sender's mail address -->
<div class="mb-3">
<label for="sender" class="form-label">Sender's Email address</label>
<input type="email" class="form-control" name="sender" id="sender" required placeholder="name@example.com">
</div>
<!-- recipient mail address -->
<div class="mb-3">
<label for="recipient" class="form-label">Recipient Email address</label>
<input type="text" class="form-control" name="recipient" id="recipient" required placeholder="name@example.com">
</div>
<!-- subject of the mail -->
<div class="mb-3">
<label for="title" class="form-label">Subject of message</label>
<input type="text" class="form-control" name="title" id="title" required placeholder="Electricity bills">
</div>
<!-- body of the mail -->
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Enter message</label>
<textarea class="form-control" required name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<button class="btn btn-outline-success">Submit</button>
</form>
</div>
</body>
</html>
# change these as per use
your_email = "projectinstruments1@gmail.com"
your_password = "zuraobyxeepigpze"