summaryrefslogtreecommitdiff
path: root/internal/email/util.go
diff options
context:
space:
mode:
authorLibravatar Forest Johnson <forest.n.johnson@gmail.com>2022-01-31 10:46:20 +0000
committerLibravatar GitHub <noreply@github.com>2022-01-31 11:46:20 +0100
commit5be8a7a7ea96d962d0f0b9f09b967e403a227698 (patch)
tree448ae55de146463dc86005e55ae5f2e1eb8f8bc8 /internal/email/util.go
parent[bug] Fix sqlite empty address issue (#370) (diff)
downloadgotosocial-5be8a7a7ea96d962d0f0b9f09b967e403a227698.tar.xz
[bug] Send plaintext emails to fix "message refused: Message is not RFC 2822 compliant" (#366)
* trying to fix "message refused: Message is not RFC 2822 compliant" * fix "message refused: Message is not RFC 2822 compliant" 550 5.7.1 Delivery not authorized, message refused: Message is not RFC 2822 compliant * remove silly regex * lint * fix tests * we should use text/template instead of html/template now
Diffstat (limited to 'internal/email/util.go')
-rw-r--r--internal/email/util.go45
1 files changed, 30 insertions, 15 deletions
diff --git a/internal/email/util.go b/internal/email/util.go
index db95128fa..52290dbe4 100644
--- a/internal/email/util.go
+++ b/internal/email/util.go
@@ -19,15 +19,12 @@
package email
import (
+ "errors"
"fmt"
- "html/template"
"os"
"path/filepath"
-)
-
-const (
- mime = `MIME-version: 1.0;
-Content-Type: text/html;`
+ "strings"
+ "text/template"
)
func loadTemplates(templateBaseDir string) (*template.Template, error) {
@@ -41,16 +38,34 @@ func loadTemplates(templateBaseDir string) (*template.Template, error) {
return template.ParseGlob(tmPath)
}
-func assembleMessage(mailSubject string, mailBody string, mailTo string, mailFrom string) []byte {
- from := fmt.Sprintf("From: GoToSocial <%s>", mailFrom)
- to := fmt.Sprintf("To: %s", mailTo)
+// https://datatracker.ietf.org/doc/html/rfc2822
+// I did not read the RFC, I just copy and pasted from
+// https://pkg.go.dev/net/smtp#SendMail
+// and it did seem to work.
+func assembleMessage(mailSubject string, mailBody string, mailTo string, mailFrom string) ([]byte, error) {
+
+ if strings.Contains(mailSubject, "\r") || strings.Contains(mailSubject, "\n") {
+ return nil, errors.New("email subject must not contain newline characters")
+ }
+
+ if strings.Contains(mailFrom, "\r") || strings.Contains(mailFrom, "\n") {
+ return nil, errors.New("email from address must not contain newline characters")
+ }
+
+ if strings.Contains(mailTo, "\r") || strings.Contains(mailTo, "\n") {
+ return nil, errors.New("email to address must not contain newline characters")
+ }
+
+ // normalize the message body to use CRLF line endings
+ mailBody = strings.ReplaceAll(mailBody, "\r\n", "\n")
+ mailBody = strings.ReplaceAll(mailBody, "\n", "\r\n")
msg := []byte(
- mailSubject + "\r\n" +
- from + "\r\n" +
- to + "\r\n" +
- mime + "\r\n" +
- mailBody + "\r\n")
+ "To: " + mailTo + "\r\n" +
+ "Subject: " + mailSubject + "\r\n" +
+ "\r\n" +
+ mailBody + "\r\n",
+ )
- return msg
+ return msg, nil
}