You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用mail命令发送MIME邮件内容失败,如何让收件端正确识别?

Fixing Raw MIME Email Display When Sending via mail Command

Hey there! I totally get the frustration when you put together a MIME email manually, send it, and the recipient just sees a wall of raw code instead of properly formatted content. Let's break down why this is happening and how to fix it—it's definitely feasible, you just need to nail a couple key details.

Common Reasons Your MIME Email Is Showing as Raw Text

  • The mail command is overriding your MIME headers: By default, many versions of the mail utility add their own Content-Type header (usually text/plain) if it doesn't detect one properly, or modifies existing headers. This breaks the multipart structure the recipient's email client expects.
  • Incomplete or incorrect MIME structure: Your Python script might be missing critical parts of the MIME format—like a properly declared multipart/mixed Content-Type with a boundary, or properly formatted sub-parts (each with their own headers).
  • Missing top-level email headers: Your generated MIME content needs to include standard headers like From, To, Subject, and the core Content-Type for the multipart container before the body parts.

Step-by-Step Fixes

1. Ensure Your Python Script Generates a Valid MIME Structure

First, let's make sure your script is outputting a properly formatted MIME message. Using Python's built-in email library (instead of manually constructing strings) will avoid most formatting errors. Here's a corrected example that builds a multipart/mixed email with a plain text part and an attachment:

#!/usr/bin/env python
# coding: utf-8
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# Create the multipart container
msg = MIMEMultipart('mixed')
msg['From'] = 'your-email@example.com'
msg['To'] = 'recipient@example.com'
msg['Subject'] = 'Test Multipart Email'

# Add plain text body part
plain_text = MIMEText('This is the plain text body of the email.', 'plain', 'utf-8')
msg.attach(plain_text)

# Add an attachment
filename = 'example.txt'
attachment = open(filename, 'rb')

part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f'attachment; filename= {filename}')

msg.attach(part)

# Generate the full MIME string
full_email = msg.as_string()

# Write to a file for testing or direct sending
with open('output_email.txt', 'w') as f:
    f.write(full_email)

2. Send the Email Without Breaking the MIME Structure

The mail command can be tricky here—instead of letting it handle headers, use flags to tell it to respect your pre-built MIME content:

Option 1: Use mail with -E and -t flags

The -E flag prevents mail from adding extra headers, and -t tells it to read To, From, and Subject directly from your MIME content:

cat output_email.txt | mail -E -t

Option 2: Use sendmail directly (more reliable)

If mail is still causing issues, use sendmail which is more low-level and won't modify your content:

cat output_email.txt | sendmail -t

3. Verify the MIME Content

Before sending, check your generated output_email.txt to confirm:

  • The first section includes Content-Type: multipart/mixed; boundary="..." (the boundary string should match what's used to separate parts)
  • Each sub-part starts with --[boundary-string]
  • The final boundary ends with -- (e.g., --abc123--)
  • All headers are properly formatted (no typos, correct spacing)

Why This Works

When your email client receives a properly structured multipart/mixed message with the correct headers, it knows to parse each boundary-separated part individually—displaying the text body normally and treating attachments as files, instead of showing the raw code.

内容的提问来源于stack exchange,提问作者Neal.Marlin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:29:00