A simple Django project that sets up an SMTP server to handle contact form submissions. The project includes an endpoint to submit contact form data which will be sent via email using SMTP.
git clone https://github.com/dharshan-kumarj/SMTP_Django.git
cd SMTP_Django
Create a .env
file in the root directory of your project and add the following environment variables. Replace the placeholder values with your actual credentials.
SECRET_KEY=your_secret_key_here
DEBUG=True
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
[email protected]
EMAIL_HOST_PASSWORD=your_app_password_here
Note: If using Gmail, you must enable 2-factor authentication in your Gmail account and generate an app-specific password to use as EMAIL_HOST_PASSWORD
. Follow this guide to generate an app password.
The project is containerized with Docker for easy setup and deployment.
docker-compose up
This will:
- Build the Docker image if it doesn't exist
- Start the Django application with Gunicorn
- Apply any pending migrations
- Make the service available at http://localhost:8000
You should see output similar to:
Attaching to web-1
web-1 | Operations to perform:
web-1 | Apply all migrations: admin, auth, contenttypes, sessions
web-1 | Running migrations:
web-1 | No migrations to apply.
web-1 | [2025-02-14 02:16:49 +0000] [8] [INFO] Starting gunicorn 21.2.0
web-1 | [2025-02-14 02:16:49 +0000] [8] [INFO] Listening at: http://0.0.0.0:8000 (8)
web-1 | [2025-02-14 02:16:49 +0000] [8] [INFO] Using worker: sync
web-1 | [2025-02-14 02:16:49 +0000] [9] [INFO] Booting worker with pid: 9
If you prefer to run without Docker:
- Create a Virtual Environment and Install Dependencies
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
- Run Migrations
python manage.py migrate
- Run the Server
python manage.py runserver
- URL:
http://localhost:8000/api/contact/submit/
- Method:
POST
- Headers:
Content-Type: application/json
- Body:
{
"name": "Test User",
"email": "[email protected]",
"message": "This is a test message!"
}
You can use curl
to submit a contact form:
curl -X POST http://localhost:8000/api/contact/submit/ \
-H "Content-Type: application/json" \
-d '{
"name": "Test User",
"email": "[email protected]",
"message": "This is a test message!"
}'
{
"status": "success",
"message": "Email sent successfully"
}
SMTP_Django/
├── manage.py
├── Dockerfile
├── docker-compose.yml
├── smtp_django/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ ├── asgi.py
├── contact/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── serializers.py
│ ├── views.py
│ ├── urls.py
├── .env
├── requirements.txt
The project includes Docker configuration files:
Dockerfile
: Defines the container image with Python and required dependenciesdocker-compose.yml
: Orchestrates the service deployment with the right environment variables and port mappings
The requirements.txt
file should include:
Django>=4.2.0
djangorestframework>=3.14.0
python-dotenv>=1.0.0
gunicorn>=21.2.0
This project uses Django's built-in email functionality with SMTP. The following settings are configured in settings.py
:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.environ.get('EMAIL_HOST')
EMAIL_PORT = os.environ.get('EMAIL_PORT')
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
For production deployment:
- Set
DEBUG=False
in your.env
file - Configure a proper database (PostgreSQL recommended)
- Set up HTTPS using a reverse proxy like Nginx
- Use Docker Swarm or Kubernetes for orchestration
- Configure proper email error logging
- Email not sending: Check that your SMTP credentials are correct and that your email provider allows SMTP access
- Permission denied: Ensure your app password is correct and that less secure apps are allowed (if applicable)
- Connection errors: Verify that your firewall allows outgoing connections on the SMTP port
- Docker issues: Make sure Docker and Docker Compose are installed correctly and running
- Port conflicts: Ensure that port 8000 is not being used by another service on your host
Contributions are welcome! Please feel free to submit a Pull Request.