106 lines
3.2 KiB
Go
106 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/smtp"
|
|
)
|
|
|
|
func contactFormHandler(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// Parse form data
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "Error parsing form", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
name := r.FormValue("name")
|
|
email := r.FormValue("email")
|
|
message := r.FormValue("message")
|
|
|
|
// Basic validation
|
|
if name == "" || email == "" || message == "" {
|
|
http.Error(w, "All fields are required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// In a real application, you'd send an email or save to a database here
|
|
// For demonstration, print to console
|
|
fmt.Printf("Received contact form submission:\nName: %s\nEmail: %s\nMessage: %s\n", name, email, message)
|
|
|
|
// SMTP server details for Stalwart
|
|
smtpHost := "your_stalwart_hostname.com" // Replace with your Stalwart server hostname
|
|
smtpPort := "587" // Standard submission port with STARTTLS
|
|
smtpAddress := smtpHost + ":" + smtpPort
|
|
|
|
// Authentication credentials
|
|
username := "your_email@your_domain.com" // Replace with a valid Stalwart email account
|
|
password := "your_email_password" // Replace with the password for the email account
|
|
|
|
// Sender and recipient details
|
|
from := username
|
|
to := []string{"recipient@example.com"} // Replace with the recipient's email address
|
|
|
|
// Email message
|
|
msg := []byte("To: recipient@example.com\r\n" +
|
|
"Subject: Test Email from Go and Stalwart\r\n" +
|
|
"\r\n" +
|
|
"This is a test email sent from a Go application through Stalwart.")
|
|
|
|
// Set up authentication
|
|
auth := smtp.PlainAuth("", username, password, smtpHost)
|
|
|
|
// Send the email
|
|
err := smtp.SendMail(smtpAddress, auth, from, to, msg)
|
|
if err != nil {
|
|
log.Fatalf("Error sending email: %v", err)
|
|
}
|
|
|
|
log.Println("Email sent successfully!")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
fmt.Fprintf(w, "Contact form submitted successfully!")
|
|
}
|
|
|
|
func main() {
|
|
http.HandleFunc("/v1/api/contact", contactFormHandler)
|
|
|
|
fmt.Println("Server listening on :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
|
|
// // SMTP server details for Stalwart
|
|
// smtpHost := "mail.npfd.info" // Replace with your Stalwart server hostname
|
|
// smtpPort := "587" // Standard submission port with STARTTLS
|
|
// smtpAddress := smtpHost + ":" + smtpPort
|
|
|
|
// // Authentication credentials
|
|
// username := "postman@npfd.info" // Replace with a valid Stalwart email account
|
|
// password := "TOPSECRET" // Replace with the password for the email account
|
|
|
|
// // Sender and recipient details
|
|
// from := username
|
|
// to := []string{"recipient@example.com"} // Replace with the recipient's email address
|
|
|
|
// // Email message
|
|
// msg := []byte("To: recipient@example.com\r\n" +
|
|
// "Subject: Test Email from Go and Stalwart\r\n" +
|
|
// "\r\n" +
|
|
// "This is a test email sent from a Go application through Stalwart.")
|
|
|
|
// // Set up authentication
|
|
// auth := smtp.PlainAuth("", username, password, smtpHost)
|
|
|
|
// // Send the email
|
|
// err := smtp.SendMail(smtpAddress, auth, from, to, msg)
|
|
// if err != nil {
|
|
// log.Fatalf("Error sending email: %v", err)
|
|
// }
|
|
|
|
// log.Println("Email sent successfully!")
|
|
}
|