Linux学习–>如何通过Shell脚本实现发送邮件通知功能?

Linux学习–>如何通过Shell脚本实现发送邮件通知功能?1、安装和配置sendmail不需要注册公网域名和MX记录(不需要架设公网邮件服务器),通过Linux系统自带的mail命令即可对公网邮箱发送邮件。不过mail命令是依赖sendmail的,所以我们需要先检查安装和配置sendmail。一般系统都自带sendmail,但是只能给内网的邮箱发邮件。如果想给公网的邮箱发邮件(比如qq邮箱)就需要配置sendmail.这里记录sendmail的安装启动配置…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

#1、安装和配置sendmail
不需要注册公网域名和MX记录(不需要架设公网邮件服务器),通过Linux系统自带的mail命令即可对公网邮箱发送邮件。不过mail命令是依赖sendmail的,所以我们需要先检查安装和配置sendmail。

一般系统都自带sendmail,但是只能给内网的邮箱发邮件。如果想给公网的邮箱发邮件(比如qq邮箱)就需要配置sendmail.

这里记录sendmail的安装启动配置,各取所需。

尝试使用mail命令发送一封邮件。

root@ubuntu4146:/data/gitlabData/backups# mail -s "Hello from Linux Server by shell" ouyangpeng@oaserver.com
The program 'mail' is currently not installed. You can install it by typing:
apt-get install mailutils

发送失败,命令mail没有安装

因此,我们使用 apt-get install mailutils 命令安装mailutils


root@ubuntu4146:/data/gitlabData/backups# apt-get install mailutils
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  lib32asan0 lib32atomic1 lib32gcc-4.8-dev lib32gcc1 lib32gomp1 lib32itm1
  lib32quadmath0 lib32stdc++-4.8-dev lib32stdc++6 libbonobo2-common
  libc6-dev-x32 libc6-x32 libgnome2-common libltdl-dev liborbit-2-0 libssl-dev
  libssl-doc libstdc++-4.8-dev libx32asan0 libx32atomic1 libx32gcc-4.8-dev
  libx32gcc1 libx32gomp1 libx32itm1 libx32quadmath0 libx32stdc++-4.8-dev
  libx32stdc++6 pkg-php-tools shtool
Use 'apt-get autoremove' to remove them.
将会安装下列额外的软件包:
  guile-2.0-libs libgsasl7 libkyotocabinet16 liblzo2-2 libmailutils4 libntlm0
  mailutils-common
建议安装的软件包:
  mailutils-mh mailutils-doc
下列【新】软件包将被安装:
  guile-2.0-libs libgsasl7 libkyotocabinet16 liblzo2-2 libmailutils4 libntlm0
  mailutils mailutils-common
