您的位置:首页 > 大数据 > 人工智能

linux中mail/mailx使用外部smtp发送邮件

2012-11-21 16:43 1366 查看
一、LINUX下的mail \ mailx为什么无法使用外部SMTP发邮件?

from:/article/10109703.html

有时为了达到某种需求统一使用某一SMTP服务器作为发送邮件报告。

通常的做法都是,关闭本机的sendmail服务,修改/etc/mail.rc,增加一下类似两行,指定外部的smtp服务器地址、帐号密码等。

[plain]
view plaincopyprint?

# vi /etc/mail.rc set from=demo@qq.com smtp=smtp.qq.com set smtp-auth-user=demo smtp-auth-password=demopass smtp-auth=login

# vi /etc/mail.rc
set from=demo@qq.com smtp=smtp.qq.com
set smtp-auth-user=demo smtp-auth-password=demopass smtp-auth=login


然后通过命令来发送邮件

[plain]
view plaincopyprint?

echo hello word | mail -s "demo title" demo@qq.com

echo  hello word | mail -s "demo title" demo@qq.com

可是发现在RHEL6运行是没有问题,在RHEL5下,mail命令始终是调用本地的sendmail服务来发送邮件的。到底RHEL6和RHEL5下的mail和mailx又有着什么关系呢?

RHEL6下

mail 链接到 mailx 命令,mailx版本为12.4

[plain]
view plaincopyprint?

# rpm -qa|grep mailx mailx-12.4-6.el6.x86_64 # ll /bin/mail* 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

# rpm -qa|grep mailx
mailx-12.4-6.el6.x86_64
# ll /bin/mail*
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下,则发现了一些不同

mailx 链接到 mail 命令,mailx版本为8.1

[plain]
view plaincopyprint?

# rpm -qa|grep mailx mailx-8.1.1-44.2.2 # ll /bin/mail* -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这个参数

# rpm -qa|grep mailx
mailx-8.1.1-44.2.2
# ll /bin/mail*
-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这个参数

也就是说,在RHEL6\5下,运行mail 和mailx,实际上都是一个命令,而mail版本不同,导致了RHEL5下mail命令无法调用外部SMTP服务器。

安装mailx新版本可解决此问题。

mailx官方站点 http://heirloom.sourceforge.net/
下载最新版本mailx-12.4.tar.bz2 (271.5 kB) http://sourceforge.net/projects/heirloom/files/latest/download?source=files
解压、安装。下载的压缩包里有安装说明(见INSTALL 文件)。默认安装是make ; make install

同时安装说明也说了,如果make install 出错,则换为 make install UCBINSTALL=/usr/bin/install命令试试。

命令:

[plain]
view plaincopyprint?

# tar jxvf mailx-12.4.tar.bz2 # cd mailx-12.4 # make # make install UCBINSTALL=/usr/bin/install

# tar jxvf mailx-12.4.tar.bz2
# cd mailx-12.4
# make
# make install UCBINSTALL=/usr/bin/install

我在make install很长时间没有反应,只有Ctrl + C 中断了,输入make install UCBINSTALL=/usr/bin/install 则可以成功安装

查看mailx程序默认安装位置、及版本

[plain]
view plaincopyprint?

# whereis mailx mailx: /usr/local/bin/mailx /usr/share/man/man1p/mailx.1p.gz # mailx -V 12.4 7/29/08

# whereis mailx
mailx: /usr/local/bin/mailx /usr/share/man/man1p/mailx.1p.gz
# mailx -V
12.4 7/29/08

安装后的mailx使用的是 /etc/nail.rc 配置,不是 /etc/mail.rc 这个需要注意下。

用mailx命令测试发送邮件

[plain]
view plaincopyprint?

# echo hello word | mailx -v -s "demo title" demo@qq.com

# echo hello word | mailx -v -s "demo title" demo@qq.com


成功发送的话,就可以看到类似的提示

[plain]
view plaincopyprint?

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

二、Linux5中使用mailx利用SMTP发送邮件
from:http://blog.sina.com.cn/s/blog_4d22b97201019yav.html

Linux5中使用mailx利用SMTP发送邮件

Linux中mail/mailx默认使用sendmail发送邮件,我们可以关闭sendmail让其使用外部SMTP发送mail。

由于Linux 5级以下的版本默认使用sendmail发送mail,关闭sendmail之后将无法发送mail,而Linux 6却可以,他们主要区别是使用mailx的版本不一样.Linux 6使用mailx 版本mailx-12.4-6.el6.x86_64而Linux 5使用mailx版本mailx-8.1.1-44.2.2 。

