简体   繁体   中英

How to create an email file?

After the fetchmail fetches mails, the new mails are stored in a file like /var/mail/user . We can open the file user by text editor like vim .

How can I create such text-based email files? Say, I want to send an email with contents:

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

The problem is how to make these to be a formal text-based email.

Besides, If I have such an email file. How can I extract files(say, subjects, contents, attached files, etc.) by some command line tools. I know I can open it with program like mutt . Can this be done using a command line utility?

The file format is called "mbox". There's a good article on Wikipedia ( http://en.wikipedia.org/wiki/Mbox ), as well as all over the Internet. Like RFC 4155. :)

There is a bunch of standards you need to understand, but email is fundamentally text.

The file format in /var/spool/mail or /var/mail/user etc is typically Berkeley mbox . This is not formally defined anywhere, but consists of a sequence of RFC5322 (née RFC822) email messages, each preceded by a From_ line, the format of which is basically From %s %C where %s is the sender's email address (what you also see in Return-Path: ) and %C is the date when the message arrived. Notice the two spaces between the format strings!

The toplevel email message is RFC5322 but on top of that, you need to understand MIME .

You will also stumble over (E)SMTP RFC5321 which is only tangential to your question, but good to know. Notice how 821 and 822 (and later 2821 and 2822, and now 5321 and 5322) have adjacent RFC numbers.

Furthermore, there is a wild, wild West of non-standard headers, some of which are nonetheless significant. Dan Bernstein's reference http://cr.yp.to/immhf.html is a lifesaver. As a general guideline, what spammers typically do is copy/paste headers without understanding them; therefore, an essential practice for deliverability is "don't do that". In other words, if you don't know what a header is for, don't use it.

Any modern programming language will come with libraries to create and manipulate RFC5322 and MIME, and probably mbox too. For creating a message you can send somewhere, you don't need mbox anyway, just something along the lines of (pseudocode)

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);

A multipart message looks something like what you describe in your question, except there is no specific header to identify "attachments" and actually conceptually no "attachments", just "body parts". Here is a simple MIME message to show what the message in your question would properly look something like.

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--
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM