SMTP relay for sending emails via the Microsoft Graph API
Microsoft has, for years, been trying to push everyone away from using SMTP with baisc authentication (username + password), which I completely support. However, not a lot of the open source projects I use have caught on to adding OAuth2 based authentication for sending out email. Heck, even many enterprise level products still require using basic authentication. There are some official work arounds, but all of them involve identity authentication methods that either don't work in many cases (where an IP addess may change frequently) or require disabling certain security policies that can be dangerous.
This service is ideal for developers and organizations looking to integrate email functionalities within their applications hosted on cloud platforms, microservices architectures, or any situation where traditional SMTP access poses challenges. With easy setup, comprehensive documentation, and Docker support, it streamlines the process of sending emails through Microsoft's secure and reliable Graph API, thereby enhancing application communication strategies with minimal overhead.
- Docker
- Python 3.8 or higher
- A Microsoft Azure account with access to Microsoft Graph API
- Clone this repository to your local machine.
- Navigate to the cloned directory.
- Copy
.env.sample
to.env
and fill in the necessary environment variables. - Build the Docker container:
docker build -t microsoft-graph-smtp-relay
. - Run the container:
docker run -d --name smtp-relay microsoft-graph-smtp-relay
The .env.sample
file contains all necessary used configuration keys with partial examples.
- Create a new Registed Application.
- The Application (client) ID should be the CLIENT_ID environmental variable.
- The Directory (tenant) ID should be appended to
https://login.microsoftonline.com/
to be the AUTHORITY environmental variable.
- Go to the Certificates & secrets page and create a new secret.
- The Value for that secret should be the CLIENT_SECRET environmental variable.
- Go to the API permissions page.
- Remove all existing permissions if this registered app will only be used for the SMTP relay.
- Click Add a permission.
- Click Microsoft Graph.
- Click Application permissions.
- Search for mail.
- Click the checkbox next to Mail.Send. It should be the one that says Send mail as any user below it.
- Click Add permission
This service can be deployed in container orchestration environments using Docker Compose or Kubernetes. Examples provided in the examples
directory demonstrate the configuration needed for each method.
The docker-compose.yaml
file in the examples
directory provides a template for deploying the SMTP Relay Service as a container. To use it:
- Ensure Docker Compose is installed on your system.
- Navigate to the
examples
directory. - Review and adjust the
docker-compose.yaml
file as necessary, ensuring environment variables are set according to your specific requirements. - Run the following command to start the service:
docker-compose up
This will pull the necessary image and start the SMTP Relay Service as defined in the Docker Compose configuration.
For deployment in a Kubernetes cluster, the k8s.yaml
file offers a detailed manifest for setting up the service. To deploy:
- Ensure you have kubectl installed and configured to interact with your Kubernetes cluster.
- Navigate to the
examples
directory. - Review the
k8s.yaml
file, adjusting the deployment specifications and environment variables to fit your environment. - Apply the configuration to your cluster with:
kubectl apply -f k8s.yaml
This will create the necessary Kubernetes resources (e.g., Deployment, Service) to run the SMTP Relay Service within your cluster.
Both methods provide a scalable and manageable way to incorporate email functionality into your applications, leveraging the power of Microsoft Graph API through the SMTP Relay Service.
The .devcontainer
directory contains VS Code and Docker configuration files for setting up a development environment inside a container.
The app
directory contains the source code of the service.
Use the provided dockerfile
for development and testing.
The test
directory includes a basic client script that can be used to test the relay service.
We welcome contributions to this project! Please refer to the LICENSE
for more details on contributing and the terms of use.
This project is licensed under the terms of the MIT License. See the LICENSE
file for more details.
For support and feature requests, please open an issue in the repository's issue tracker.