0

获取邮件后fetchmail,新邮件将存储在类似/var/mail/user. user我们可以通过文本编辑器打开文件,例如vim.

如何创建此类基于文本的电子邮件文件?说,我想发送一封包含以下内容的电子邮件:

From: sender <sender@xx.com> 
To: receiver <receiver@xx.com>
Subject: test subject
Contents: ...
Attached: file1.txt, file2.png, file3.pdf

问题是如何使这些成为正式的基于文本的电子邮件。

此外,如果我有这样的电子邮件文件。如何通过某些命令行工具提取文件(例如,主题、内容、附件等)。我知道我可以用mutt. 这可以使用命令行实用程序来完成吗?

4

3 回答 3

4

您需要了解许多标准,但电子邮件基本上是文本。

/var/spool/mailor 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--
于 2013-09-04T03:54:25.330 回答
3

文件格式称为“mbox”。在 Wikipedia ( http://en.wikipedia.org/wiki/Mbox ) 以及整个 Internet上有一篇很好的文章。像 RFC 4155。:)

于 2013-09-04T03:53:55.483 回答
0
telnet your.mail.server 25
helo localhost.localdomain
mail from:<sender@address.com>
rcpt to:<recipient@address.com>
data
From:Me
Subject:This is an email via Telnet

Hi,

The first line connects to the server on port 25. Replace "your.mail.server" with the name or address of the MX server for the domain.
Most servers expect the second "HELO" line to begin the session. I have seen servers that don't care, but in general they should throw an error.
You must have a "MAIL FROM:" line with the address you expect a reply to come to.
The mail is going nowhere if you don't specify the "RCPT TO:" address.
The message body begins with "DATA" line. This will usually be met with instruction on how to end the message - a single "." on a line by itself.
The "From:" and "Subject:" headers above are optional. You can add any additional headers here.

.
quit
于 2013-09-04T04:05:10.697 回答