升级了 0 个软件包,新安装了 8 个软件包,要卸载 0 个软件包,有 310 个软件包未被升级。
需要下载 3,461 kB 的软件包。
解压缩后会消耗掉 18.0 MB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://cn.archive.ubuntu.com/ubuntu/ trusty-updates/main liblzo2-2 amd64 2.06-1.2ubuntu1.1 [46.1 kB]
获取:2 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libkyotocabinet16 amd64 1.2.76-4 [288 kB]
获取:3 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libntlm0 amd64 1.4-1 [15.3 kB]
获取:4 http://cn.archive.ubuntu.com/ubuntu/ trusty/main guile-2.0-libs amd64 2.0.9+1-1ubuntu1 [2,127 kB]
获取:5 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libgsasl7 amd64 1.8.0-2ubuntu2 [117 kB]
获取:6 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe mailutils-common all 1:2.99.98-1.1 [245 kB]
获取:7 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe libmailutils4 amd64 1:2.99.98-1.1 [398 kB]
获取:8 http://cn.archive.ubuntu.com/ubuntu/ trusty/universe mailutils amd64 1:2.99.98-1.1 [225 kB]
下载 3,461 kB,耗时 2秒 (1,283 kB/s)
Selecting previously unselected package liblzo2-2:amd64.
(正在读取数据库 ... 系统当前共安装有 172915 个文件和目录。)
Preparing to unpack .../liblzo2-2_2.06-1.2ubuntu1.1_amd64.deb ...
Unpacking liblzo2-2:amd64 (2.06-1.2ubuntu1.1) ...
Selecting previously unselected package libkyotocabinet16:amd64.
Preparing to unpack .../libkyotocabinet16_1.2.76-4_amd64.deb ...
Unpacking libkyotocabinet16:amd64 (1.2.76-4) ...
Selecting previously unselected package libntlm0:amd64.
Preparing to unpack .../libntlm0_1.4-1_amd64.deb ...
Unpacking libntlm0:amd64 (1.4-1) ...
Selecting previously unselected package guile-2.0-libs.
Preparing to unpack .../guile-2.0-libs_2.0.9+1-1ubuntu1_amd64.deb ...
Unpacking guile-2.0-libs (2.0.9+1-1ubuntu1) ...
Selecting previously unselected package libgsasl7.
Preparing to unpack .../libgsasl7_1.8.0-2ubuntu2_amd64.deb ...
Unpacking libgsasl7 (1.8.0-2ubuntu2) ...
Selecting previously unselected package mailutils-common.
Preparing to unpack .../mailutils-common_1%3a2.99.98-1.1_all.deb ...
Unpacking mailutils-common (1:2.99.98-1.1) ...
Selecting previously unselected package libmailutils4.
Preparing to unpack .../libmailutils4_1%3a2.99.98-1.1_amd64.deb ...
Unpacking libmailutils4 (1:2.99.98-1.1) ...
Selecting previously unselected package mailutils.
Preparing to unpack .../mailutils_1%3a2.99.98-1.1_amd64.deb ...
Unpacking mailutils (1:2.99.98-1.1) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
正在设置 liblzo2-2:amd64 (2.06-1.2ubuntu1.1) ...
正在设置 libkyotocabinet16:amd64 (1.2.76-4) ...
正在设置 libntlm0:amd64 (1.4-1) ...
正在设置 guile-2.0-libs (2.0.9+1-1ubuntu1) ...
正在设置 libgsasl7 (1.8.0-2ubuntu2) ...
正在设置 mailutils-common (1:2.99.98-1.1) ...
正在设置 libmailutils4 (1:2.99.98-1.1) ...
正在设置 mailutils (1:2.99.98-1.1) ...
update-alternatives: using /usr/bin/frm.mailutils to provide /usr/bin/frm (frm) in 自动模式
update-alternatives: using /usr/bin/from.mailutils to provide /usr/bin/from (from) in 自动模式
update-alternatives: using /usr/bin/messages.mailutils to provide /usr/bin/messages (messages) in 自动模式
update-alternatives: using /usr/bin/movemail.mailutils to provide /usr/bin/movemail (movemail) in 自动模式
update-alternatives: using /usr/bin/readmsg.mailutils to provide /usr/bin/readmsg (readmsg) in 自动模式
update-alternatives: using /usr/bin/dotlock.mailutils to provide /usr/bin/dotlock (dotlock) in 自动模式
update-alternatives: using /usr/bin/mail.mailutils to provide /usr/bin/mailx (mailx) in 自动模式
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
root@ubuntu4146:/data/gitlabData/backups# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Duxb3wDz-1660292705180)(https://img-blog.csdn.net/20170818095518434?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

#2、使用shell脚本发送Email

首先执行命令mail --help 查找mail命令的用法

root@ubuntu4146:/data/gitlabData/backups# mail --help
Usage: mail [OPTION...] [address...]
  or:  mail [OPTION...] -f [OPTION...] [file]
  or:  mail [OPTION...] --file [OPTION...] [file]
  or:  mail [OPTION...] --file=file [OPTION...]
