From c05af16cf4fb6c318324f390eea49acda1e76b00 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Mon, 13 Jan 2025 23:37:10 +0100 Subject: [PATCH] email: fix partial bodies Depending on the email and buffer size, cleartext.Close() and body.Close() may happen *after* buf.Bytes() is called; producing incomplete emails, interrupted in the middle of the body. Make sure to close both *before* getting the buffer contents. Fixes: db4d67a2a6fb ("email: fix empty emails sent on retries") Signed-off-by: Robin Jarry --- email/worker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/email/worker.go b/email/worker.go index 5437090297c65eea3cd0ae0c6c3341709c61d7de..6d0bd9ef00c7a182d1d47c8eac98cb01b7d2519c 100644 --- a/email/worker.go +++ b/email/worker.go @@ -157,7 +157,6 @@ func EnqueueStd(ctx context.Context, header mail.Header, cleartext = nopWriteCloser{&buf} } } - defer cleartext.Close() var inlineHeader mail.Header inlineHeader.SetContentType("text/plain", map[string]string{ @@ -168,12 +167,13 @@ func EnqueueStd(ctx context.Context, header mail.Header, if err != nil { panic(err) } - defer body.Close() _, err = io.Copy(body, bodyReader) if err != nil { log.Fatal(err) } + body.Close() + cleartext.Close() return queue.Enqueue(NewTask(buf.Bytes(), rcpts)) }