linux命令sendmail
-
sendmail是一种在Linux操作系统中用于发送电子邮件的命令。它是一种强大而灵活的工具,可以让用户通过命令行发送电子邮件,无论是通过终端还是通过脚本。sendmail命令可以方便地与其他脚本和程序进行集成,从而实现自动化的邮件通知和报告功能。
使用sendmail命令发送邮件非常简单。以下是一些常用的sendmail命令及其选项:
1. 基本语法:
sendmail [选项] [收件人邮件地址]2. 选项:
-f <发件人邮件地址>:指定发件人的电子邮件地址。
-s <邮件主题>:指定邮件主题。
-i:忽略输入中的点号,使得发送的邮件内容可以包含点号。
-t:从标准输入中读取收件人列表和邮件内容。3. 示例:
a. 发送邮件到单个收件人:
sendmail user@example.com < mail.txt b. 发送邮件到多个收件人: sendmail user1@example.com,user2@example.com < mail.txt c. 指定发件人地址和主题: sendmail -f me@example.com -s “Test email” user@example.com < mail.txtsendmail命令还支持更多高级的选项和功能,可以用于设置邮件优先级、附件、抄送和密送等。这使得sendmail成为一个非常强大的工具,适用于各种邮件发送需求。除了sendmail命令,Linux系统还提供了其他一些用于发送邮件的命令和工具,如mail、mutt等。这些命令和工具各有特点,可以根据实际需求选择适合的工具。sendmail命令由于其灵活性和强大功能而被广泛使用,尤其适用于自动化任务和脚本编程。2年前 -
1. `sendmail`是Linux系统中的一个常用命令,用于发送电子邮件。它是一个灵活且强大的邮件传输代理(MTA),可以将电子邮件从一个系统发送到另一个系统。
2. `sendmail`命令的基本语法是`sendmail [options] [recipient]`。其中,`options`是可选的参数,用于指定邮件的各种属性,如发件人、主题、附件等。`recipient`是邮件的收件人,可以是单个邮箱地址或多个邮箱地址的列表。
3. 若要使用`sendmail`发送邮件,首先需要在系统上安装和配置sendmail软件包。在大多数Linux发行版上,sendmail默认是预装的。一旦安装完成,就可以使用`sendmail`命令来发送邮件了。
4. `sendmail`可以通过管道(`pipe`)或重定向(`redirect`)来读取邮件的内容。例如,可以使用`echo`命令生成邮件内容,并将其通过管道传递给`sendmail`命令,如`echo “This is the body of the email” | sendmail recipient@example.com`。
5. `sendmail`还支持通过命令行参数指定邮件的各种属性,如发件人、主题、附件等。例如,使用`-f`参数可以指定发件人,使用`-s`参数可以指定主题。例如,`sendmail -f sender@example.com -s “Hello” recipient@example.com`。
2年前 -
简介:
sendmail是一款用于在Linux系统中发送邮件的命令行工具,它具备非常强大和灵活的功能,可用于发送电子邮件,包括文本邮件、HTML邮件和附件邮件等。本文将介绍sendmail的安装、配置和常用操作。目录:
1. 安装sendmail
2. 配置sendmail
3. 发送文本邮件
4. 发送带附件的邮件
5. 发送HTML邮件
6. 发送HTML模板邮件
7. 发送带图片的HTML邮件
8. 发送带抄送和密送的邮件
9. 发送有格式的邮件
10. 发送邮件到多个收件人
11. 添加邮件主题和正文
12. 发送带优先级的邮件
13. 发送邮件并保存副本
14. 发送HTML邮件并保存副本
15. 发送邮件并设置回执
16. 发送邮件并设置回执接收者
17. 发送邮件并设置回执事项
18. 发送邮件并设置回执日志文件
19. 发送HTML模板邮件并设置回执
20. 发送HTML模板邮件并设置回执接收者
21. 发送HTML模板邮件并设置回执事项
22. 发送HTML模板邮件并设置回执日志文件1. 安装sendmail
在大多数Linux发行版中,sendmail默认已经安装。如果没有安装,可以使用以下命令进行安装:
“`shell
sudo apt-get install sendmail
“`
2. 配置sendmail
sendmail配置文件位于/etc/mail/sendmail.cf。通过编辑该文件,我们可以设置多种sendmail的参数和选项。配置sendmail需要一定的Linux基础知识,以下是一些常见的配置选项:
– DaemonPortOptions选项:用于指定sendmail守护进程的监听端口和参数。默认情况下,sendmail监听端口为25,通过TCP/IP协议接受邮件。
– QueueDirectory选项:用于指定sendmail邮件队列的存储目录。这个目录将存储待发送的邮件。
– SendmailPidFile选项:用于指定sendmail守护进程的进程ID文件的路径和文件名。
– SmtpGreetingMessage选项:用于指定sendmail服务器的欢迎消息。
…
以上只是一些常见的配置选项,配置sendmail还有很多其他选项,需要根据具体需求进行设置。3. 发送文本邮件
使用sendmail发送文本邮件非常简单,只需在命令行中输入类似下面的命令:
“`shell
echo “This is the body of the email” | sendmail recipient@example.com
“`
其中,echo “This is the body of the email”表示邮件的正文内容,recipient@example.com表示邮件的接收者。4. 发送带附件的邮件
要发送带附件的邮件,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: multipart/mixed; boundary=”BOUNDARY”‘;
echo “”;
echo “–BOUNDARY”;
echo “Content-Type: text/plain”;
echo “”;
echo “This is the body of the email”;
echo “–BOUNDARY”;
echo ‘Content-Type: application/pdf; name=”attachment.pdf”‘;
echo ‘Content-Disposition: attachment; filename=”attachment.pdf”‘;
echo “”;
echo “Attachment content”;
echo “–BOUNDARY–“) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,构建了带有附件的邮件。需要注意的是,附件的内容需要根据具体类型进行编码。5. 发送HTML邮件
要发送HTML邮件,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: text/html’;
echo “”;
echo “This is the body of the email
“) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,构建了带有HTML内容的邮件。6. 发送HTML模板邮件
使用sendmail发送HTML模板邮件可以更加灵活。首先,我们需要创建一个HTML模板文件,其中包含邮件的整体结构和变量占位符。例如,创建一个名为template.html的文件,内容如下:
“`htmlHello, {{name}}!
This is the body of the email.
“`
然后,我们可以使用以下命令来发送带有HTML模板的邮件:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: text/html’;
echo “”;
echo “$EMAIL”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了将HTML模板中的占位符替换为具体内容并发送的功能。7. 发送带图片的HTML邮件
要发送带有图片的HTML邮件,首先需要将图片嵌入到邮件中。可以使用以下命令来发送带有图片的HTML邮件:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: multipart/related; boundary=”BOUNDARY”‘;
echo “”;
echo “–BOUNDARY”;
echo “Content-Type: text/html”;
echo “”;
echo “$EMAIL”;
echo “–BOUNDARY”;
echo ‘Content-Type: image/png; name=”image.png”‘;
echo ‘Content-Transfer-Encoding: base64’;
echo ‘Content-Disposition: inline; filename=”image.png”‘;
echo “”;
echo “$(base64 image.png)”;
echo “–BOUNDARY–“) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了将HTML模板中的占位符替换为具体内容,并将图片嵌入到HTML中的功能。8. 发送带抄送和密送的邮件
sendmail也可以发送带有抄送和密送的邮件。使用以下命令可以发送带有抄送和密送的邮件:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Cc: cc@example.com“;
echo “Bcc: bcc@example.com“;
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送带有抄送和密送的邮件。9. 发送有格式的邮件
sendmail支持发送带有格式的邮件,可以使用以下命令发送有格式的邮件:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: text/plain; format=flowed’;
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送有格式的邮件。10. 发送邮件到多个收件人
sendmail可以发送邮件到多个收件人,只需在命令行中使用逗号分隔多个收件人,例如:
“`shell
(echo “To: recipient1@example.com, recipient2@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: text/plain’;
echo “”;
echo “This is the body of the email”) | sendmail recipient1@example.com,recipient2@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件到多个收件人的功能。11. 添加邮件主题和正文
发送邮件时,可以使用echo命令将主题和正文添加到邮件中。以下是一个例子:
“`shell
(echo -e “To: recipient@example.com\nSubject: This is the subject of the email\n”; cat body.txt) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了添加邮件主题和正文的功能。12. 发送带优先级的邮件
sendmail可以发送带有优先级的邮件。使用以下命令可以发送带有优先级的邮件:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Priority: urgent”;
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送带有优先级的邮件。13. 发送邮件并保存副本
要发送邮件并在本地保存副本,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Save-Copy-To: /path/to/copy.txt”;
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件并保存副本的功能。14. 发送HTML邮件并保存副本
要发送HTML邮件并在本地保存副本,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo ‘Content-Type: text/html’;
echo “Save-Copy-To: /path/to/copy.html”;
echo “”;
echo “This is the body of the email
“) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送HTML邮件并保存副本的功能。15. 发送邮件并设置回执
sendmail可以设置邮件的回执。使用以下命令可以发送邮件并设置回执:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Disposition-Notification-To: sender@example.com“;
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件并设置回执的功能。16. 发送邮件并设置回执接收者
要发送邮件并设置回执接收者,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Disposition-Notification-Options: X-Delivery-time, X-Display-receipt”);
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件并设置回执接收者的功能。17. 发送邮件并设置回执事项
要发送邮件并设置回执事项,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Disposition-Notification-Options: X-Delivery-time, X-Display-receipt”);
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件并设置回执事项的功能。18. 发送邮件并设置回执日志文件
要发送邮件并设置回执日志文件,可以使用以下命令:
“`shell
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Return-Receipt-To: log@example.com“);
echo “”;
echo “This is the body of the email”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送邮件并设置回执日志文件的功能。19. 发送HTML模板邮件并设置回执
要发送HTML模板邮件并设置回执,可以使用以下命令:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo “Disposition-Notification-To: sender@example.com“;
echo “”;
echo “$EMAIL”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送HTML模板邮件并设置回执的功能。20. 发送HTML模板邮件并设置回执接收者
要发送HTML模板邮件并设置回执接收者,可以使用以下命令:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Disposition-Notification-Options: X-Delivery-time, X-Display-receipt”);
echo “”;
echo “$EMAIL”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送HTML模板邮件并设置回执接收者的功能。21. 发送HTML模板邮件并设置回执事项
要发送HTML模板邮件并设置回执事项,可以使用以下命令:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Disposition-Notification-Options: X-Delivery-time, X-Display-receipt”);
echo “”;
echo “$EMAIL”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送HTML模板邮件并设置回执事项的功能。22. 发送HTML模板邮件并设置回执日志文件
要发送HTML模板邮件并设置回执日志文件,可以使用以下命令:
“`shell
TEMPLATE=$(cat template.html)
EMAIL=$(echo “$TEMPLATE” | sed “s/{{name}}/John Doe/g”)
(echo “To: recipient@example.com“;
echo “Subject: This is the subject of the email”;
echo “MIME-Version: 1.0”;
echo “Disposition-Notification-To: sender@example.com“;
echo “Return-Receipt-To: log@example.com“);
echo “”;
echo “$EMAIL”) | sendmail recipient@example.com
“`
以上命令通过管道将多行文本输入sendmail命令,实现了发送HTML模板邮件并设置回执日志文件的功能。总结:
sendmail是一款功能强大的命令行工具,可以在Linux系统中发送各种类型的邮件。通过逐步介绍的发送文本邮件、发送带附件的邮件、发送HTML邮件、发送HTML模板邮件以及发送带抄送、密送等特殊邮件,我们了解了sendmail的基本用法和一些高级功能。为了更好地使用sendmail,建议深入学习sendmail的配置和更多选项,以满足特定需求,并提高邮件的安全性和可靠性。2年前