GNU mail -- process mail messages.
If -f or --file is given, mail operates on the mailbox named by the first
argument, or the user's mbox, if no argument given.

  -a, --append=HEADER: VALUE append given header to the message being sent
  -A, --attach=FILE          attach FILE
      --content-type=TYPE    set content type for subsequent --attach options
  -e, --exist                return true if mail exists
      --encoding=NAME        set encoding for subsequent --attach options
  -E, --exec=COMMAND         execute COMMAND
  -F, --byname               save messages according to sender
  -H, --headers              write a header summary and exit
  -i, --ignore               ignore interrupts
  -n, --norc                 do not read the system mailrc file
  -N, --nosum                do not display initial header summary
  -p, --print, --read        print all mail to standard output
  -q, --quit                 cause interrupts to terminate program
  -r, --return-address=ADDRESS   use address as the return address when sending
                             mail
  -s, --subject=SUBJ         send a message with the given SUBJECT
  -t, --to                   precede message by a list of addresses
  -u, --user=USER            operate on USER's mailbox

 Common options
      --config-file=FILE, --rcfile=FILE
                             load this configuration file
      --config-help          show configuration file summary
      --config-lint, --rcfile-lint
                             check configuration file syntax and exit
      --config-verbose, --rcfile-verbose
                             verbosely log parsing of the configuration files
      --no-site-config, --no-site-rcfile
                             do not load site configuration file
      --no-user-config, --no-user-rcfile
                             do not load user configuration file
      --set=PARAM=VALUE      set configuration parameter
      --show-config-options  show compilation options


 Global debugging settings
      --debug-level=LEVEL    set Mailutils debugging level
      --debug-line-info      show source info with debugging messages

  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <bug-mailutils@gnu.org>.
root@ubuntu4146:/data/gitlabData/backups# 

2.1 无邮件正文

mail -s "主题"  收件地址

例如我们执行:

root@ubuntu4146:/data/gitlabData/backups# mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com
Cc: 
Null message body; hope that's ok
root@ubuntu4146:/data/gitlabData/backups# 

