CentOS下使用mail命令發送郵件

來源:互聯網
上載者:User

CentOS下使用mail命令發送郵件

Linux伺服器mail程式本身就是調用sendmail來進行郵件發送的,sendmail伺服器提供對外的郵件發送功能。CentOS預設不能發送郵件,需要發送郵件的童鞋可以安裝一個sendmail程式

[root@ProxyServer~]#yum-yinstallsendmail[root@ProxyServer~]#/etc/init.d/sendmailstartStartingsendmail:[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、通過命令列發送郵件

[root@ProxyServer~]#mail-s"test"justin@51cto.comdfdafddfadffadfEOT[root@ProxyServer~]#

第一行是輸入的命令,-s表示郵件的主題,後面的justin@51cto.com則是郵件的接收人,輸入完這行命令後斷行符號,會進入郵件內文的編寫,可以輸入任何文字,比如上面的三行。當郵件內文輸入完成後,需要按CTRL+D結束輸入,此時會提示你輸入Cc地址,即郵件抄送地址,沒有直接斷行符號就完成了郵件的發送。

2、使用管道進行郵件發送

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

使用管道直接敲入這行命令即可完成郵件的發送,其中echo後的是郵件內文。

3、使用檔案進行郵件發送

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

使用上面的命令後,我們就可以把/etc/passwd檔案的內容作為郵件的內容發送給justin@51cto.com了

很多情況下,我們也需要使用郵件來發送附件,在linux下使用mail命令發送附件也很簡單,不過首先需要安裝uuencode軟體包,這個程式是對二進位檔案進行編碼使其適合通過郵件進行發送,直接使用centos的yum源可能找不到uuencode命令的包sharutils,我這裡使用了網易Yum源

[root@ProxyServeryum.repos.d]#yum-yinstallsharutils[root@ProxyServeryum.repos.d]#uuencodeCentOS6-Base-163.repo163_yum_repo|mail-s"附件"pengyl2@finchina.com</etc/passwd[root@ProxyServeryum.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#文末添加以下67setfrom=justin@51cto.comsmtp=smtp.51cto.com68setsmtp-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:發送的郵件地址,對方顯示的寄件者

smtp:發生的外部smtp伺服器的地址

smtp-auth-user:外部smtp伺服器認證的使用者名稱

smtp-auth-password:外部smtp伺服器認證的使用者密碼

smtp-auth:郵件認證的方式

如果提示mail: command not found

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

那麼就是沒有安裝mail命令,此時需要安裝mail命令

[root@ProxyServer~]#yuminstallmailx-y[root@ProxyServer~]#rpm-qa|grepmaillibreport-plugin-mailx-2.0.9-19.el6.x86_64mailx-12.4-7.el6.x86_64procmail-3.22-25.1.el6.x86_64mailcap-2.1.31-2.el6.noarchsendmail-8.14.4-8.el6.x86_64[root@ProxyServer~]#

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.