mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
Translated by qhwdw
This commit is contained in:
parent
c47e4e81a0
commit
a6a16c708c
@ -1,269 +0,0 @@
|
|||||||
Translating by qhwdw
|
|
||||||
Mail transfer agent (MTA) basics
|
|
||||||
======
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
In this tutorial, learn to:
|
|
||||||
|
|
||||||
* Use the `mail` command.
|
|
||||||
* Create mail aliases.
|
|
||||||
* Configure email forwarding.
|
|
||||||
* Understand common mail transfer agent (MTA) programs such as postfix, sendmail, qmail, and exim.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Controlling where your mail goes
|
|
||||||
|
|
||||||
Email on a Linux system is delivered using MTAs. Your MTA delivers mail to other users on your system and MTAs communicate with each other to deliver mail all over a group of systems or all over the world.
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
|
|
||||||
To get the most from the tutorials in this series, you need a basic knowledge of Linux and a working Linux system on which you can practice the commands covered in this tutorial. You should be familiar with GNU and UNIX commands. Sometimes different versions of a program format output differently, so your results might not always look exactly like the listings shown here.
|
|
||||||
|
|
||||||
In this tutorial, I use Ubuntu 14.04 LTS and sendmail 8.14.4 for the sendmail examples.
|
|
||||||
|
|
||||||
## Mail transfer
|
|
||||||
|
|
||||||
Mail transfer agents such as sendmail deliver mail between users and between systems. Most Internet mail uses the Simple Mail Transfer Protocol (SMTP), but local mail may be transferred through files or sockets among other possibilities. Mail is a store and forward operation, so mail is stored in some kind of file or database until a user collects it or a receiving system or communication link is available. Configuring and securing an MTA is quite a complex task, most of which is beyond the scope of this introductory tutorial.
|
|
||||||
|
|
||||||
## The mail command
|
|
||||||
|
|
||||||
If you use SMTP email, you probably know that there are many, many mail clients that you can use, including `mail`, `mutt`, `alpine`, `notmuch`, and a host of other console and graphical mail clients. The `mail` command is an old standby that can be used to script the sending of mail as well as receive and manage your incoming mail.
|
|
||||||
|
|
||||||
You can use `mail` interactively to send messages by passing a list of addressees, or with no arguments you can use it to look at your incoming mail. Listing 1 shows how to send a message to user steve and user pat on your system with a carbon copy to user bob. When prompted for the cc:user and the subject, enter the body and complete the message by pressing **Ctrl+D** (hold down the Ctrl key and press D).
|
|
||||||
|
|
||||||
##### Listing 1. Using `mail` interactively to send mail
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ mail steve,pat
|
|
||||||
Cc: bob
|
|
||||||
Subject: Test message 1
|
|
||||||
This is a test message
|
|
||||||
|
|
||||||
Ian
|
|
||||||
```
|
|
||||||
|
|
||||||
If all is well, your mail is sent. If there is an error, you will see an error message. For example, if you typed an invalid name as a recipient, the mail is not sent. Note that in this example, all users are on your local system and therefore all must be valid users.
|
|
||||||
|
|
||||||
You can also send mail non-interactively using the command line. Listing 2 shows how to send a small message to users steve and pat. This capability is particularly useful in scripts. Different versions of the `mail` command are available in different packages. Some support a `-c` option for cc:, but the version I am using here does not, so I specify only the to: addresses.
|
|
||||||
|
|
||||||
Listing 2. Using `mail` non-interactively
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ mail -t steve,pat -s "Test message 2" <<< "Another test.\n\nIan"
|
|
||||||
```
|
|
||||||
|
|
||||||
If you use `mail` with no options you will see a list of your incoming mail as shown in Listing 3. You see that user steve has the two messages I sent above, plus an earlier one from me and a later one from user bob. All the mail is marked as 'N' for new mail.
|
|
||||||
|
|
||||||
Listing 3. Using `mail` for incoming mail
|
|
||||||
```
|
|
||||||
steve@attic4-u14:~$ mail
|
|
||||||
"/var/mail/steve": 4 messages 4 new
|
|
||||||
>N 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
|
||||||
N 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
|
||||||
N 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
|
||||||
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
|
||||||
?
|
|
||||||
```
|
|
||||||
|
|
||||||
The currently selected message is shown with a '>', which is message number 1 in Listing 3. If you press **Enter** , the first page of the next unread message will be displayed. Press the **Space bar** to page through the message. When you finish reading the message and return to the '?' prompt, press **Enter** again to view the next message, and so on. At any '?' prompt you can type 'h' to see the list of message headers again. The ones you have read will now show 'R' in the status as shown in Listing 4.
|
|
||||||
|
|
||||||
Listing 4. Using 'h' to display mail headers
|
|
||||||
```
|
|
||||||
? h
|
|
||||||
R 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
|
||||||
R 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
|
||||||
>R 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
|
||||||
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
|
||||||
?
|
|
||||||
```
|
|
||||||
|
|
||||||
Here Steve has read the three messages from Ian but has not read the message from Bob. You can select individual messages by number, and you can also delete messages that you don't want by typing 'd', or '3d' to delete the third message. If you type 'q' you will quit the `mail` command. Messages that you have read will be transferred to the mbox file in your home directory and the unread messages will remain in your inbox, by default in /var/mail/$(id -un). See Listing 5.
|
|
||||||
|
|
||||||
Listing 5. Using 'q' to quit `mail`
|
|
||||||
```
|
|
||||||
? h
|
|
||||||
R 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
|
||||||
R 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
|
||||||
>R 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
|
||||||
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
|
||||||
? q
|
|
||||||
Saved 3 messages in /home/steve/mbox
|
|
||||||
Held 1 message in /var/mail/steve
|
|
||||||
You have mail in /var/mail/steve
|
|
||||||
```
|
|
||||||
|
|
||||||
If you type 'x' to exit instead of 'q' to quit, your mailbox will be left unchanged. Because this is on the /var file system, your system administrator may allow mail to be kept there only for a limited time. To reread or otherwise process mail that has been saved to your local mbox file, use the `-f` option to specify the file you want to read. For example `mail -f mbox`.
|
|
||||||
|
|
||||||
## Mail aliases
|
|
||||||
|
|
||||||
In the previous section you saw how mail can be sent to various users on a system. You can use a fully qualified name, such as ian@myexampledomain.com to send mail to a user on another system.
|
|
||||||
|
|
||||||
Sometimes you might want all the mail for a user to go to some other place. For example, you may have a server farm and want all the root mail to go to a central system administrator. Or you may want to create a mailing list where mail goes to several people. To do this, you use aliases that allow you to define one or more destinations for a given user name. The destinations may be other user mail boxes, files, pipes, or commands that do further processing. You do this by specifying the aliases in /etc/mail/aliases or /etc/aliases. Depending on your system, you may find that one of these is a symbolic link to the other, or you may have only one of them. You need root authority to change the aliases file.
|
|
||||||
|
|
||||||
The general form of an alias is
|
|
||||||
name: addr_1, addr_2, addr_3, ...
|
|
||||||
where the name is a local user name to alias or an alias and the addr_1, addr_2, ... are one or more aliases. Aliases can be a local user, a local file name, another alias, a command, an include file, or an external address.
|
|
||||||
|
|
||||||
So how does sendmail distinguish the aliases (the addr-N values)?
|
|
||||||
|
|
||||||
* A local user name is a text string that matches the name of a user on this system. Technically this means it can be found using the `getpwnam` call .
|
|
||||||
* A local file name is a full path and file name that starts with '/'. It must be writeable by `sendmail`. Messages are appended to the file.
|
|
||||||
* A command starts with the pipe symbol (|). Messages are sent to the command using standard input.
|
|
||||||
* An include file alias starts with :include: and specifies a path and file name. The aliases in file are added to the aliases for this name.
|
|
||||||
* An external address is an email address such as john@somewhere.com.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
You should find an example file, such as /usr/share/sendmail/examples/db/aliases that was installed with your sendmail package. It contains some recommended aliases for postmaster, MAILER-DAEMON, abuse, and spam. In Listing 6, I have combined entries from the example file on my Ubuntu 14.04 LTS system with some rather artificial examples that illustrate several of the possibilities.
|
|
||||||
|
|
||||||
Listing 6. Somewhat artificial /etc/mail/aliases example
|
|
||||||
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ cat /etc/mail/aliases
|
|
||||||
# First include some default system aliases from
|
|
||||||
# /usr/share/sendmail/examples/db/aliases
|
|
||||||
|
|
||||||
#
|
|
||||||
# Mail aliases for sendmail
|
|
||||||
#
|
|
||||||
# You must run newaliases(1) after making changes to this file.
|
|
||||||
#
|
|
||||||
|
|
||||||
# Required aliases
|
|
||||||
postmaster: root
|
|
||||||
MAILER-DAEMON: postmaster
|
|
||||||
|
|
||||||
# Common aliases
|
|
||||||
abuse: postmaster
|
|
||||||
spam: postmaster
|
|
||||||
|
|
||||||
# Other aliases
|
|
||||||
|
|
||||||
# Send steve's mail to bob and pat instead
|
|
||||||
steve: bob,pat
|
|
||||||
|
|
||||||
# Send pat's mail to a file in her home directory and also to her inbox.
|
|
||||||
# Finally send it to a command that will make another copy.
|
|
||||||
pat: /home/pat/accumulated-mail,
|
|
||||||
\pat,
|
|
||||||
|/home/pat/makemailcopy.sh
|
|
||||||
|
|
||||||
# Mailing list for system administrators
|
|
||||||
sysadmins: :include: /etc/aliases-sysadmins
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that pat is both an alias and a user of the system. Alias expansion is recursive, so if an alias is also a name, then it will be expanded. Sendmail does not send mail twice to a given user, so if you just put 'pat' as an alias for 'pat', then it would be ignored since sendmail had already found and processed 'pat'. To avoid this problem, you prefix an alias name with a '\' to indicate that it is a name not subject to further aliasing. This way, pat's mail can be sent to her normal inbox as well as the file and command.
|
|
||||||
|
|
||||||
Lines in the aliases that start with '$' are comments and are ignored. Lines that start with blanks are treated as continuation lines.
|
|
||||||
|
|
||||||
The include file /etc/aliases-sysadmins is shown in Listing 7.
|
|
||||||
|
|
||||||
Listing 7. The /etc/aliases-sysadmins include file
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ cat /etc/aliases-sysadmins
|
|
||||||
|
|
||||||
# Mailing list for system administrators
|
|
||||||
bob,pat
|
|
||||||
```
|
|
||||||
|
|
||||||
## The newaliases command
|
|
||||||
|
|
||||||
Most configuration files used by sendmail are compiled into database files. This is also true for mail aliases. You use the `newaliases` command to compile your /etc/mail/aliases and any included files to /etc/mail/aliases.db. Note that `newaliases` is equivalent to `sendmail -bi`. Listing 8 shows an example.
|
|
||||||
|
|
||||||
Listing 8. Rebuild the database for the mail aliases file
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ sudo newaliases
|
|
||||||
/etc/mail/aliases: 7 aliases, longest 62 bytes, 184 bytes total
|
|
||||||
ian@attic4-u14:~$ ls -l /etc/mail/aliases*
|
|
||||||
lrwxrwxrwx 1 root smmsp 10 Dec 8 15:48 /etc/mail/aliases -> ../aliases
|
|
||||||
-rw-r----- 1 smmta smmsp 12288 Dec 13 23:18 /etc/mail/aliases.db
|
|
||||||
```
|
|
||||||
|
|
||||||
## Examples of using aliases
|
|
||||||
|
|
||||||
Listing 9 shows a simple shell script that is used as a command in my alias example.
|
|
||||||
|
|
||||||
Listing 9. The makemailcopy.sh script
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ cat ~pat/makemailcopy.sh
|
|
||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# Note: Target file ~/mail-copy must be writeable by sendmail!
|
|
||||||
cat >> ~pat/mail-copy
|
|
||||||
```
|
|
||||||
|
|
||||||
Listing 10 shows the files that are updated when you put all this to the test.
|
|
||||||
|
|
||||||
Listing 10. The /etc/aliases-sysadmins include file
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ date
|
|
||||||
Wed Dec 13 22:54:22 EST 2017
|
|
||||||
ian@attic4-u14:~$ mail -t sysadmins -s "sysadmin test 1" <<< "Testing mail"
|
|
||||||
ian@attic4-u14:~$ ls -lrt $(find /var/mail ~pat -type f -mmin -3 2>/dev/null )
|
|
||||||
-rw-rw---- 1 pat mail 2046 Dec 13 22:54 /home/pat/mail-copy
|
|
||||||
-rw------- 1 pat mail 13240 Dec 13 22:54 /var/mail/pat
|
|
||||||
-rw-rw---- 1 pat mail 9442 Dec 13 22:54 /home/pat/accumulated-mail
|
|
||||||
-rw-rw---- 1 bob mail 12522 Dec 13 22:54 /var/mail/bob
|
|
||||||
```
|
|
||||||
|
|
||||||
Some points to note:
|
|
||||||
|
|
||||||
* There is a user 'mail' with group name 'mail' that is used by sendmail.
|
|
||||||
* User mail is stored by sendmail in /var/mail which is also the home directory of user 'mail'. The inbox for user 'ian' defaults to /var/mail/ian.
|
|
||||||
* If you want sendmail to write files in a user directory, the file must be writeable by sendmail. Rather than making it world writeable, it is customary to make it group writeable and make the group 'mail'. You may need a system administrator to do this for you.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Using a .forward file to forward mail
|
|
||||||
|
|
||||||
The aliases file must be managed by a system administrator. Individual users can enable forwarding of their own mail using a .forward file in their own home directory. You can put anything in your .forward file that is allowed on the right side of the aliases file. The file contains plain text and does not need to be compiled. When mail is destined for you, sendmail checks for a .forward file in your home directory and processes the entries the same way it processes aliases.
|
|
||||||
|
|
||||||
## Mail queues and the mailq command
|
|
||||||
|
|
||||||
Linux mail handling uses a store-and-forward model. You have already seen that your incoming mail is stored in a file in /var/mail until you read it. Outgoing mail is also stored until a receiving server connection is available. You use the `mailq` command to see what mail is queued. Listing 11 shows an example of mail being sent to an external user, ian@attic4-c6, and the result of running the `mailq` command. In this case, there is currently no active link to attic4-c6, so the mail will remain queued until a link becomes active.
|
|
||||||
|
|
||||||
Listing 11. Using the `mailq` command
|
|
||||||
```
|
|
||||||
ian@attic4-u14:~$ mail -t ian@attic4-c6 -s "External mail" <<< "Testing external mail queues"
|
|
||||||
ian@attic4-u14:~$ mailq
|
|
||||||
MSP Queue status...
|
|
||||||
/var/spool/mqueue-client is empty
|
|
||||||
Total requests: 0
|
|
||||||
MTA Queue status...
|
|
||||||
/var/spool/mqueue (1 request)
|
|
||||||
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
|
|
||||||
vBE4mdE7025908* 29 Wed Dec 13 23:48 <ian@attic4-u14.hopto.org>
|
|
||||||
<ian@attic4-c6.hopto.org>
|
|
||||||
Total requests: 1
|
|
||||||
```
|
|
||||||
|
|
||||||
## Other mail transfer agents
|
|
||||||
|
|
||||||
In response to security issues with sendmail, several other mail transfer agents were developed during the 1990's. Postfix is perhaps the most popular, but qmail and exim are also widely used.
|
|
||||||
|
|
||||||
Postfix started life at IBM research as an alternative to sendmail. It attempts to be fast, easy to administer, and secure. The outside looks somewhat like sendmail, but the inside is completely different.
|
|
||||||
|
|
||||||
Qmail is a secure, reliable, efficient, simple message transfer agent developerd by Dan Bernstein. However, the core qmail package has not been updated for many years. Qmail and several other packages have now been collected into IndiMail.
|
|
||||||
|
|
||||||
Exim is another MTA developed at the University of Cambridge. Originally, the name stood for EXperimental Internet Mailer.
|
|
||||||
|
|
||||||
All of these MTAs were designed as sendmail replacements, so they all have some form of sendmail compatibility. Each can handle aliases and .forward files. Some provide a `sendmail` command as a front end to the particular MTA's own command. Most allow the usual sendmail options, although some options might be ignore silently. The `mailq` command is supported directly or by an alternate command with a similar function. For example, you can use `mailq` or `exim -bp` to display the exim mail queue. Needless to say, output can look different compared to that produced by sendmail's `mailq` command.
|
|
||||||
|
|
||||||
See Related topics where you can find more information on all of these MTAs.
|
|
||||||
|
|
||||||
This concludes your introduction to mail transfer agents on Linux.
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://www.ibm.com/developerworks/library/l-lpic1-108-3/index.html
|
|
||||||
|
|
||||||
作者:[Ian Shields][a]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]:https://www.ibm.com
|
|
||||||
[1]:http://www.lpi.org
|
|
||||||
[2]:https://www.ibm.com/developerworks/library/l-lpic1-map/
|
|
267
translated/tech/20171221 Mail transfer agent (MTA) basics.md
Normal file
267
translated/tech/20171221 Mail transfer agent (MTA) basics.md
Normal file
@ -0,0 +1,267 @@
|
|||||||
|
邮件传输代理(MTA)基础
|
||||||
|
======
|
||||||
|
|
||||||
|
## 概述
|
||||||
|
|
||||||
|
本教程中,你将学习:
|
||||||
|
|
||||||
|
* 使用 `mail` 命令。
|
||||||
|
* 创建邮件别名。
|
||||||
|
* 配置电子邮件转发。
|
||||||
|
* 了解常见邮件传输代理(MTA),比如,postfix、sendmail、qmail、以及 exim。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 控制邮件去向
|
||||||
|
|
||||||
|
Linux 系统上的电子邮件是使用 MTAs 投递的。你的 MTA 投递邮件到你的系统上的其他用户,以及系统上的其它系统组或者与全世界的其它 MTAs 通讯以投递邮件。
|
||||||
|
|
||||||
|
### 前提条件
|
||||||
|
|
||||||
|
为完成本系列教程的大部分内容,你需要具备 Linux 的基础知识,你需要拥有一个 Linux 系统来实践本教程中的命令。你应该熟悉 GNU 以及 UNIX 命令。有时候不同版本的程序的输出格式可能不同,因此,在你的系统中输出的结果可能与我在下面列出的稍有不同。
|
||||||
|
|
||||||
|
在本教程中,我使用的是 Ubuntu 14.04 LTS 和 sendmail 8.14.4 来做的演示。
|
||||||
|
|
||||||
|
## 邮件传输
|
||||||
|
|
||||||
|
邮件传输代理(比如 sendmail)在用户和系统之间投递邮件。大量的因特网邮件使用简单邮件传输协议(SMTP),但是本地邮件可能是通过文件或者套接字等其它可能的方式来传输的。邮件是一种存储和转发的操作,因此,在用户接收邮件或者接收系统或者通讯联系可用之前,邮件一直是存储在某种文件或者数据库中。配置和确保 MTA 的安全是非常复杂的任务,它们中的大部分内容都已经超出了本教程的范围。
|
||||||
|
|
||||||
|
## mail 命令
|
||||||
|
|
||||||
|
如果你使用 SMTP 协议传输电子邮件,你或许知道你可以使用的许多邮件客户端,包括 `mail`、`mutt`、`alpine`、`notmuch`、以及其它基于主机控制台或者图形界面的邮件客户端。`mail` 命令是最老的、可用于脚本中的、发送和接收以及管理收到的邮件的备用命令。
|
||||||
|
|
||||||
|
你可以使用 `mail` 命令交互式的向列表中的收件人发送信息,或者不使用参数去查看你收到的邮件。Listing 1 展示了如何在你的系统上去发送信息到用户 steve 和 pat,同时抄送拷贝给用户 bob。当提示 cc:和 subject:时,输入相应的抄送用户以及邮件主题,接着输入邮件正文,输入完成后按下 **Ctrl+D** (按下 Ctrl 键并保持再按下 D 之后全部松开)。
|
||||||
|
|
||||||
|
##### Listing 1. 使用 `mail` 交互式发送邮件
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ mail steve,pat
|
||||||
|
Cc: bob
|
||||||
|
Subject: Test message 1
|
||||||
|
This is a test message
|
||||||
|
|
||||||
|
Ian
|
||||||
|
```
|
||||||
|
|
||||||
|
如果一切顺利,你的邮件已经发出。如果在这里发生错误,你将看到错误信息。例如,如果你在接收者列表中输入一个无效的用户名,邮件将无法发送。注意在本示例中,所有的用户都在本地系统上存在,因此他们都是有效用户。
|
||||||
|
|
||||||
|
你也可以使用命令行以非交互式发送邮件。Listing 2 展示了如何给用户 steve 和 pat 发送一封邮件。这种方式可以用在脚本中。在不同的包中 `mail` 命令的版本不同。对于抄送(cc:)有些支持一个 `-c` 选项,但是我使用的这个版本不支持这个选项,因此,我仅将邮件发送到收件人。
|
||||||
|
|
||||||
|
Listing 2. 使用 `mail` 命令非交互式发送邮件
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ mail -t steve,pat -s "Test message 2" <<< "Another test.\n\nIan"
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你使用没有选项的 `mail` 命令,你将看到一个如 Listing 3 中所展示的那样一个收到的邮件的列表。你将看到用户 steve 有我上面发送的两个信息,再加上我以前发送的一个信息和后来用户 bob 发送的信息。所有的邮件都用 'N' 标记为新邮件。
|
||||||
|
|
||||||
|
Listing 3. 使用 `mail` 查看收到的邮件
|
||||||
|
```
|
||||||
|
steve@attic4-u14:~$ mail
|
||||||
|
"/var/mail/steve": 4 messages 4 new
|
||||||
|
>N 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
||||||
|
N 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
||||||
|
N 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
||||||
|
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
||||||
|
?
|
||||||
|
```
|
||||||
|
|
||||||
|
当前选中的信息使用一个 '>' 来标识,它是 Listing 3 中的第一封邮件。如果你按下 **回车键(Enter)**,将显示下一封未读邮件的第一页。按下 **空格楗(Space bar)** 将显示这个信息的下一页。当你读完这个信息并想返回到 '?' 提示时,按下 **回车键** 再次查看下一封邮件,依次类推。在任何 '?' 提示符下,你可以输入 'h' 再次去查看邮件头。你看过的邮件前面将显示一个 'R' 状态,如 Listing 4 所示。
|
||||||
|
|
||||||
|
Listing 4. 使用 'h' 去显示邮件头
|
||||||
|
```
|
||||||
|
? h
|
||||||
|
R 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
||||||
|
R 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
||||||
|
>R 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
||||||
|
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
||||||
|
?
|
||||||
|
```
|
||||||
|
|
||||||
|
在这个图中,Steve 已经读了三个信息,但是没有读来自 bob 的信息。你可以通过数字来选择单个的信息,你也可以通过输入 ‘d' 删除你不想要的信息,或者输入 '3d' 去删除三封信息。如果你输入 'q' 你将退出 `mail` 命令。已读的信息将被转移到你的 home 目录下的 mbox 文件中,而未读的信息仍然保留在你的收件箱中,默认在 /var/mail/$(id -un)。如 Listing 5 所示。
|
||||||
|
|
||||||
|
Listing 5. 使用 'q' 退出 `mail`
|
||||||
|
```
|
||||||
|
? h
|
||||||
|
R 1 Ian Shields Tue Dec 12 21:03 16/704 test message
|
||||||
|
R 2 Ian Shields Tue Dec 12 21:04 18/701 Test message 1
|
||||||
|
>R 3 Ian Shields Tue Dec 12 21:23 15/661 Test message 2
|
||||||
|
N 4 Bob C Tue Dec 12 21:45 17/653 How about lunch tomorrow?
|
||||||
|
? q
|
||||||
|
Saved 3 messages in /home/steve/mbox
|
||||||
|
Held 1 message in /var/mail/steve
|
||||||
|
You have mail in /var/mail/steve
|
||||||
|
```
|
||||||
|
|
||||||
|
如果你输入 'x' 而不是使用 'q' 去退出,你的邮箱在退出后将不保留你做的改变。因为这在 /var 文件系统中,你的系统管理员可能仅允许邮件在一个有限的时间范围内去保留三封邮件。去重读或者以其它方式再次处理保存在你的本地邮箱中的邮件,你可以使用 `-f` 选项去指定想要去读的文件。比如,`mail -f mbox`。
|
||||||
|
|
||||||
|
## 邮件别名
|
||||||
|
|
||||||
|
在前面的节中,看了如何在系统上给许多用户发送邮件。你可以使用一个全限定名字(比如 ian@myexampledomain.com)给其它系统上的用户发送邮件。
|
||||||
|
|
||||||
|
有时候你可能希望用户的所有邮件都可以发送到其它地方。比如,你有一个服务器群,你希望所有的 root 用户的邮件都发给核心系统管理员。或者你可能希望去创建一个邮件列表,将邮件发送给一些人。为实现上述目标,你可以使用别名,别名允许你为一个给定的用户名定义一个或者多个目的地。这个目的地或者是其它用户的邮箱、文件、管道、或者是某个进一步处理的命令。你可以在 /etc/mail/aliases 或者 /etc/aliases 中创建别名来实现上述目的。根据你的系统的不同,你可以找到上述其中一个,符号链接到它们、或者其中之一。改变别名文件你需要有 root 权限。
|
||||||
|
|
||||||
|
别名的格式一般是:
|
||||||
|
name: addr_1, addr_2, addr_3, ...
|
||||||
|
name 的位置是一个本地用户名字到别名,或者一个别名和 addr_1,addr_2,... 一个或多个别名。别名可以是一个本地用户,一个本地文件名,另一个别名,一个命令,一个包含文件,或者一个外部地址。
|
||||||
|
|
||||||
|
因此,发送邮件时如何区分别名呢(addr-N 值)?
|
||||||
|
|
||||||
|
* 一个本地用户名是你机器上系统中的一个用户名字。从技术角度来说,它可以通过调用 `getpwnam` 命令找到它。
|
||||||
|
* 一个本地文件名是以 '/' 开始的完全路径和文件名。它必须通过 `sendmail` 来写。信息是追加到这个文件上的。
|
||||||
|
* 一个命令是以一个管道符号开始的(|)。信息是通过标准输入的方式发送到命令的。
|
||||||
|
* 一个包含文件别名是以 `:include:` 和指定的一个路径和文件名开始的。文件中的别名被添加到别名中。
|
||||||
|
* 一个外部地址是一个电子邮件地址,比如 john@somewhere.com。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
你可以在你的系统中找到一个示例文件,它是与你的 sendmail 包一起安装的,它的位置在 /usr/share/sendmail/examples/db/aliases。它包含一些给 postmaster、MAILER-DAEMON、abuse、和 spam的建议别名。在 Listing 6,我把我的 Ubuntu 14.04 LTS 系统上的一些示例文件,和人为的示例结合起来说明一些可能的情况。
|
||||||
|
|
||||||
|
Listing 6. 人为的 /etc/mail/aliases 示例
|
||||||
|
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ cat /etc/mail/aliases
|
||||||
|
# First include some default system aliases from
|
||||||
|
# /usr/share/sendmail/examples/db/aliases
|
||||||
|
|
||||||
|
#
|
||||||
|
# Mail aliases for sendmail
|
||||||
|
#
|
||||||
|
# You must run newaliases(1) after making changes to this file.
|
||||||
|
#
|
||||||
|
|
||||||
|
# Required aliases
|
||||||
|
postmaster: root
|
||||||
|
MAILER-DAEMON: postmaster
|
||||||
|
|
||||||
|
# Common aliases
|
||||||
|
abuse: postmaster
|
||||||
|
spam: postmaster
|
||||||
|
|
||||||
|
# Other aliases
|
||||||
|
|
||||||
|
# Send steve's mail to bob and pat instead
|
||||||
|
steve: bob,pat
|
||||||
|
|
||||||
|
# Send pat's mail to a file in her home directory and also to her inbox.
|
||||||
|
# Finally send it to a command that will make another copy.
|
||||||
|
pat: /home/pat/accumulated-mail,
|
||||||
|
\pat,
|
||||||
|
|/home/pat/makemailcopy.sh
|
||||||
|
|
||||||
|
# Mailing list for system administrators
|
||||||
|
sysadmins: :include: /etc/aliases-sysadmins
|
||||||
|
```
|
||||||
|
|
||||||
|
注意那个 pat 既是一个别名也是一个系统中的用户。别名是以递归的方式展开的,因此,如果一个别名也是一个名字,那么它将被展开。Sendmail 并不会给同一个用户发送相同的邮件两遍,因此,如果你正好将 'pat' 作为 'pat' 的别名,那么 sendmail 在已经找到并处理完用户 ’pat‘ 之后,将忽略别名 'pat’。为避免这种问题,你可以在别名前使用一个'\' 做为前缀去指示它是一个不要进一步引起混淆的名字。在这种情况下,pat 的邮件除了文件和命令之外,其余的可能会被发送到他的正常的邮箱中。
|
||||||
|
|
||||||
|
在 aliases 中以 '$' 开始的行是注释,它会被忽略。以空白开始的行会以延续行来处理。
|
||||||
|
|
||||||
|
Listing 7 展示了包含文件 /etc/aliases-sysadmins。
|
||||||
|
|
||||||
|
Listing 7 包含文件 /etc/aliases-sysadmins
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ cat /etc/aliases-sysadmins
|
||||||
|
|
||||||
|
# Mailing list for system administrators
|
||||||
|
bob,pat
|
||||||
|
```
|
||||||
|
|
||||||
|
## newaliases 命令
|
||||||
|
|
||||||
|
sendmail 使用的主要配置文件被编译成数据库文件。邮件别名也是如此。你可以使用 `newaliases` 命令去编译你的 /etc/mail/aliases 和任何包含文件到 /etc/mail/aliases.db 中。注意,那个 `newaliases` 命令等价于 `sendmail -bi`。Listing 8 展示了一个示例。
|
||||||
|
|
||||||
|
Listing 8. 为邮件别名重建数据库
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ sudo newaliases
|
||||||
|
/etc/mail/aliases: 7 aliases, longest 62 bytes, 184 bytes total
|
||||||
|
ian@attic4-u14:~$ ls -l /etc/mail/aliases*
|
||||||
|
lrwxrwxrwx 1 root smmsp 10 Dec 8 15:48 /etc/mail/aliases -> ../aliases
|
||||||
|
-rw-r----- 1 smmta smmsp 12288 Dec 13 23:18 /etc/mail/aliases.db
|
||||||
|
```
|
||||||
|
|
||||||
|
## 使用别名的示例
|
||||||
|
|
||||||
|
Listing 9 展示了一个简单的 shell 脚本,它在我的别名示例中以一个命令的方式来使用。
|
||||||
|
|
||||||
|
Listing 9. makemailcopy.sh 脚本
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ cat ~pat/makemailcopy.sh
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Note: Target file ~/mail-copy must be writeable by sendmail!
|
||||||
|
cat >> ~pat/mail-copy
|
||||||
|
```
|
||||||
|
|
||||||
|
Listing 10 展示了用于测试时更新的文件。
|
||||||
|
|
||||||
|
Listing 10. /etc/aliases-sysadmins 包含文件
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ date
|
||||||
|
Wed Dec 13 22:54:22 EST 2017
|
||||||
|
ian@attic4-u14:~$ mail -t sysadmins -s "sysadmin test 1" <<< "Testing mail"
|
||||||
|
ian@attic4-u14:~$ ls -lrt $(find /var/mail ~pat -type f -mmin -3 2>/dev/null )
|
||||||
|
-rw-rw---- 1 pat mail 2046 Dec 13 22:54 /home/pat/mail-copy
|
||||||
|
-rw------- 1 pat mail 13240 Dec 13 22:54 /var/mail/pat
|
||||||
|
-rw-rw---- 1 pat mail 9442 Dec 13 22:54 /home/pat/accumulated-mail
|
||||||
|
-rw-rw---- 1 bob mail 12522 Dec 13 22:54 /var/mail/bob
|
||||||
|
```
|
||||||
|
|
||||||
|
需要注意的几点:
|
||||||
|
|
||||||
|
* 有一个用户 'mail' 与 sendmail 使用的组名字 'mail' 是一样的。
|
||||||
|
* sendmail 在 /var/mail 保存用户邮件,它也在用户 ‘mail' 的 home 目录下。用户 'ian' 的默认收件箱在 /var/mail/ian 中。
|
||||||
|
* 如果你希望 sendmail 在用户目录下写入文件,这个文件必须允许 sendmail 可写入。与其让任何人都可以写入,还不如定义一个组可写入,组名称为 'mail'。这需要系统管理员来帮你完成。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## 使用一个 `.forward` 文件去转发邮件
|
||||||
|
|
||||||
|
别名文件是由系统管理员来管理的。个人用户可以使用它们自己的 home 目录下的 `.forward` 文件去转发他们自己的邮件。你可以在你的 `.forward` 文件中放任何东西,它可以放在别名文件的右侧。这个文件的内容是明文的,不需要编译。当你收到邮件时,sendmail 将检查你的 home 目录中的 `.forward` 文件,然后就像处理别名一样处理它。
|
||||||
|
|
||||||
|
## 邮件队列和 mailq 命令
|
||||||
|
|
||||||
|
Linux 邮件使用存储-转发的处理模式。你已经看到的已接收邮件,在你读它之前一直保存在文件 /var/mail 中。你发出的邮件在接收服务器连接可用之前也会被保存。你可以使用 `mailq` 命令去查看邮件队列。Listing 11 展示了一个发送给外部用户 ian@attic4-c6 的一个邮件示例,以及运行 `mailq` 命令的结果。在这个案例中,当前服务器没有连接到 attic4-c6,因此邮件在与对方服务器连接可用之前一直保存在队列中。
|
||||||
|
|
||||||
|
Listing 11. 使用 `mailq` 命令
|
||||||
|
```
|
||||||
|
ian@attic4-u14:~$ mail -t ian@attic4-c6 -s "External mail" <<< "Testing external mail queues"
|
||||||
|
ian@attic4-u14:~$ mailq
|
||||||
|
MSP Queue status...
|
||||||
|
/var/spool/mqueue-client is empty
|
||||||
|
Total requests: 0
|
||||||
|
MTA Queue status...
|
||||||
|
/var/spool/mqueue (1 request)
|
||||||
|
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
|
||||||
|
vBE4mdE7025908* 29 Wed Dec 13 23:48 <ian@attic4-u14.hopto.org>
|
||||||
|
<ian@attic4-c6.hopto.org>
|
||||||
|
Total requests: 1
|
||||||
|
```
|
||||||
|
|
||||||
|
## 其它邮件传输代理
|
||||||
|
|
||||||
|
为解决使用 sendmail 时安全方面的问题,在 上世纪九十年代开发了几个其它的邮件传输代理。Postfix 或许是最流行的一个,但是 qmail 和 exim 也大量使用。
|
||||||
|
|
||||||
|
Postfix 是 IBM 为代替 sendmail 而研发的。它更快、也易于管理、安全性更好一些。从外表看它非常像 sendmail,但是它的内部完全与 sendmail 不同。
|
||||||
|
|
||||||
|
Qmail 是一个安全、可靠、高效、简单的邮件传输代理,它由 Dan Bernstein 开发。但是,最近几年以来,它的核心包已经不再更新了。Qmail 和几个其它的包已经被吸收到 IndiMail 中了。
|
||||||
|
|
||||||
|
Exim 是另外一个 MTA,它由 University of Cambridge 开发。最初,它的名字是 `EXperimental Internet Mailer`。
|
||||||
|
|
||||||
|
所有的这些 MTAs 都是为代替 sendmail 而设计的,因此,它们它们都兼容 sendmail 的一些格式。它们都能够处理别名和 `.forward` 文件。有些规定了一个 `sendmail` 命令作为一个前端到特定的 MTA 的自有命令。尽管一些选项可能会被静默忽略,但是大多数都允许使用常见的 sendmail 选项。`mailq` 命令是被直接支持的,或者使用一个类似功能的命令来代替。比如,你可以使用 `mailq` 或者 `exim -bp` 去显示 exim 邮件队列。当然,输出可以看到与 sendmail 的 `mailq` 命令的不同之外。
|
||||||
|
|
||||||
|
查看相关的主题,你可以找到更多的关于这些 MTA 的更多信息。
|
||||||
|
|
||||||
|
对 Linux 上的邮件传输代理的介绍到此结束。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://www.ibm.com/developerworks/library/l-lpic1-108-3/index.html
|
||||||
|
|
||||||
|
作者:[Ian Shields][a]
|
||||||
|
译者:[qhwdw](https://github.com/qhwdw)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]:https://www.ibm.com
|
||||||
|
[1]:http://www.lpi.org
|
||||||
|
[2]:https://www.ibm.com/developerworks/library/l-lpic1-map/
|
Loading…
Reference in New Issue
Block a user