提示你输入Cc地址,即邮件抄送地址,没有抄送地址则直接回车就完成了邮件的发送。
接着会让你输入邮件正文,当邮件正文输入完成后,需要按CTRL+D结束输入。此时我们不填写邮件正文,直接按CTRL+D结束输入。它会提示我们Null message body; hope that’s ok

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-19Z8IXQw-1660292705183)(https://img-blog.csdn.net/20170818101012631?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

此时查看OA邮件,可以收到刚才发送过来的无正文的邮件。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YZq3XrIL-1660292705184)(https://img-blog.csdn.net/20170818101158316?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

2.2 有邮件正文

2.2.1 echo “邮件正文” | mail -s 邮件主题 收件地址

执行管道命令,来填写邮件正文,例如我们执行命令:

root@ubuntu4146:/# echo "Email Content:This is the content of mail. Welcome to ouyangpeng's blog : http://blog.csdn.net/ouyang_peng/" | mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com
root@ubuntu4146:/# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9erRCW1s-1660292705185)(https://img-blog.csdn.net/20170818101707428?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

这时,我们收到的OA邮件内容如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5xS2JoVq-1660292705186)(https://img-blog.csdn.net/20170818101811893?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

2.2.2 mail -s “主题” 收件地址< 文件(邮件正文)

例如我们执行如下命令,则将/data/gitlabData/backups/log/2017-08-18.log 文件内容当做邮件正文发送了。

root@ubuntu4146:/# mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com < /data/gitlabData/backups/log/2017-08-18.log 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jXwEhDW4-1660292705187)(https://img-blog.csdn.net/20170818102507625?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

这时,我们收到的OA邮件内容如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YlFGDIel-1660292705188)(https://img-blog.csdn.net/20170818102554337?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

2.2.3 cat 邮件正文.txt | mail -s 邮件主题 收件地址

例如我们执行如下命令,则将/data/gitlabData/backups/log/2017-08-17.log 文件内容当做邮件正文发送了。

root@ubuntu4146:/# cat /data/gitlabData/backups/log/2017-08-17.log | mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-INqWX0L3-1660292705189)(https://img-blog.csdn.net/20170818102907268?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

这时,我们收到的OA邮件内容如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a5q45zoO-1660292705190)(https://img-blog.csdn.net/20170818103009697?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

2.3 邮件附件

2.3.1 mail -s “主题” 收件地址 -A 附件

root@ubuntu4146:/# mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com -A /data/gitlabData/backups/log/2017-08-18.log 
Cc: 
This email attach a file.
root@ubuntu4146:/# 

提示你输入Cc地址,即邮件抄送地址,没有抄送地址则直接回车就完成了邮件的发送。
接着会让你输入邮件正文 “This email attach a file.”,当邮件正文输入完成后,需要按CTRL+D结束输入。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TOWGsz4Z-1660292705191)(https://img-blog.csdn.net/20170818103505870?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

这时,我们收到的OA邮件内容如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wmfso3qG-1660292705199)(https://img-blog.csdn.net/20170818103559698?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

2.3.2 mail -s “主题” 收件地址 -a 附件 < 文件(邮件正文.txt)

root@ubuntu4146:/# mail -s "Email Subject : Hello from Linux Server by shell" ouyangpeng@oaserver.dw.gdbbk.com -A /data/gitlabData/backups/auto_backup.sh < /data/gitlabData/backups/log/2017-08-18.log 

执行如上命令,/data/gitlabData/backups/auto_backup.sh 会当做附件发送,/data/gitlabData/backups/log/2017-08-18.log 的内容会当做正文发送

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zR1CiDvG-1660292705202)(https://img-blog.csdn.net/20170818103938136?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

这时,我们收到的OA邮件内容如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h76Wswvb-1660292705204)(https://img-blog.csdn.net/20170818104027617?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

3、发送给多个人

root@ubuntu4146:/data/gitlabData/backups# mail --help
Usage: mail [OPTION...] [address...]
  or:  mail [OPTION...] -f [OPTION...] [file]
  or:  mail [OPTION...] --file [OPTION...] [file]
  or:  mail [OPTION...] --file=file [OPTION...]
GNU mail -- process mail messages.
If -f or --file is given, mail operates on the mailbox named by the first
argument, or the user's mbox, if no argument given.

  -a, --append=HEADER: VALUE append given header to the message being sent
  -A, --attach=FILE          attach FILE
      --content-type=TYPE    set content type for subsequent --attach options
  -e, --exist                return true if mail exists
      --encoding=NAME        set encoding for subsequent --attach options
  -E, --exec=COMMAND         execute COMMAND
  -F, --byname               save messages according to sender
  -H, --headers              write a header summary and exit
  -i, --ignore               ignore interrupts
  -n, --norc                 do not read the system mailrc file
  -N, --nosum                do not display initial header summary
  -p, --print, --read        print all mail to standard output
  -q, --quit                 cause interrupts to terminate program
  -r, --return-address=ADDRESS   use address as the return address when sending
                             mail
  -s, --subject=SUBJ         send a message with the given SUBJECT
  -t, --to                   precede message by a list of addresses
  -u, --user=USER            operate on USER's mailbox

 Common options
      --config-file=FILE, --rcfile=FILE
                             load this configuration file
      --config-help          show configuration file summary
      --config-lint, --rcfile-lint
                             check configuration file syntax and exit
      --config-verbose, --rcfile-verbose
                             verbosely log parsing of the configuration files
      --no-site-config, --no-site-rcfile
                             do not load site configuration file
      --no-user-config, --no-user-rcfile
                             do not load user configuration file
      --set=PARAM=VALUE      set configuration parameter
      --show-config-options  show compilation options


 Global debugging settings
      --debug-level=LEVEL    set Mailutils debugging level
      --debug-line-info      show source info with debugging messages

  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs to <bug-mailutils@gnu.org>.
root@ubuntu4146:/data/gitlabData/backups# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rpjwPr04-1660292705206)(https://img-blog.csdn.net/20170819111900567?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

查看上面的mail帮助文档,使用 -t 选项 指定一个收件人列表。

下面测试发送一封邮件给3个人,如下所示:

root@ubuntu4146:/data/gitlabData/backups# cat /data/gitlabData/backups/log/2017-08-17.log | mail -s "Email Subject : Hello from Linux Server by shell" -t yizhongwei@oaserver.dw.gdbbk.com ouyangpeng@oaserver.dw.gdbbk.com huxiaoqiao@oaserver.dw.gdbbk.com

OA邮箱收到邮件可以看到有三个收件人。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ue7h97HL-1660292705207)(https://img-blog.csdn.net/20170819112047727?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

上面的命令是Ubuntu系统的mail命令的用法,但是在Center OS系统时,该命令有所不同

首先通过如下命令 lsb_release -a 查看系统版本

[root@localhost gitlabDataBackup]# lsb_release -a
LSB Version:	:base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:	CentOS
Description:	CentOS release 6.8 (Final)
Release:	6.8
Codename:	Final

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DwoJEXxZ-1660292705208)(https://img-blog.csdn.net/20170821090914622?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

然后使用命令 man help 查看mail命令的用法

[root@localhost gitlabDataBackup]# mail --help
mail: illegal option -- -
Usage: mail -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users
[root@localhost gitlabDataBackup]# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5PhJOACE-1660292705208)(https://img-blog.csdn.net/20170821091440338?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

使用 man mail 命令查看mail的更加具体的用法

MAILX(1)                         User Commands                        MAILX(1)

NAME
       mailx - send and receive Internet mail

SYNOPSIS
       mailx [-BDdEFintv~] [-s subject] [-a attachment ] [-c cc-addr] [-b bcc-addr] [-r from-addr] [-h hops] [-A account] [-S variable[=value]] to-addr . . .
       mailx [-BDdeEHiInNRv~] [-T name] [-A account] [-S variable[=value]] -f [name]
       mailx [-BDdeEinNRv~] [-A account] [-S variable[=value]] [-u user]

DESCRIPTION
       Mailx is an intelligent mail processing system, which has a command syntax reminiscent of ed(1) with lines replaced by messages.  It is based on Berkeley Mail 8.1,
       is intended to provide the functionality of the POSIX mailx command, and offers extensions for MIME, IMAP, POP3, SMTP, and S/MIME.  Mailx  provides  enhanced  fea-
       tures  for interactive use, such as caching and disconnected operation for IMAP, message threading, scoring, and filtering.  It is also usable as a mail batch lan-
       guage, both for sending and receiving mail.

       The following options are accepted:

       -A name
              Executes an account command (see below) for name after the startup files have been read.

       -a file
              Attach the given file to the message.

       -B     Make standard input and standard output line-buffered.

       -b address
              Send blind carbon copies to list.  List should be a comma-separated list of names.

       -c address
              Send carbon copies to list of users.

       -D     Start in disconnected mode; see the description for the disconnected variable option.

       -d     Enables debugging messages and disables the actual delivery of messages.  Unlike -v, this option is intended for mailx development only.

       -e     Just check if mail is present in the system mailbox.  If yes, return an exit status of zero, else, a non-zero value.

       -E     If an outgoing message does not contain any text in its first or only message part, do not send it but discard it silently, effectively setting the skipemp-
              tybody variable at program startup.  This is useful for sending messages from scripts started by cron(8).

       -f [file]
              Read  in  the  contents  of the user’s mbox (or the specified file) for processing; when mailx is quit, it writes undeleted messages back to this file.  The
              string file is handled as described for the folder command below.

       -F     Save the message to send in a file named after the local part of the first recipient’s address.

       -H     Print header summaries for all messages and exit.

       -h hops
              Invoke sendmail with the specified hop count.  This option has no effect when SMTP is used for sending mail.

       -i     Ignore tty interrupt signals.  This is particularly useful when using mailx on noisy phone lines.

       -I     Shows the ‘Newsgroup:’ or ‘Article-Id:’ fields in the header summary.  Only applicable in combination with -f.

       -n     Inhibits reading /etc/mail.rc upon startup.  This option should be activated for mailx scripts that are invoked on more than one machine, because  the  con-
              tents of that file may differ between them.

       -N     Inhibits the initial display of message headers when reading mail or editing a mail folder.

       -q file
              Start the message with the contents of the specified file.  May be given in send mode only.

       -r address
              Sets  the  From  address.  Overrides  any  from variable specified in environment or startup files.  Tilde escapes are disabled.  The -r address options are
              passed to the mail transfer agent unless SMTP is used.  This option exists for compatibility only; it is recommended  to  set  the  from  variable  directly
              instead.

       -R     Opens any folders read-only.

       -s subject
              Specify subject on command line (only the first argument after the -s flag is used as a subject; be careful to quote subjects containing spaces).

       -S variable[=value]
              Sets the internal option variable and, in case of a string option, assigns value to it.

       -T name
              Writes  the ‘Message-Id:’ and ‘Article-Id:’ header fields of each message read in the file name.  Implies -I.  Compressed files are handled as described for
              the folder command below.

       -t     The message to be sent is expected to contain a message header with ‘To:’, ‘Cc:’, or ‘Bcc:’ fields giving its recipients.  Recipients specified on the  com-
              mand line are ignored.

       -u user
              Reads the mailbox of the given user name.

       -v     Verbose mode.  The details of delivery are displayed on the user’s terminal.

       -V     Print mailx’s version and exit.

       -~     Enable tilde escapes even if not in interactive mode.

后面还有很多介绍,就不贴代码了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sKjbkWDz-1660292705209)(https://img-blog.csdn.net/20170821091721970?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YbcDqkhj-1660292705210)(https://img-blog.csdn.net/20170821091803477?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

由上面可以发现,Ubuntu的mail命令和Center OS系统的mail命令是不一样的,因此在Center OS 系统想同时发生给多个人的话,不能使用-t选项。

因此在Center OS 上,使用如下命令来进行给多个人发送邮件

[root@localhost gitlabDataBackup]# echo "Test send mail to multiple people" | mail -s "Congratulation! GitLab clean old backupFiles Success Report." -a /root/gitlabDataBackup/log/2017-08-21.log ouyangpeng@oaserver.dw.gdbbk.com -c yizhongwei@oaserver.dw.gdbbk.com,huxiaoqiao@oaserver.dw.gdbbk.com
You have new mail in /var/spool/mail/root
[root@localhost gitlabDataBackup]# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VbDge7g0-1660292705211)(https://img-blog.csdn.net/20170821093824803?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

收到的OA邮件如下所示:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jbo4LPCW-1660292705212)(https://img-blog.csdn.net/20170821093857866?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

4、检查邮件是否发出

检查所传送的电子邮件是否送出,或滞留在邮件服务器中

/usr/lib/sendmail -bp

若屏幕显示为“Mail queue is empty” 的信息,表示mail 已送出。
若为其他错误信息,表示电子邮件因故尚未送出。

例如下面执行命令之后,就可以看到有些很久之前的邮件没有发送出去。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NQwhk21I-1660292705214)(https://img-blog.csdn.net/20170818105235981?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

5、读取邮件

系统收到邮件都会保存在“/var/spool/mail/[linux用户名]”文件中。
在linux中输入mail回车 ,就进入了收件箱,并显示邮件列表。

root@ubuntu4146:/# mail
"/var/mail/root": 3 messages 3 new
>N   1 Cron Daemon        二  8月 15 10: 147/5848  Cron <root@ubuntu4146> /data/gitlabData/backups/auto_backup.sh -D 1    
 N   2 Cron Daemon        二  8月 15 10: 147/5848  Cron <root@ubuntu4146> /data/gitlabData/backups/auto_backup.sh -D 1    
 N   3 Cron Daemon        三  8月 16 06:  18/793   Cron <root@ubuntu4146> test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
? 
Return-Path: <root@ubuntu4146.ubantu4146>
X-Original-To: root
Delivered-To: root@ubuntu4146.ubantu4146
Received: by ubuntu4146.ubantu4146 (Postfix, from userid 0)
	id 5A4AC1463CE; Tue, 15 Aug 2017 10:30:37 +0800 (CST)
From: root@ubuntu4146.ubantu4146 (Cron Daemon)
To: root@ubuntu4146.ubantu4146
Subject: Cron <root@ubuntu4146> /data/gitlabData/backups/auto_backup.sh -D 1    
Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <LOGNAME=root>
Message-Id: <20170815023037.5A4AC1463CE@ubuntu4146.ubantu4146>
Date: Tue, 15 Aug 2017 10:30:37 +0800 (CST)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8mv9sCvA-1660292705215)(https://img-blog.csdn.net/20170818105627824?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

#6、编写shell脚本监控执行任务然后邮件通知

可以参考我下一篇博客

博客里面详细说明了如何进行任务监控以及邮件通知!


[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wElwbjGb-1660292705216)(https://img-blog.csdn.net/20150708201910089)]

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng/article/details/76220621

如果本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行打赏。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yKNmLffQ-1660292705217)(https://img-blog.csdn.net/20170413233715262?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb3V5YW5nX3Blbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/179773.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • pycharm支持文件式和交互式执行python代码_编写教材的步骤

    pycharm支持文件式和交互式执行python代码_编写教材的步骤Pycharm中使用Ipython编写交互式notebook1.建议直接下载安装Anaconda,其包含丰富的库,以及我们接下来需要使用的Ipython.所有的操作都一部到位,具体从Pycharm调用Anaconda可参考百度。2. 在工程中新建文件,选择JupyterNotebook,即IPythonNotebookfile,文件的后缀为.ipynb。

  • 仿微博php生成短网址

    htmlcodephpcode推荐:http://www.cnblogs.com/roucheng/p/3528396.html

    2021年12月27日
  • 解决Symantec卸载需要密码问题又一新招

    解决Symantec卸载需要密码问题又一新招今天在同事哪里学到卸载SYMANTEC需要密码又一新招,看图就知道了,对!就是修改注册表,在LOCAL_MACHINE—SOFTWARE—SYMANTEC—SYMANTECENDPOINTPRTECTION—SECURITY找到UseVPUninstallPassword键值,修心DWORD值,看图就知道了哈,说多了废话.我这里是SEP注册表…

  • 几种web字体格式建议收藏

    目前,文字信息仍是网站最主要的内容,随着CSS3技术的不断成熟,Web字体逐渐成为话题,这项让未来Web更加丰富多彩的技术拥有多种实现方案,其中之一是通过@font-face属性在网页中嵌入自定义字体

    2021年12月20日
  • 详解数据库的第一范式、第二范式、第三范式、BCNF范式[通俗易懂]

    版权声明:本文转自小小呆原创文章https://blog.csdn.net/gui951753/article/details/79609874第一范式定义以及分析:问题研究:第二范式必备知识点定义分析:解决办法:问题研究:第三范式:定义:分析:问题分析:BCNF范式分析问题研究小结:参考文献…

  • Springboot文件上传机制[通俗易懂]

    Springboot文件上传机制[通俗易懂]springboot文件上传机制:1.访问路径2. 上传完成后返回访问文件地址3. 我们只需要访问返回的地址就可以访问到图片4. yaml配置文件(localpath是实际存储的地址)5. 添加配置类,进行访问地址和存储地址映射 @Value(“${file.upload.suffixPath}”) private String uploadSuffixPath; @Value(“${file.upload.localPath}”) private Strin

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号