This is a simple Go-based service for sending emails using SMTP. It can be used to send contact form messages to a predefined email address. The service includes logging for debugging and tracking email sending activity.
- Send email using SMTP.
- Handle contact form submissions.
- Log details for each email sent (with logging enabled).
- CORS support for integration with frontend apps.
- Go 1.18+
- SMTP credentials (for example, from Mailtrap or any SMTP provider)
- Frontend application URL (for CORS configuration)
- Docker (optional, if you want to run the service in a container)
git clone https://github.com/Gideon-Yebei/go-email-sender.git
cd go-email-senderCreate a .env file at the root of the project with the following contents (replace with your actual SMTP credentials and frontend URL):
The application reads configuration from environment variables (either set in .env or passed directly in the environment). The following environment variables are required and must be set:
- SMTP_HOST: The SMTP server host (e.g.,
smtp.mailtrap.io). - SMTP_PORT: The SMTP server port (must be set explicitly).
- SMTP_USER: Your SMTP username.
- SMTP_PASSWORD: Your SMTP password.
- RECIPIENT_EMAIL: The email address where messages will be sent.
- FRONTEND_URL: The frontend URL for CORS configuration.
- DEBUG: Set to
trueto enable detailed debug logging (must be set as eithertrueorfalse). - PORT: The port for the server to run on (must be set explicitly).
see .env.local
go mod tidyTo run the application locally, use the following command:
go run cmd/main.goSend a POST request to /send-email with the contact form data in JSON format.
curl -X POST http://localhost:[PORT]/send-email \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john.doe@example.com",
"subject": "Test Subject",
"message": "This is a test message."
}'If the email is sent successfully, the response will be:
{
"message": "Message sent successfully!"
}If there is an error (e.g., invalid input, failure to send email), the response will contain an error message.
If logging is enabled (DEBUG=true in .env), the application will log details of the request and the email sent, for example:
2024/11/09 12:34:56 Sending email from: john.doe@example.com
To: recipient@example.com
Subject: Test Subject
Message: This is a test message.
2024/11/09 12:34:57 Email sent successfully to recipient@example.comYou can also run this application inside a Docker container.
If you're using Docker Compose, you can start the service with the following command:
docker-compose up --buildThis will build the image and start the container.
To view the logs from the Docker container, use:
docker-compose logs -fThis will stream the logs from the container to your terminal.
If you prefer not to use Docker, you can build and run the service directly on your local machine.
go build -o go-email-sender ./cmd./go-email-senderRequest Body (JSON):
{
"name": "Your Name",
"email": "your.email@example.com",
"subject": "Subject of the message",
"message": "Your message here"
}Response (JSON):
{
"message": "Message sent successfully!"
}Error Responses:
- 400 Bad Request: Invalid input or missing required fields.
- 500 Internal Server Error: Failure to send email (e.g., SMTP error).