In this posting, we will figure out how to send an email on Flask.
Installation
First of all, we need flask and flask-mail.
$ pip install flask flask-mail
Create Flask Application
Now, it's Flask time.
You can refer to the basics of Flask in the below links.
2020/10/11 - [Python] - [Python] Getting Started with Flask
2020/10/23 - [Python] - [Python] Templates Static Files in Flask
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
app.run()
templates/index.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=device-width, initial-scale=1.0">
<title>Send Email on Flask</title>
</head>
<body>
<div>
<form action="/send-email" method="post">
<div>
<label for="title">Title</label>
<input type="text" name="title" id="title">
</div>
<div>
<label for="content">Content</label>
<textarea type="text" name="content" id="content"></textarea>
</div>
<button type="submit">Send Email</button>
</form>
</div>
</body>
</html>
This is just an example to send an email, so I didn't apply the styles.
Configuration for Flask-Mail
app.py
# ...
from flask_mail import Mail
app = Flask(__name__)
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USERNAME"] = ""
app.config["MAIL_PASSWORD"] = ""
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
email = Mail(app)
#...
I used Gmail because it is the most popular email application.
The setting is pretty simple.
You should enter your username and password of your Gmail account in the string form.
Send an Email on Flask
app.py
from flask import Flask, render_template, request, redirect
from flask_mail import Mail, Message
# ...
@app.route("/send-email", methods=["POST"])
def send_email():
title = request.form["title"]
content = request.form["content"]
msg = Message(
f"{title}",
body=f"{content}",
sender="",
recipients=[""],
)
email.send(msg)
return redirect("/")
# ...
I created the email title and body from the form in the HTML.
You need to enter sender and recipients, and recipients allow the list of recipients.
The one important thing remains.
Google doesn't allow less secure apps, so we have to set it.
> Go to Google's account managing page.
> Goto Security page.
> Check On in less secure app access.
Now, we can send an email using our application.
Conclusion
In this posting, we learned how to send emails on Flask using Flask-Mail.
We used Gmail, but we can use other email services by simply change the configurations.
This is the full source code of app.py.
from flask import Flask, render_template, request, redirect
from flask_mail import Mail, Message
app = Flask(__name__)
app.config["MAIL_SERVER"] = "smtp.gmail.com"
app.config["MAIL_PORT"] = 465
app.config["MAIL_USERNAME"] = ""
app.config["MAIL_PASSWORD"] = ""
app.config["MAIL_USE_TLS"] = False
app.config["MAIL_USE_SSL"] = True
email = Mail(app)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/send-email", methods=["POST"])
def send_email():
title = request.form["title"]
content = request.form["content"]
msg = Message(
f"{title}",
body=f"{content}",
sender="",
recipients=[""],
)
email.send(msg)
return redirect("/")
if __name__ == "__main__":
app.run()
'Python' 카테고리의 다른 글
[Python] Underscore Usage (0) | 2021.04.26 |
---|---|
[Flask] Using Markdown on Flask (0) | 2021.03.01 |
Draw Graph with MatPlotLib in Python (0) | 2021.02.25 |
[Python] Introduce to DearPyGui (1) | 2021.02.11 |
[Python] argparse module (0) | 2021.02.03 |
댓글