故为了让linux 5在关闭sendmail的情况下,使用外部SMTP发送mail,需要在Linux5上安装mailx-12.4-6.el6.x86_64.

下载mailx-12.4-6.el6.x86_64

mailx官方站点 http://heirloom.sourceforge.net/ 下载最新版本mailx-12.4.tar.bz2 (271.5 kB) http://sourceforge.net/projects/heirloom/files/latest/download?source=files
安装mailx-12.4-6.el6.x86_64

[root@zabbix opt]# tar -jxvf mailx-12.4.tar.bz2

[root@zabbix opt]# cd mailx-12.4

[root@zabbix opt]# make

[root@zabbix opt]# make install UCBINSTALL=/usr/bin/install

配置mailx-12.4-6.el6.x86_64(以QQ SMTP为例)

[root@zabbix etc]# pwd

/etc

[root@zabbix etc]# cat /etc/nail.rc

# This is the configuration file for Heirloom mailx (formerly

# known under the name "nail".

# See mailx(1) for further options.

# This file is not overwritten when 'make install' is run in

# the mailx build process again.

#set from=zabbix@ips.com smtp=mail.qiye.163.com

#set smtp-auth-user=zabbix smtp-auth-password=zabbix123 smtp-auth=login

set from=1141680682@qq.com smtp=smtp.qq.com

set smtp-auth-user=1141680682 smtp-auth-password=xxxxxx smtp-auth=login

# Sccsid @(#)nail.rc 2.10 (gritter) 3/4/06

# Do not forward to mbox by default since this is likely to be

# irritating for most users today.

set hold

# Append rather than prepend when writing to mbox automatically.

# This has no effect unless 'hold' is unset again.

set append

# Ask for a message subject.

set ask

# Assume a CRT-like terminal and invoke a pager.

set crt

# Messages may be terminated by a dot.

set dot

# Do not remove empty mail folders in the spool directory.

# This may be relevant for privacy since other users could

# otherwise create them with different permissions.

set keep

# Do not remove empty private mail folders.

set emptybox

# Quote the original message in replies by "> " as usual on the Internet.

set indentprefix="> "

# Automatically quote the text of the message that is responded to.

set quote

# Outgoing messages are sent in ISO-8859-1 if all their characters are

# representable in it, otherwise in UTF-8.

set sendcharsets=iso-8859-1,utf-8

# Display sender's real names in header summaries.

set showname

# Display the recipients of messages sent by the user himself in

# header summaries.

set showto

# Automatically check for new messages at each prompt, but avoid polling

# of IMAP servers or maildir folders.

set newmail=nopoll

# If threaded mode is activated, automatically collapse thread.

set autocollapse

# Hide some header fields which are uninteresting for most human readers.

ignore received in-reply-to message-id references

ignore mime-version content-transfer-encoding

# Only include selected header fields when forwarding messages.

fwdretain subject date from to

[root@zabbix etc]#

测试mailx-12.4-6.el6.x86_64

[root@zabbix etc]# echo hello world |mailx -s "test10" kylixxu@ips.com

[root@zabbix etc]# could not connect: Connection refused

"/root/dead.letter" 11/296

. . . message not sent.

出现以上Error,由于发送邮件是我的QQ邮箱,在外的QQ邮箱发现如下一份mail:

尊敬的QQ邮箱用户:
我们发现您尝试设置SMTP服务,但设置未成功。 您可以检查以下的细节来解决:
1. 您是否在邮箱中开启了这项服务,如果尚未开启,请您在【邮箱设置】的【帐号】中开启相关服务
2. 您是否设置了邮箱独立密码,如果您设置了独立密码,在客户端设置时,密码输入项需填写邮箱独立密码。
其他可能对您有用的帮助信息:

POP3/SMTP服务设置帮助
于是,查看QQ邮箱的SMTP设置,果然SMTP服务没有开启,开启后继续测试:

[root@zabbix etc]# echo hello word | mailx -v -s "demo title" kylixxu@ips.com

Resolving host smtp.qq.com . . . done.

Connecting to 112.90.138.97 . . . connected.

220 smtp.qq.com Esmtp QQ Mail Server

>>> EHLO zabbix

250-smtp.qq.com

250-PIPELINING

250-SIZE 52428800

250-AUTH LOGIN PLAIN

