Before discussing the delivery process of local emails, let's clarify the definitions of "local", "foreign", and "virtual" emails and the related MDA.
PostfixShipping Agent
The postfix determines whether to accept the email Based on the email address and how to select the appropriate MDA for subsequent shipping tasks. Postfix receives emails from three domains: local, relay, and virtual. Their definitions and related MDA are as follows:
Local email
If the mail terminal is one of the domains listed by the mydestination parameter, the postfix regards it as a local mail, and the local MDA (or other programs you specify) executes the shipping task. The recipient of a local email must have the user account of the local system (the host where the postfix server is located) or its name is defined in the alias file (traditionally/etc/aliases ). The local mail will be delivered to the system's mail storage directory (usually/var/spool/mail/), or the mail file in the personal home directory (~ /Mail /).
Forward email
If the mail terminal is one of the domains listed by the relay_domains parameter, the postfix regards it as the forwarded mail, and the relay MDA executes the shipping task. Generally, a postfix can be used only when the postfix is used as the email gateway of the local area network and there are other domain email servers on the same network. That is to say, the so-called "forwarding" usually refers to other hosts on the same local area network, while relay is actually a copy of smtp MDA, it was deliberately designed to be especially suitable for sending messages to hosts on the local network.
Virtual domain email
An email server generally serves only one canonical domain. To serve multiple domains at the same time, an additional domain is called a virtual domain ). Virtual domains are delivered by virtual MDA. Based on whether the user has a system account on the server, the email in the virtual domain can also be divided into "virtual mailbox" and "virtual alias. The recipient of a virtual mailbox does not have a system account, and each virtual mailbox domain has its own mailbox directory (mail spool). All virtual mailbox domains must be listed in the virtual_mailbox_domains parameter. On the other hand, the recipient of a virtual alias domain can have a local or non-local SYSTEM account, and the postfix will rewrite the Receiving address of this type of mail and deliver it to smtp MDA (if the new address is not a local domain ), or return to the receiving Queue (if the new address is a local domain ).
Email format
When the postfix is delivered to a local email, the email content is sent to the appropriate mailbox on the postfix system. The two most common mailbox formats are the traditional mbox and the newer maildir. Both use common files to store mail content. The difference is that the internal organization of files is different. In postfix, when you set any mail file or directory parameters, if you add a/symbol at the end of the path, it means you want to use a mailbox in maildir format.
Mbox format
Traditionally, the unix system stores all emails of the same user in the same file. A mailbox format like this is usually called mbox. The first five characters of each email in the email file must be "from ". Traditionally, for convenience, we usually write it as "from _", the following underlined characters emphasize the existence of spaces. Do not confuse the "from" used to separate emails in the mbox file with the "from:" field in the mail header. The last line of the email in the mbox file must be blank. Therefore, a line of space followed by a from _ can be considered as the beginning of the next letter.
Before postfix writes an email to the mbox file, the from _ text line is created using the sender address on the envelope and the date at that time, and the string of the line is written to the end of the mbox file, then enter the email content. If the postfix finds that the mail content itself has any line of text starting with "from", it will add a> symbol at the beginning of the line, avoid the text line being mistaken for the beginning of the next letter.
When the pop/imap server reads emails in the mbox file, the first step is to scan the file content and find the from text line that indicates the beginning of the mail. When reading the mail content, if you encounter the next from _ text line (or the end of the file), you can conclude that the current mail has been read. Some pop/imap servers will take the initiative to restore the original state of "> from", but some will not.
Since postfix and pop/imap server may access the same mbox file at the same time, all of them must use the file locking Mechanism to ensure access. Before a local email is delivered to a local email, you must lock the file before writing the email content to the mbox file. Postfix supports multiple locking mechanisms, depending on the system platform. Use the postconf-l command to check which locking mechanisms your system provides for postfix:
Postconf-l
If you want to know the details of the various locking mechanisms listed by postfix on your system, please tell man the name of the locking mechanism:
Man folck
If your system platform supports flock and fcntl, you can find their online instruction files, because both are functions provided by the operating system or function library, the dotlock mechanism supported by any system platform may not be able to find the instruction file, because dotlock is only an unwritten protocol between programs and does not require additional function libraries. The principle of dotlock is very simple. You can give an example to illustrate it clearly. Assume that the postfix needs to access the user1 email file. It must first check whether there is one in the same directory of the file. user1.lock file. If it exists, it indicates that the user1 email file is currently occupied by another process; if. if the user1.lock file step exists, the postfix will generate one to let other processes know that the user1 file is currently in use. After the postfix closes the user1 file, you must actively remove the. user1.lock file so that other processes can use the user1 email file. The disadvantage of the dotlock locking mechanism is that it is not mandatory (any process can not check whether user1.lock exists and directly access the user1 file), and the efficiency is not good.
Generally, you do not have to worry about the details of the locking mechanism or the types of locking mechanisms supported by the system, because postfix can automatically make the best choice.
Maildir format
The maildir mailbox format is different from mbox in that it uses a directory structure to store emails. Maildir is designed to solve the possibility of mbox format and file lock problems. For example, if the system crashes before the email content is fully written to the mbox file, only part of the content may be in the mailbox. When the system resumes operation, when MDA writes emails to the mailbox, the new content will be followed by the previous incomplete content, resulting in problems.
Another drawback of mbox format is that when pop/imap server and smtp server attempt to enable the same mailbox at the same time. If the two parties do not use the same locking mechanism, the mailbox file may be damaged. As mentioned earlier, there are several file locking mechanisms, but not all mail programs use locking mechanisms. However, if the maildir format is used, the file protection lock is not used, because each mail is stored in a separate file. Therefore, you cannot access the same file at the same time by using an unused email program.
A maildir directory contains three subdirectories: tmp/, new/, and cur /. These subdirectories and their upper directories must be located in the same file system. Normally, they should be placed in the mail directory of the user's main directory.
The email file in the new/directory is a message that the MDA has been delivered but has not been read by the user. The modification time of the file itself is the time when the email is received. The email file usually contains emails in RFC 2822 format and does not require "from _".
After you read the email, the email file will be transferred to the cur/directory. The tmp/directory allows the MDA to store the mail content as a file. After all the files are written to the file, the mail file will be moved to the new/directory.
Should I select mbox or maildir?
There is no simple answer to this question. Which mailbox format is the most suitable for you depends on many factors. The advantage of mbox format is that it is supported almost all over the world, but it is precisely because it has a file lock problem that leads to the emergence of maildir format. The maildir format is also quite questionable in terms of scalability, because some file systems may not be able to cope with too many Mail Files. In terms of efficiency, the two formats have their own advantages and disadvantages: When searching, accessing, and deleting a specific email, maildir is faster; but in terms of the delivery efficiency of MDA, it may be faster to directly put the email content into text (in mbox format. In fact, your choice may depend on the POP/imap server you use. If the POP/imap server you set up only supports the maildir format, you obviously have no choice. Postfix supports both formats, so you only need to consider other factors. If your environment makes you feel embarrassed, we recommend that you test the two formats, try to experiment with the actual running environment and workload as close as possible, and make a choice based on the experiment results.