Introduction
Hello! ๐
Recently I was asked on how to send emails using flask and Python, so I'd thought I'd also share how I did it and hopefully it helps you. ๐
Enabling The Virtual Environment
First we need to setup the virtual environment, this can be done via the following commands:
python3 -m venv env
source env/bin/activate
Installing The Dependencies
Next we need to install the dependencies, create a file called "requirements.txt" and add the following:
flask
To install the dependencies run the following command:
pip install -r requirements.txt
Next we can actually start writing the code. ๐
Coding The Server
Now we can actually move on coding the server. Open up a file called main.py and add the following imports:
from flask import Flask, request
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
Next we need to initialize the flask app, which can be done with the following line:
app = Flask(__name__)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
Once the app is initialized we need to define a route that takes three parameters, an address to send to, the subject of the mail and the main body of the mail.
Which can be done via the following:
@app.route("/send", methods=["POST"])
def send():
email_address = request.form['address']
email_subject = request.form['subject']
email_message = request.form['message']
sender_email = 'youremail'
sender_password = 'your password'
receiver_email = email_address
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = email_subject
message.attach(MIMEText(email_message, 'plain'))
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, receiver_email, message.as_string())
server.quit()
return 'Email Sent!'
except Exception as e:
return str(e)
The function uses the three parameters and sends the mail using a gmail account, make sure to replace the variables with your own. ๐
Finally we just need to initialize the main function:
if __name__ == "__main__":
app.run(debug=True)
Done! ๐
The server can be tested with the following curl command:
curl --location --request POST 'localhost:5000/send' \
--form 'address=example@gmail.com' \
--form 'subject=Test email' \
--form 'message=Hello, this is a test email sent using curl and Flask!'
After you've run the above command (don't forget to change the address), you should see the mail in your inbox. ๐
Conclusion
Here I have shown you how to send emails using flask, python and gmail.
I was actually surprised you don't need to code that much. ๐
Hopefully this has been of some use to you. ๐
As always you can find the source code for this example via Github:
https://github.com/ethand91/flask-email
Happy Coding! ๐
Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.
If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the following course
Top comments (0)