envelope and data for email

The concept of envelope and email content(formally called “Message”)  is confusing because we see many redundant stuff here. For example, there are an envelope “mail from” and a “from” header in the message itself. There are “rcpt to” in the envelope and a “To” email header. What is the difference between envelope from and from header in the message? What is the difference between envelope “rcpt to” and the “To” email header?

From the view of SMTP interaction, there is no difference between envelope and message. They are all the byte stream sent through a TCP connection. The SMTP server analyzes received byte stream and splits it into envelope and message. How does the SMTP server split the byte stream? It uses the “\r\n” as the separator.  In most cases, a line is an SMTP command such as “EHLO”,”MAIL FROM”,”RCPT TO”, etc. When SMTP server receives a command, it will respond to the client with a code. If SMTP server receives a “DATA” command, it will keep receiving the following bytes (the email content)ignoring “\r\n”  until it receives “\r\n”.”\r\n”(a single dot in a new line), then it realizes the email content is over and starts the subsequent relay/delivery process. The bytes after the DATA and before the dot-only line constitute the message.

A message is further split into headers and body by an empty line. There are many possible email headers such as “From:”, “To:”, “Date:”,”Subject:”,”MIME-Version:”, “Content-Type:”, “Content-Transfer-Encoding:”, etc. A special content-type is multipart content-type like:

Content-Type: multipart/related; boundary=9355a2e3ae86a0bbaab5871e39d235d

The body of a multipart message( a message with content-type:mutlipat/*) has multiple parts separated by boundary lines(the boundary line is defined also in the Content-Type: header). Note that every part of a multipart body can also have headers and body itself(separated by an empty line). Multipart message can have nested structure. Common mutlipart types are multipart/related, multipart/alternative, multipart/mixed,multipart/digest,multipart/report,multipart/signed,multipart/encrypted.

Posted in tips of hosting