Use the mail command in CentOS to send emails
The mail program on the Linux server calls sendmail to send mails. The sendmail server provides the function of sending mails externally. By default, you cannot send emails to CentOS. you can install a sendmail program for the children's shoes that need to send emails.
[root@ProxyServer~]
#yum-yinstallsendmail
[root@ProxyServer~]
#/etc/init.d/sendmailstart
Startingsendmail:[OK]
Startingsm-client:[OK]
[root@ProxyServer~]
#chkconfigsendmailon
[root@ProxyServer~]
#manmail
......
mailx[-BDdEFintv~][-ssubject][-aattachment][-ccc-addr][-bbcc-addr][-rfrom-addr][-hhops][-Aaccount]
[-Svariable[=value]]to-addr...
mailx[-BDdeEHiInNRv~][-Tname][-Aaccount][-Svariable[=value]]-f[name]
mailx[-BDdeEinNRv~][-Aaccount][-Svariable[=value]][-uuser]
......
1. send an email through the command line
[root@ProxyServer~]
#mail-s"test"justin@51cto.com
dfdafd
dfadf
fadf
EOT
[root@ProxyServer~]
#
The first line is the input command,-s indicates the subject of the mail, followed by the justin@51cto.com is the recipient of the mail, enter this line of command and press enter, will enter the body of the mail writing, you can enter any text, such as the preceding three lines. After entering the body of the email, press CTRL + D to complete the input. At this time, you will be prompted to enter the Cc address, that is, the Cc address. If you do not press enter, the email will be sent.
2. Use MPs queues to send emails
[root@ProxyServer~]
#echo"hello,echo"|mail-s"echo"justin@51cto.com
You can use the pipeline to directly input this line of command to send the mail. The echo is the mail body.
3. Use files to send emails
[root@ProxyServer~]
#mail-s"password"justin@51cto.com</etc/passwd
With the above command, we can send the content of the/etc/passwd file to the justin@51cto.com as the mail content
In many cases, we also need to use emails to send attachments. in linux, It is also easy to use the mail command to send attachments. However, we need to install the uencode package first, this program is used to encode binary files so that they can be sent by email. directly using the centos yum source may not find the uencode package sharutils. I use Netease Yum source here.
[root@ProxyServeryum.repos.d]
#yum-yinstallsharutils
[root@ProxyServeryum.repos.d]
# UuencodeCentOS6-Base-163.repo163_yum_repo | mail-s "attachment" pengyl2@finchina.com </etc/passwd
[root@ProxyServeryum.repos.d]
#
The CentOS6-Base-163.repo file is sent out as an attachment to the mail. Uuencode has two parameters: the first is the file CentOS6-Base-163.repo to be sent, and the second is the file name 163_yum_repo to be displayed.
4. Use external smtp (qq SMTP) to send emails
By default, bin/mail uses local sendmail to send emails. Therefore, the local machine must install and start the Sendmail service, and modify the configuration file/etc/mail. rc (/etc/nail. rc) You can use an external SMTP server to send emails without using sendmail.
[root@ProxyServer~]
# Vim/etc/mail. rc # Add the following at the end of the article
67
set
from=justin@51cto.comsmtp=smtp.51cto.com
68
set
smtp-auth-user=justin@51cto.comsmtp-auth-password=51ctosmtp-auth=login
[root@ProxyServer~]
#source/etc/mail.rc
[root@ProxyServer~]
#mail-s"51cto"15001*****@139.com</etc/passwd
From: The sent email address, the sender displayed by the recipient
Smtp: the address of the external smtp server.
Smtp-auth-user: user Name authenticated by the external smtp Server
Smtp-auth-password: User password authenticated by the external smtp Server
Smtp-auth: email authentication method
If mail: command not found is prompted
[root@ProxyServer~]
#mail-s"password"justin@51cto.com</etc/passwd
-
bash
:mail:
command
notfound
The mail command is not installed. In this case, the mail command must be installed.
[root@ProxyServer~]
#yuminstallmailx-y
[root@ProxyServer~]
#rpm-qa|grepmail
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~]
#