說明:
linux內建的mail可以實現外部smtp發郵件。不需要本地配置postfix,sendmail郵件伺服器,配置整套下來確實需要點時間。
實現:
1.關閉原生sendmail服務或者postfix服務
| 代碼如下 |
複製代碼 |
|
sendmail
# service sendmail stop
# chkconfig sendmail off
postfix
# service postfix stop
# chkconfig postfix off |
2.修改/etc/mail.rc,增加一下類似N行,指定外部的smtp伺服器位址、帳號密碼等。
| 代碼如下 |
複製代碼 |
# cp /etc/mail.rc /etc/mailrc.bak.20131113
# cat /etc/mail.rc //內容如下
set from=123456@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=123456
set smtp-auth-password=sbsbsbsb
set smtp-auth=login
|
3.然後通過命令來發送郵件
| 代碼如下 |
複製代碼 |
# echo hello word | mail -s " title" 123456@qq.com
|
RHEL6下發送郵件沒有問題。但RHEL5下,一直沒辦法發送。這是為什麼呢?可以先看下RHEL6和RHEL5下mail和mailx的不同
RHEL6下
| 代碼如下 |
複製代碼 |
# rpm -qa|grep mailx
mailx-12.4-6.el6.x86_64
# ll /bin/mail* //mail連結到mailx,mailx版本為12.4
lrwxrwxrwx. 1 root root 5 7月 22 2011 /bin/mail -> mailx
-rwxr-xr-x. 1 root root 390424 2月 16 2010 /bin/mailx
# mail -V
12.4 7/29/08
|
RHEL5下,則發現了一些不同
| 代碼如下 |
複製代碼 |
# rpm -qa|grep mailx
mailx-8.1.1-44.2.2
# ll /bin/mail* //mailx連結到mail命令,mailx版本為8.1
-rwxr-xr-x 1 root mail 84856 Jun 6 2007 /bin/mail
lrwxrwxrwx 1 root root 4 Apr 12 18:03 /bin/mailx -> mail
# mail -V |
此處運行就會出錯,沒有-V這個參數 也就是說,在RHEL65下,運行mail和mailx,實際上都是一個命令,而mail版本不同,導致了RHEL5下mail命令無法調用外部SMTP伺服器。因此安裝mailx新版本可以解決此問題。
1.RHEL5安裝新版本mailx
| 代碼如下 |
複製代碼 |
# wget ftp://ftp.spline.inf.fu-berlin.de/pub/gentoo/distfiles/mailx-12.4.tar.bz2 //下載mailx-12.4.tar.bz2,更多下載地址http://www.filewatcher.com/m/mailx-12.4.tar.bz2.271482.0.0.html
# tar jxvf mailx-12.4.tar.bz2
# cd mailx-12.4
# make
# make install UCBINSTALL=/usr/bin/install
# whereis mailx //查看mailx程式預設安裝位置、及版本
mailx: /usr/local/bin/mailx /usr/share/man/man1p/mailx.1p.gz
# mailx -V
|
12.4 7/29/08
2.修改/etc/nail.rc,安裝後的mailx使用的是/etc/nail.rc而不是/etc/mail.rc。這個需要注意下!!!
| 代碼如下 |
複製代碼 |
# vi /etc/nail.rc //在行尾添加如下行
set from=123456@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=123456
set smtp-auth-password=sbsbsbsb
set smtp-auth=login
|
2.然後通過命令來發送郵件
| 代碼如下 |
複製代碼 |
|
# echo hello word | mailx -v -s " title" 123456@qq.com //成功發送的話,就可以看到類似的提示
235 2.7.0 Authentication successful
>>> MAIL FROM:<demo@qq.com>
250 2.1.0 Ok
>>> RCPT TO:<xxxxx@qq.com>
250 2.1.5 Ok
>>> DATA
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 9291963494
>>> QUIT
221 2.0.0 Bye |