Activity 48: Documentation of Python SMTP

STEP 1: Create a folder

STEP 2: Open the folder and launch it on “cmd” or terminal

STEP 3: Create a python virtual environment

use this command below to create a python virtual environment:

python -m venv myvenv

The command python -m venv myvenv creates a new virtual environment in a folder named "venv" inside your project directory. This environment will have its own Python interpreter and libraries.

Verify the myvenv folder in the project folder which is named “act-48

STEP 4: Activate the python virtual environment

  • use the command below to activate python virtual environment
myvenv\Scripts\activate

Once activated, the terminal prompt changes to show the name of the environment (in this case, (myvenv)), indicating that you are now working within the virtual environment.

STEP 5: Install Flask

pip install Flask

STEP 6: Add venv on .gitignore

Adding the myvenv directory to .gitignore is essential to prevent the virtual environment files from being tracked by version control tools like Git. Since the venv directory contains environment-specific files that are not necessary for the application’s source code, it is a good practice to exclude it from version control to keep the repository clean and reduce clutter.

Steps to add myvenv to .gitignore, use the command below:

nul > .gitignore

Open the .gitignore file with a text editor or notepad.

  1. Add the following line below:
myvenv/

save the file:

STEP 7: Open the project in pycharm

STEP 8: Create new directory or folder and name it “templates

STEP 9: Inside the templates directory/folder, create a index.html

STEP 10: Create a python file named “app.py"

STEP 11: In the “app.py", import Flask, render_template, smtplib and MIMEText

from email.mime.multipart import MIMEMultipart

from flask import Flask, request, jsonify
import smtplib
from email.mime.text import MIMEText

app = Flask(__name__)

SMTP_SERVER = 'smtp.gmail.com'  # Enter your SMTP server
SMTP_PORT = 587  # Common port for SMTP
SMTP_USERNAME = 'franciscojohannes50@gmail.com'  # Your email
SMTP_PASSWORD = 'your_password'  # Your Gmail app password or regular password (consider using app-specific passwords)

@app.route('/send-email', methods=['POST'])
def send_email():
    data = request.json
    if 'message' not in data or 'email' not in data:
        return jsonify({"error": "missing email or message"}), 400

    message = data['message']
    email_recipient = data['email']

    try:
        # Create the email
        msg = MIMEMultipart()
        msg['From'] = SMTP_USERNAME
        msg['To'] = email_recipient
        msg['Subject'] = 'this email is from Flask'

        # Add message body
        msg.attach(MIMEText(message, 'plain'))

        # Send the email
        with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
            server.starttls()
            server.login(SMTP_USERNAME, SMTP_PASSWORD)
            server.sendmail(SMTP_USERNAME, email_recipient, msg.as_string())

        return jsonify({'message': 'Email sent successfully'}), 200

    except Exception as e:
        return jsonify({'error': 'Failed to send email'}), 500


if __name__ == '__main__':
    app.run(debug=True)

STEP 12: In the “index.html” file, use the passed variable:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello Page</title>
</head>
<body>
    <h1>{{ message }}</h1>
</body>
</html>

STEP 13: Create GitHub Repository

go to “your repositories

click new:

create a repository name:

below, click “create repository“:

STEP 14: Go to terminal and push the created repository

follow this steps:

STEP 15: Go back to pycharm and run “app.py

use this command to run:

the name of the file is “app.py

python name_of_python_file

so we’ll type the command like this:

python app.py

after running the app.py copy the url or link given:

copy this link: http://127.0.0.1:5000

create a new collection:

name the collection:

add a request, the default request would be GET, but let’s change it to POST since we’re going to post a message

paste the given link:

to access or post a message, we’re going to use the name from the app.route

then paste it beside the link in postman:

after that, go to “raw” section and type the message with your email and click send:

  • make sure that the email is the as the email coded in “SMTP_USERNAME

after the message or email sent succesfully, go to the email messages and you will receive the message;

GitHub repository link: https://github.com/FranciscoJohannes/python-smtp