Skip to content

fix(mailer): generate RFC-compliant MIME messages - #2825

Open
lzyruc wants to merge 2 commits into
dagucloud:mainfrom
lzyruc:fix/mailer-rfc-mime
Open

lzyruc wants to merge 2 commits into
dagucloud:mainfrom
lzyruc:fix/mailer-rfc-mime

Conversation

@lzyruc

@lzyruc lzyruc commented Sep 20, 2026

Copy link
Copy Markdown

Summary

  • add Date, Message-ID, and MIME-Version headers to outgoing messages
  • send messages without readable attachments as single-part text/html
  • use multipart/mixed only when attachments are present
  • generate a unique MIME boundary for each multipart message
  • wrap base64-encoded content at 76 characters per line
  • preserve the existing HTML body behavior

Tests

  • go test -p 1 -parallel 1 ./internal/cmn/mailer -count=1
  • go test -p 1 -parallel 1 ./internal/runtime/builtin/mail -count=1
  • go vet ./internal/cmn/mailer ./internal/runtime/builtin/mail

Fixes #2803


Summary by cubic

Fixes #2803 by generating RFC-compliant MIME messages so email clients no longer misinterpret outgoing mail. Previously every message used multipart/mixed with a fixed boundary and lacked Date, Message-ID, and MIME-Version; now all messages include these headers, messages without readable attachments are single-part text/html, multipart messages use unique boundaries, and base64-encoded content is wrapped at 76 characters per line. SMTP sending now reports email composition failures clearly.

Written for commit 500e8c9. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes
    • Improved email formatting for messages with attachments, including correct MIME metadata and encoding.
    • Messages without usable attachments are now sent as standard single-part HTML emails.
    • Improved handling of unreadable or empty attachment files.
    • Generated messages now use unique identifiers and multipart boundaries.
    • SMTP sending now reports email composition failures clearly.

@coderabbitai

coderabbitai Bot commented Sep 20, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 4a9892dd-6b22-4f75-a698-42ac45e32846

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 84e63f28-0e07-4463-be40-0f2d673146de

📥 Commits

Reviewing files that changed from the base of the PR and between 0a8edff and e325b6f.

📒 Files selected for processing (2)
  • internal/cmn/mailer/mailer.go
  • internal/cmn/mailer/mailer_test.go

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The mailer now creates RFC-formatted single-part or multipart messages, adds generated message metadata, encodes attachments with standard MIME writers, skips invalid attachments, and returns composition errors. Tests parse generated messages and verify headers, parts, boundaries, IDs, encoding, and delimiters.

Changes

Mailer MIME composition

Layer / File(s) Summary
MIME composition and error handling
internal/cmn/mailer/mailer.go
The composer now generates message IDs, MIME metadata, random multipart boundaries, single-part HTML messages, and multipart messages for readable attachments. It uses standard multipart writers, wraps encoded data at 76 characters, skips unreadable or empty attachments, removes the legacy attachment builder, and propagates composition errors through Send.
MIME composition validation
internal/cmn/mailer/mailer_test.go
Tests verify single-part output without usable attachments, multipart output with readable attachments, required MIME metadata, distinct boundaries and message IDs, attachment encoding, header sanitization, and one closing boundary delimiter.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to e325b

The mailer preserves its HTML body contract and no actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: generating RFC-compliant MIME messages in the mailer.
Description check ✅ Passed The description explains the changes, links the issue, and lists targeted tests. It does not use the template's explicit Changes or Checklist sections, but it provides the required change and validati…
Linked Issues check ✅ Passed The changes satisfy issue #2803. composeHeader adds Date, Message-ID, and MIME-Version: 1.0 to each composed message. Messages without readable attachments use a single text/html part. Messa…
Out of Scope Changes check ✅ Passed The pull request stays within issue #2803. The production changes replace the static MIME construction, add standard headers, select single-part or multipart output based on readable attachments, and …
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: mail.send produces non-RFC-compliant MIME (missing Date/Message-Id/MIME-Version), triggers spam filters

1 participant