Skip to content

Yebei-Gideon/Go-Email-Sender

Repository files navigation

Go Email Sender

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.

Features

  • Send email using SMTP.
  • Handle contact form submissions.
  • Log details for each email sent (with logging enabled).
  • CORS support for integration with frontend apps.

Requirements

  • 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)

Setup

1. Clone the repository

git clone https://github.com/Gideon-Yebei/go-email-sender.git
cd go-email-sender

2. Set the necessary environment variables in .env

Create 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 true to enable detailed debug logging (must be set as either true or false).
  • PORT: The port for the server to run on (must be set explicitly).

Example .env file

see .env.local

3. Install dependencies

go mod tidy

4. Run the application

To run the application locally, use the following command:

go run cmd/main.go

Usage Example

API Endpoint: /send-email

Send a POST request to /send-email with the contact form data in JSON format.

Request

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."
}'

Response

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.

Logs

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.com

Docker

You can also run this application inside a Docker container.

Build the Docker Image and Start the Services with Docker Compose

If you're using Docker Compose, you can start the service with the following command:

docker-compose up --build

This will build the image and start the container.

Check the Logs

To view the logs from the Docker container, use:

docker-compose logs -f

This will stream the logs from the container to your terminal.

Without Docker

If you prefer not to use Docker, you can build and run the service directly on your local machine.

1. Build the application

go build -o go-email-sender ./cmd

2. Run the application

./go-email-sender

API Reference

POST /send-email

Request 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).

About

My contacts page email sender in golang

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors