jlelse
/
MailyGo
Archived
1
Fork 0
This repository has been archived on 2024-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
MailyGo/mail.go

90 lines
2.5 KiB
Go
Raw Permalink Normal View History

2020-03-14 21:26:34 +00:00
package main
import (
"bytes"
"fmt"
"net/smtp"
2020-03-15 16:15:08 +00:00
"sort"
2020-03-14 21:26:34 +00:00
"strconv"
"strings"
"time"
)
2020-04-12 10:34:51 +00:00
func sendForm(values *FormValues) {
2020-03-14 21:26:34 +00:00
recipient := findRecipient(values)
sendMail(recipient, buildMessage(recipient, time.Now(), values))
}
2020-04-12 10:34:51 +00:00
func buildMessage(recipient string, date time.Time, values *FormValues) string {
2020-04-26 12:50:54 +00:00
msgBuffer := &bytes.Buffer{}
_, _ = fmt.Fprintf(msgBuffer, "From: Forms <%s>", appConfig.Sender)
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "To: %s", recipient)
_, _ = fmt.Fprintln(msgBuffer)
2020-03-14 21:26:34 +00:00
if replyTo := findReplyTo(values); replyTo != "" {
2020-04-26 12:50:54 +00:00
_, _ = fmt.Fprintf(msgBuffer, "Reply-To: %s", replyTo)
_, _ = fmt.Fprintln(msgBuffer)
2020-03-14 21:26:34 +00:00
}
2020-04-26 12:50:54 +00:00
_, _ = fmt.Fprintf(msgBuffer, "Date: %s", date.Format(time.RFC1123Z))
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintf(msgBuffer, "Subject: New submission on %s", findFormName(values))
_, _ = fmt.Fprintln(msgBuffer)
_, _ = fmt.Fprintln(msgBuffer)
2020-03-15 16:15:08 +00:00
bodyValues := removeMetaValues(values)
var keys []string
2020-04-12 10:34:51 +00:00
for key := range *bodyValues {
2020-03-15 16:15:08 +00:00
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
2020-04-26 12:50:54 +00:00
_, _ = fmt.Fprint(msgBuffer, key)
_, _ = fmt.Fprint(msgBuffer, ": ")
_, _ = fmt.Fprintln(msgBuffer, strings.Join((*bodyValues)[key], ", "))
2020-03-14 21:26:34 +00:00
}
return msgBuffer.String()
}
func sendMail(to, message string) {
2020-10-22 18:18:11 +00:00
auth := smtp.PlainAuth("", appConfig.SMTPUser, appConfig.SMTPPassword, appConfig.SMTPHost)
err := smtp.SendMail(appConfig.SMTPHost+":"+strconv.Itoa(appConfig.SMTPPort), auth, appConfig.Sender, []string{to}, []byte(message))
2020-03-14 21:26:34 +00:00
if err != nil {
fmt.Println("Failed to send mail:", err.Error())
}
}
2020-04-12 10:34:51 +00:00
func findRecipient(values *FormValues) string {
if len((*values)["_to"]) == 1 && (*values)["_to"][0] != "" {
formDefinedRecipient := (*values)["_to"][0]
2020-03-14 21:26:34 +00:00
for _, allowed := range appConfig.AllowedRecipients {
if formDefinedRecipient == allowed {
return formDefinedRecipient
}
}
}
return appConfig.DefaultRecipient
}
2020-04-12 10:34:51 +00:00
func findFormName(values *FormValues) string {
if len((*values)["_formName"]) == 1 && (*values)["_formName"][0] != "" {
return (*values)["_formName"][0]
2020-03-14 21:26:34 +00:00
}
return "a form"
}
2020-04-12 10:34:51 +00:00
func findReplyTo(values *FormValues) string {
if len((*values)["_replyTo"]) == 1 && (*values)["_replyTo"][0] != "" {
return (*values)["_replyTo"][0]
2020-03-14 21:26:34 +00:00
}
return ""
}
2020-04-12 10:34:51 +00:00
func removeMetaValues(values *FormValues) *FormValues {
2020-03-14 21:26:34 +00:00
cleanedValues := FormValues{}
2020-04-12 10:34:51 +00:00
for key, value := range *values {
2020-03-14 21:26:34 +00:00
if !strings.HasPrefix(key, "_") {
cleanedValues[key] = value
}
}
2020-04-12 10:34:51 +00:00
return &cleanedValues
2020-03-14 21:26:34 +00:00
}