This documentation provides detailed instructions on how to set up and use the Email Sending API Endpoint in your Django project. The API endpoint allows you to send emails using the Mailgun API service.
Before setting up the Email Sending API Endpoint, ensure you have the following prerequisites:
- Python (3.8 or higher)
- Django (3.2 or higher)
- Docker (for deployment)
- Mailgun account with API key and server name
-
Clone the repository containing your Django project.
git clone https://github.com/YounoussaBen/EmailAPI.git
-
Navigate to the project directory.
cd EmailAPI
-
Install the project dependencies using pip.
pip install -r requirements.txt
-
Create a copy of the
.env.example
file and name it.env
.cp .env.example .env
-
Fill in the required environment variables in the
.env
file. Replace placeholders with appropriate values for your environment.
For local development, ensure the following settings are configured:
DEV_SECRET_KEY
: Secret key for Django.DEV_ALLOWED_HOSTS
: Comma-separated list of allowed hosts for development.
For production deployment, configure the following settings:
PROD_SECRET_KEY
: Secret key for Django.PROD_ALLOWED_HOSTS
: Domain name(s) for your production environment.
Depending on your environment, configure the following database settings:
DB_ENGINE
: Set todjango.db.backends.sqlite3
.- No additional database configuration needed.
DB_ENGINE
: Set topostgresql
.DB_NAME
: Name of the PostgreSQL database.DB_USER
: Username for the PostgreSQL database.DB_PASSWORD
: Password for the PostgreSQL database.DB_HOST
: Hostname for the PostgreSQL database server.DB_PORT
: Port number for the PostgreSQL database server.
To run the Django project locally, use the following command:
python manage.py runserver
The development server will start, and you can access the Email Sending API Endpoint at http://localhost:8000/send-email/
.
To deploy the Django project in a production environment using Docker, follow these steps:
-
Build the Docker image:
docker-compose build
-
Run the Docker containers:
docker-compose up -d
The Django application will be accessible on port 7000. You can access the Email Sending API Endpoint at http://localhost:7000/send-email/
.
To use the Email Sending API Endpoint, send a POST request to /send-email/
with the following parameters:
from
: Sender's email address.to
: List of recipient email addresses.subject
: Email subject.text
: Email body text.attachments
: List of attachments (optional).
{
"from": "[email protected]",
"to": ["[email protected]", "[email protected]"],
"subject": "Test Email",
"text": "This is a test email sent using the Email Sending API Endpoint.",
"attachments": [
{
"filename": "example.pdf",
"content": "<base64_encoded_content>"
}
]
}
Replace <base64_encoded_content>
with the Base64-encoded content of the attachment file.
{
"message": "Email sent successfully"
}
If an error occurs during email sending, an appropriate error message will be returned along with the corresponding status code.
This concludes the documentation for the Email Sending API Endpoint. Follow the provided instructions to set up, configure, and use the API endpoint in your Django project. If you encounter any issues or have questions, please refer to the project's documentation or reach out to the project maintainers for assistance.