This is a simple Go backend for handling contact form submissions and sending the form data via email using SMTP.
- Accepts contact form data via a POST API.
- Validates all required fields.
- Sends the contact form data as an email to a recipient.
- Supports configuration via an
.envfile.
- Go 1.18+ installed on your system.
- An SMTP server for sending emails (e.g., Gmail SMTP).
-
Clone the repository:
git clone https://github.com/your-repo/contact-form-backend.git cd contact-form-backend -
Install dependencies:
go mod tidy
-
Create a
.envfile in the project root:# SMTP Configuration SMTP_HOST=smtp.gmail.com SMTP_PORT=587 SMTP_USER=[email protected] SMTP_PASS=your-email-password # Contact Form Recipient Email CONTACT_FORM_RECIPIENT=[email protected] # Server Configuration PORT=8080 # Allowed Domains for CORS ALLOWED_DOMAINS=https://example.com,https://another-example.com
Replace
[email protected],your-email-password, and[email protected]with your own credentials and recipient email. -
Run the application:
go run main.go
The server will start on the specified port (default is
8080).
Handles contact form submissions and sends the data via email.
- Method:
POST - Content-Type:
application/json - Body:
{ "name": "John Doe", "email": "[email protected]", "number": "1234567890", "message": "Hello, I am interested in your services." }
- 200 OK: Message sent successfully.
- 400 Bad Request: Invalid request body or missing fields.
- 500 Internal Server Error: Failed to send email.
curl -X POST http://localhost:8080/api/contact \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "[email protected]",
"number": "1234567890",
"message": "Hello, I am interested in your services."
}'The .env file is used to configure the application. Below are the available variables:
| Variable | Description | Default |
|---|---|---|
SMTP_HOST |
SMTP server host | Required |
SMTP_PORT |
SMTP server port | Required |
SMTP_USER |
SMTP server username | Required |
SMTP_PASS |
SMTP server password | Required |
CONTACT_FORM_RECIPIENT |
Email to receive contact form submissions | Required |
PORT |
Server port | 8080 |
ALLOWED_DOMAINS |
Comma-separated list of allowed CORS domains | None |
The server logs:
- Startup messages, including the port it's running on.
- Errors encountered when processing requests or sending emails.
Contributions are welcome! Please fork this repository and create a pull request for any improvements or bug fixes.