A simple and robust Flask API to handle newsletter subscriptions with MailerLite and contact form submissions via SMTP.
- Newsletter Subscription:
/subscribeendpoint to add users to a MailerLite mailing list. - Contact Form:
/contactendpoint to send contact form data via SMTP. - Validation: Uses Pydantic for request data validation.
- Configuration: Manages settings with
python-decouplefor easy environment-based configuration. - CORS Support: Enabled globally for development.
- Containerization: Docker support for easy deployment.
CORS is currently enabled for all origins (*) to simplify local development.
from flask_cors import CORS
CORS(app, origins=["https://yourfrontenddomain.com"])This helps prevent unauthorized cross-origin requests and improves your API’s security.
Prerequisites Python 3.12+
Docker (optional, for containerized deployment)
git clone https://github.com/yorchwebs/mua-api.git
cd mua-apipython -m venv .venv
source .venv/bin/activatepip install -r requirements-dev.txtCreate a .env file in the root of the project and add the following environment variables:
# MailerLite Configuration
MAILERLITE_API_KEY=your_mailerlite_api_key
MAILERLITE_GROUP_ID=your_mailerlite_group_id
# SMTP Configuration
SMTP_HOST=your_smtp_host
SMTP_PORT=your_smtp_port
SMTP_USER=your_smtp_user
SMTP_PASSWORD=your_smtp_password
SMTP_FROM=your_smtp_from_address
SMTP_TO=your_smtp_to_addressYou can configure the API to send emails using Gmail SMTP or a custom domain.
-
Go to your Google Account → Security section.
-
Enable 2-Step Verification (required to use App Passwords).
-
Generate an App Password for “Mail”.
-
Use the following configuration in your .env file:
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your_email@gmail.com
SMTP_PASSWORD=your_generated_app_password
SMTP_FROM=your_email@gmail.com
SMTP_TO=destination_email@example.com-
Locate your domain’s email service settings (e.g., Zoho, Outlook, Namecheap, Hostinger, etc.).
-
Obtain the SMTP details (host, port, username, and password).
-
Add SPF and DKIM records to your DNS for better deliverability.
-
Update your .env file with your domain’s details, for example:
SMTP_HOST=mail.yourdomain.com
SMTP_PORT=587
SMTP_USER=contact@yourdomain.com
SMTP_PASSWORD=your_password
SMTP_FROM=contact@yourdomain.com
SMTP_TO=support@yourdomain.com- Running the Application
- Local Development
- Run the application in development mode:
python run.pyThe application will be available at:
http://127.0.0.1:5000Build the Docker image
# Copiar código
docker build -t mua-api .Run the Docker container
# Copiar código
docker run -p 8080:8080 --env-file .env mua-apiThe application will be available at:
# Copiar código
http://0.0.0.0:8080/subscribe
Method: POST
Description: Subscribes a user to the MailerLite mailing list.
Request Body:
{
"email": "user@example.com"
}Responses:
200 OK: Subscription successful.
422 Unprocessable Entity: Invalid input data.
400 Bad Request: Error from MailerLite API.
/contact
Method: POST
Description: Sends a contact form submission via SMTP.
Request Body:
{
"name": "John Doe",
"email": "john.doe@example.com",
"phone": "123-456-7890",
"message": "This is a test message."
}Responses:
200 OK: Email sent successfully.
400 Bad Request: Missing required fields.
500 Internal Server Error: Error sending the email.
Interactive API documentation is available via Postman:
This documentation includes:
-
Example requests and responses.
-
The ability to test endpoints directly.
-
A “Run in Postman” button for easy import.
To run the test suite, use the following command:
# Copiar código
pytestProduction:
-
Flask
-
Flask-Cors
-
gunicorn
-
pydantic
-
python-decouple
-
requests
Development
- pytest
and all production dependencies.
See requirements-prod.txt and requirements-dev.txt for details.