CentOS下使用mail命令发送邮件

简介:

如果提示mail: command not found

[root@ProxyServer ~] # mail -s "password" justin@51cto.com < /etc/passwd
- bash : mail:  command  not found

那么就是没有安装mail命令,此时需要安装mail命令

[root@ProxyServer ~] # yum install mailx -y
[root@ProxyServer ~] # rpm -qa|grep mail
libreport-plugin-mailx-2.0.9-19.el6.x86_64
mailx-12.4-7.el6.x86_64
procmail-3.22-25.1.el6.x86_64
mailcap-2.1.31-2.el6.noarch
sendmail-8.14.4-8.el6.x86_64
[root@ProxyServer ~] #

Linux服务器mail程序本身就是调用sendmail来进行邮件发送的,sendmail服务器提供对外的邮件发送功能。CentOS默认不能发送邮件,需要发送邮件的童鞋可以安装一个sendmail程序

[root@ProxyServer ~] # yum -y install sendmail
[root@ProxyServer ~] # /etc/init.d/sendmail start
Starting sendmail:                                         [  OK  ]
Starting sm-client:                                        [  OK  ]
[root@ProxyServer ~] # chkconfig sendmail on
[root@ProxyServer ~] # man mail
......
        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]
......

1、通过命令行发送邮件

[root@ProxyServer ~] # mail -s "test" justin@51cto.com 
dfdafd
dfadf
fadf
EOT
[root@ProxyServer ~] #

第一行是输入的命令,-s表示邮件的主题,后面的justin@51cto.com则是邮件的接收人,输入完这行命令后回车,会进入邮件正文的编写,可以输入任何文字,比如上面的三行。当邮件正文输入完成后,需要按CTRL+D结束输入,此时会提示你输入Cc地址,即邮件抄送地址,没有直接回车就完成了邮件的发送。

wKioL1ZJcWDSpWIsAAAyLpsj3lY256.png

2、使用管道进行邮件发送


[root@ProxyServer ~] # echo "hello,echo"|mail -s "echo" justin@51cto.com

wKiom1ZJch2QWrGYAAAr0bqjmfw207.png

使用管道直接敲入这行命令即可完成邮件的发送,其中echo后的是邮件正文。

3、使用文件进行邮件发送


[root@ProxyServer ~] # mail -s "password" justin@51cto.com < /etc/passwd

使用上面的命令后,我们就可以把/etc/passwd文件的内容作为邮件的内容发送给justin@51cto.com了

wKiom1ZJcrCQG8JbAABlzcaKwgs900.png

  很多情况下,我们也需要使用邮件来发送附件,在linux下使用mail命令发送附件也很简单,不过首先需要安装uuencode软件包,这个程序是对二进制文件进行编码使其适合通过邮件进行发送,直接使用centos的yum源可能找不到uuencode命令的包sharutils,我这里使用了网易Yum源

[root@ProxyServer yum.repos.d] # yum -y install sharutils
[root@ProxyServer yum.repos.d] # uuencode CentOS6-Base-163.repo 163_yum_repo |mail -s "附件" pengyl2@finchina.com < /etc/passwd
[root@ProxyServer yum.repos.d] #

CentOS6-Base-163.repo 文件作为邮件的附件发送出去了。uuencode有两个参数,第一个是要发送的文件CentOS6-Base-163.repo,第二个是显示的文件名称163_yum_repo。

4、使用外部smtp(qq的SMTP)来发送邮件

bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动Sendmail服务,而通过修改配置文件/etc/mail.rc(/etc/nail.rc)可以使用外部SMTP服务器,可以达到不使用sendmail而用外部的smtp服务器发送邮件的目的。

[root@ProxyServer ~] # vim /etc/mail.rc          #文末添加以下
  67  set  from=justin@51cto.com smtp=smtp.51cto.com
  68  set  smtp-auth-user=justin@51cto.com smtp-auth-password=51cto smtp-auth=login
[root@ProxyServer ~] # source /etc/mail.rc
[root@ProxyServer ~] # mail -s "51cto" 15001*****@139.com < /etc/passwd

from:发送的邮件地址,对方显示的发件人

smtp:发生的外部smtp服务器的地址

smtp-auth-user:外部smtp服务器认证的用户名

smtp-auth-password:外部smtp服务器认证的用户密码

smtp-auth:邮件认证的方式

wKioL1ZKqgzBo0p-AAA0OtsRubo776.png



     本文转自yzy121403725 51CTO博客,原文链接:http://blog.51cto.com/lookingdream/1868855,如需转载请自行联系原作者







相关文章
|
17天前
|
Linux 网络安全
Centos 防火墙端口控制命令
Centos 防火墙端口控制命令
27 3
|
10天前
|
网络协议 应用服务中间件 Linux
centos7 Nginx Log日志统计分析 常用命令
centos7 Nginx Log日志统计分析 常用命令
23 2
|
30天前
|
监控 网络协议 Linux
Linux 命令大全 & CentOS常用运维命令
Linux 命令大全 & CentOS常用运维命令
139 0
|
1月前
|
关系型数据库 MySQL 数据库
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
虚拟机Linux-Centos系统网络配置常用命令+Docker 的常用命令
34 0
|
3月前
|
消息中间件 Java Kafka
Apache Kafka-初体验Kafka(02)-Centos7下搭建单节点kafka_配置参数详解_基本命令实操
Apache Kafka-初体验Kafka(02)-Centos7下搭建单节点kafka_配置参数详解_基本命令实操
58 0
|
4月前
|
缓存 Linux
centos7更换yum源 一条命令搞定
centos7更换yum源 一条命令搞定
318 0
|
4月前
|
Linux 网络安全
CentOS7与CentOS6的一些不同地方(命令)
CentOS7与CentOS6的一些不同地方(命令)
29 0
|
4月前
|
Linux Shell Docker
[解决]Docker中安装Centos7后无法正常使用命令
[解决]Docker中安装Centos7后无法正常使用命令
55 0
|
4月前
|
Linux 网络安全 开发工具
centos7安装配置以及Linux常用命令
centos7安装配置以及Linux常用命令
|
4月前
|
安全 Unix Linux
【Linux】安装配置解决Centos&MobaXterm的使用及Linux常用命令以及命令模式
【Linux】安装配置解决Centos&MobaXterm的使用及Linux常用命令以及命令模式
77 0

热门文章

最新文章