Small project to send email via SendGrid using an Azure Function 4.0 running in a Docker container.
This project aims to create a small Web Service that consists in an Azure Function 4.0 with ah HTTP Trigger running in a Docker cointainer that sends email via the SendGrid C# SDK.
This project is an "academic excercise" that I used to try out the recently released Azure Functions 4.0 (which use .NET 6)
You cand read more about the capabilities of Azure Functions Here. They'are very useful in a microservice environment, they are usually small pieces of software (yup, that's what a microservice is... Kind Of).
The Azure Function i'm using uses and HTTP trigger, which means that the Function wakes up when it receives an HTTP request, runs some tasks then goes back to sleep, pretty easy.
Yup, Docker for sending a simple email, you read that right.
SendGrid is a cloud-based SMTP service provider which let you use their email server to send your emails, you get 100 emails a day on the free tier, more than enough for playing around. The paid tiers are very also honest in terms of pricing.
Why the heck would I use SendGrid? Well in a cloud environment you usually cannot send email via STMP in a simple way, also who wants to manage an email server?
SendGrid offers an C# SDK to send emails with a few lines of code, I used more lines of code to setup the environment than to send the actual email.
Create and .env
file (used to put you sendgrid API key and so on) like the following:
SendGridKey=<API-GOES-HERE>
SendGridSender=<SENDER-GOES-HERE>
SendGridSenderName=<SENDER-NAME-GOES-HERE>
Receiver=<RECEIVER-GOES-HERE>
Where do I find those variables? In your SendGrid dashboard after creating an account.
Then run the Docker image passing the .env
file like so:
docker run -dp 5000:80 --env-file .env ghcr.io/tiaringhio/emailsender:latest
This will run the server on port 5000, change that if you want/need.
you can the send HTTP Post requests followinf the EmailData class.
Example in JSON:
{
"sender": "Mattia Ricci",
"subject": "Testing email",
"message": "This mail was sent in a very complicated (and uneccessary) way"
}
Have fun!
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Shamefully copied from montali
Distributed under the GPL License. See LICENSE
for more information.
Icons made by Smashicons from www.flaticon.com