您需要了解许多标准,但电子邮件基本上是文本。
/var/spool/mail
or etc 中的文件格式/var/mail/user
通常是 Berkeley mbox
。这在任何地方都没有正式定义,而是由一系列 RFC5322(née RFC822)电子邮件消息组成,每条消息前面都有From_
一行,其格式基本上From %s
%C
是%s
发件人的电子邮件地址(在 中也可以看到Return-Path:
)和%C
日期当消息到达时。注意格式字符串之间的两个空格!
顶级电子邮件消息是 RFC5322,但最重要的是,您需要了解MIME。
您还将偶然发现 (E)SMTP RFC5321,这与您的问题无关,但很高兴知道。请注意 821 和 822(以及后来的 2821 和 2822,现在是 5321 和 5322)如何具有相邻的 RFC 编号。
此外,非标准标题的狂野西部,其中一些仍然很重要。Dan Bernstein 的参考资料http://cr.yp.to/immhf.html是救命稻草。作为一般准则,垃圾邮件发送者通常会在不理解标题的情况下复制/粘贴标题;因此,交付能力的基本做法是“不要那样做”。换句话说,如果您不知道标头的用途,请不要使用它。
任何现代编程语言都将附带库来创建和操作 RFC5322 和 MIME,而且可能mbox
也是如此。为了创建可以发送到某处的消息,无论如何您都不需要mbox
,只需要类似于(伪代码)的东西
message = new MIME({'Subject': 'hello', 'From': 'me@example.net',
'To': 'My Friend <you@example.com>'});
message.addbodypart('text/plain', 'Hi Fred.\nHow are you?');
message.addbodypart('image/png', {'file': '/home/you/logo.png'});
smtp = new SMTP('mail.example.net', 587, {'user': 'me', 'pass': 'xyzzy'});
smtp.send(message);
多部分消息看起来像您在问题中描述的内容,除了没有特定的标题来识别“附件”,实际上在概念上没有“附件”,只是“正文部分”。这是一条简单的 MIME 消息,用于显示您问题中的消息看起来应该是什么样子。
From: sender <sender@example.com>
To: receiver <receiver@example.com>
Subject: test subject
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="so_long_eFlop"
This is a MIME multipart message. Nobody actually sees what it says here.
--so_long_eFlop
Content-type: text/plain; charset="utf-8"
Content-disposition: inline
Content-transfer-encoding: 7bit
Many mail clients will display this as the "main part" but MIME does not
define any particular hierarchy. Many mail clients will generate a
text/plain rendering and a text/html rendering of the message you type in,
and the recipient's mail client will decide -- based on user preferences
-- which one to display. Anyway, I will not create an example of that
here. This is just "a text message with a picture attached", or, more
precisely, a MIME message with two body parts.
Oh, the content-disposition: inline is usually just implied for a
text/plain part. Some clients will override or ignore the disposition
set by the sender anyway.
--so_long_eFlop
Content-type: image/png
Content-disposition: attachment
Content-transfer-encoding: base64
Iam+not/attaching+a/real00picture+here/just/a/bunch0of/binary/goo===
--so_long_eFlop--