Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ func writeQuotedPrintable(w io.Writer, body string) error {
return nil
}

func writePlainTextPartHeaders(w io.Writer) {
fmt.Fprintf(w, "Content-Type: text/plain; charset=UTF-8\r\n")
fmt.Fprintf(w, "Content-Transfer-Encoding: quoted-printable\r\n\r\n")
}

// SendEmail constructs a multipart message with plain text, HTML, embedded images, and attachments.
func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody, htmlBody string, images map[string][]byte, attachments map[string][]byte, inReplyTo string, references []string, signSMIME bool, encryptSMIME bool, signPGP bool, encryptPGP bool) ([]byte, error) {
smtpServer := account.GetSMTPServer()
Expand Down Expand Up @@ -263,8 +268,7 @@ func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody

// Build the canonical MIME part (headers + body) used for signing/encryption
var partBuf bytes.Buffer
fmt.Fprintf(&partBuf, "Content-Type: text/plain; charset=UTF-8; format=flowed\r\n")
fmt.Fprintf(&partBuf, "Content-Transfer-Encoding: quoted-printable\r\n\r\n")
writePlainTextPartHeaders(&partBuf)
partBuf.Write(encodedBody)
canonicalPart := partBuf.Bytes()

Expand Down Expand Up @@ -349,8 +353,7 @@ func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody
payloadToEncrypt = canonicalBody
} else {
// Write Content-Type and body as top-level single part
fmt.Fprintf(&msg, "Content-Type: text/plain; charset=UTF-8; format=flowed\r\n")
fmt.Fprintf(&msg, "Content-Transfer-Encoding: quoted-printable\r\n\r\n")
writePlainTextPartHeaders(&msg)
msg.Write(encodedBody)
}
}
Expand Down
18 changes: 18 additions & 0 deletions sender/sender_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sender

import (
"bytes"
"errors"
"io"
"strings"
Expand Down Expand Up @@ -92,6 +93,23 @@ func TestSMTPHelloHostname(t *testing.T) {
}
}

func TestWritePlainTextPartHeadersDoesNotClaimFormatFlowed(t *testing.T) {
var buf bytes.Buffer

writePlainTextPartHeaders(&buf)

got := buf.String()
if strings.Contains(got, "format=flowed") {
t.Fatalf("plaintext headers should not claim format=flowed: %q", got)
}
if !strings.Contains(got, "Content-Type: text/plain; charset=UTF-8\r\n") {
t.Fatalf("plaintext headers missing content type: %q", got)
}
if !strings.Contains(got, "Content-Transfer-Encoding: quoted-printable\r\n\r\n") {
t.Fatalf("plaintext headers missing quoted-printable encoding: %q", got)
}
}

// TestGenerateMessageID ensures the Message-ID has the correct format.
func TestGenerateMessageID(t *testing.T) {
from := "test@example.com"
Expand Down
Loading