DEV Community

Cover image for Sending Emails Using Python And Flask And Gmail
Ethan
Ethan

Posted on Edited on Originally published at ethan-dev.com

Sending Emails Using Python And Flask And Gmail

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
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

Installing The Dependencies

Next we need to install the dependencies, create a file called "requirements.txt" and add the following:

flask
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

To install the dependencies run the following command:

pip install -r requirements.txt
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

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
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

Next we need to initialize the flask app, which can be done with the following line:

app = Flask(__name__)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
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

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)
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

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)
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

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!'
⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung ⭐ Football Update: ethand/sending emails using python and flask and gmail ba - Jadwal Siaran Langsung

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.

“Buy Me A Coffee”

If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the following course

Top comments (0)