在CentOS6 系统里为 postfix 配置外部smtp服务器发邮件

1.yum update # 更新软件
2.hostname erow.org # 设置主机名
3.vi /etc/postfix/main.cf # 编辑 postfix 配置文件,设置 myhostname = erow.org
4.yum install stunnel # 安装 stunnel 软件,目的是转发465端口数据
5.vi /etc/stunnel/stunnel.conf # 编辑 stunnel 配置文件,内容如下:

[smtp-tls-wrapper]
accept = 11125
client = yes
connect = smtp.mxhichina.com:465

6.vi /etc/rc.d/init.d/stunnel # 编辑 stunnel 启动配置文件,内容如下:

#!/bin/bash
#
# Init Script to run stunnel in daemon mode at boot time.
#
# Author: Riccardo Riva - RPM S.r.l.
# Revision 1.0 - 2010 November, 11

#====================================================================
# Run level information:
#
# chkconfig: 2345 99 99
# description: Secure Tunnel
# processname: stunnel
#
# Run "/sbin/chkconfig --add stunnel" to add the Run levels.
# This will setup the symlinks and set the process to run at boot.
#====================================================================

#====================================================================
# Paths and variables and system checks.

# Source function library
. /etc/rc.d/init.d/functions

# Check that networking is up.
#
[ ${NETWORKING} ="yes" ] || exit 0

# Path to the executable.
#
SEXE=/usr/bin/stunnel

# Path to the configuration file.
#
CONF=/etc/stunnel/stunnel.conf

# Check the configuration file exists.
#
if [ ! -f $CONF ] ; then
echo "The configuration file cannot be found!"
exit 0
fi

# Path to the lock file.
#
LOCK_FILE=/var/lock/subsys/stunnel

#====================================================================

# Run controls:

prog=$"stunnel"

RETVAL=0

# Start stunnel as daemon.
#
start() {
if [ -f $LOCK_FILE ]; then
echo "stunnel is already running!"
exit 0
else
echo -n $"Starting $prog: "
$SEXE $CONF
fi

RETVAL=$?
[ $RETVAL -eq 0 ] && success
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
}

# Stop stunnel.
#
stop() {
if [ ! -f $LOCK_FILE ]; then
echo "stunnel is not running!"
exit 0

else

echo -n $"Shutting down $prog: "
killproc stunnel
RETVAL=$?
[ $RETVAL -eq 0 ]
rm -f $LOCK_FILE
echo
return $RETVAL

fi
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
condrestart)
if [ -f $LOCK_FILE ]; then
stop
start
RETVAL=$?
fi
;;
status)
status stunnel
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac

exit $RETVAL

#--- End of file ---

7.chkconfig stunnel on # 启用 stunnel 服务
8.chmod +x /etc/init.d/stunnel # 为 stunnel 配置文件,添加执行权限
9.service stunnel start # 启动 stunnel 服务
10.mkdir /etc/postfix/sasl # 创建 sasl 目录
11.vi /etc/postfix/sasl/sasl_passwd # 创建邮箱账户登录信息配置文件,内容如下:

[localhost]:11125 postmaster@erwo.org:password

12.postmap /etc/postfix/sasl/sasl_passwd # 生成postfix密码配置文件
13.chmod 600 /etc/postfix/sasl/* # 为了保护smtp的登录信息,需要修改上边两个文件为root用户只读写
14.vi /etc/postfix/main.cf # 修改 /etc/postfix/main.cf 文件,修改 relayhost = [localhost]:11125 , 添加SMTP信息:

relayhost = [localhost]:11125

#### SMTP
# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Enable STARTTLS encryption
# smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_generic_maps = hash:/etc/postfix/generic

15.因为多数免费邮箱限定发信人是登录名(如postmaster@erwo.org),所以在 /etc/postfix/generic 文件里填写可能发信的地址,如:apache

apache postmaster@erwo.org

16.postconf -e 'smtp_host_lookup = dns, native' # 设置postfix dns解析
17./etc/init.d/postfix restart # 重启postfix
18.tail /var/log/maillog # 如有错误,可查看日志

参考:https://randomcentos.wordpress.com/2015/04/21/installing-stunnel-client-on-centos-6-6/

标签: none

评论已关闭