250-AUTH=LOGIN

250-MAILCOMPRESS

250 8BITMIME

>>> AUTH LOGIN

334 VXNlcm5hbWU6

>>> MTE0MTY4MDY4Mg==

334 UGFzc3dvcmQ6

>>> MTIzNDU2Nzg5

235 Authentication successful

>>> MAIL FROM:<1141680682@qq.com>

250 Ok

>>> RCPT TO:<kylixxu@ips.com>

250 Ok

>>> DATA

354 End data with <CR><LF>.<CR><LF>

>>> .

250 Ok: queued as

>>> QUIT

221 Bye

看起来发送成功了,但是此时QQ邮箱又收到如下mail:

尊敬的用户,您好!
很遗憾,您的邮件“demo title[c]hello word”未能成功发出。
原因是 您的邮件包含不合适的内容,请修改重试。
温馨提醒:未发送成功的邮件会保存在“草稿箱”中,建议登录网页邮箱尝试发送。

点击登录网页邮箱
腾讯邮箱运营团队

邮件的意思是邮件的内容包含不合适的内容,于是更换邮件内容:

[root@zabbix etc]# mailx -s "test" kylixxu@ips.com </etc/mail.rc

[root@zabbix etc]# mailx -s "test2" kylixxu@ips.com.cn < /etc/mail.rc

终于,在我的kylixxu@ips.com/kylixxu@ips.com.cn收到以上发送信息。

至此,整个配置结束。

三、本人测试过程:

1.upgrade mailx:

下载mailx-12.4-6.el6.x86_64

mailx官方站点 http://heirloom.sourceforge.net/

下载最新版本mailx-12.4.tar.bz2 (271.5 kB) http://sourceforge.net/projects/heirloom/files/latest/download?source=files
# tar jxvf mailx-12.4.tar.bz2

# cd mailx-12.4

# make

# make install UCBINSTALL=/usr/bin/install

2./etc/resolv.conf文件增加name server:

[root@oralin5 etc]# cat /etc/resolv.conf

#search localdomain

nameserver 8.8.8.8

3.配置/etc/nail.rc文件:

[root@oralin5 etc]# cat /etc/nail.rc

# This is the configuration file for Heirloom mailx (formerly

# known under the name "nail".

# See mailx(1) for further options.

# This file is not overwritten when 'make install' is run in

# the mailx build process again.

# Sccsid @(#)nail.rc 2.10 (gritter) 3/4/06

# Do not forward to mbox by default since this is likely to be

# irritating for most users today.

set from=testmail@163.com
smtp=smtp.163.com

set smtp-auth-user=testmail smtp-auth-password=testmail123 smtp-auth=login


set hold

# Append rather than prepend when writing to mbox automatically.

# This has no effect unless 'hold' is unset again.

set append

# Ask for a message subject.

set ask

# Assume a CRT-like terminal and invoke a pager.

set crt

# Messages may be terminated by a dot.

set dot

# Do not remove empty mail folders in the spool directory.

# This may be relevant for privacy since other users could

# otherwise create them with different permissions.

set keep

# Do not remove empty private mail folders.

set emptybox

# Quote the original message in replies by "> " as usual on the Internet.

set indentprefix="> "

# Automatically quote the text of the message that is responded to.

set quote

# Outgoing messages are sent in ISO-8859-1 if all their characters are

# representable in it, otherwise in UTF-8.

set sendcharsets=iso-8859-1,utf-8

# Display sender's real names in header summaries.

set showname

# Display the recipients of messages sent by the user himself in

# header summaries.

set showto

# Automatically check for new messages at each prompt, but avoid polling

# of IMAP servers or maildir folders.

set newmail=nopoll

# If threaded mode is activated, automatically collapse thread.

set autocollapse

# Hide some header fields which are uninteresting for most human readers.

ignore received in-reply-to message-id references

ignore mime-version content-transfer-encoding

# Only include selected header fields when forwarding messages.

fwdretain subject date from to

[root@oralin5 etc]#

只需要增加如下两行红色配置即可:

set from=testmail@163.com
smtp=smtp.163.com

set smtp-auth-user=testmail smtp-auth-password=testmail123 smtp-auth=login


4.测试mailx发送邮件情况:

echo hello world|mailx -s 'test' testmailx@sohu.com

5.成功接收邮件!

另外一篇参考:

LINUX下的mail\mailx为什么无法使用外部SMTP发邮件:

http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0823/13194.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: