64位 centos 6.4 linux系统配置 postfix 启用 smtp 认证

31 篇文章 0 订阅

转载自:http://www.server110.com/postfix/201308/389.html

Postfix 的安装 

本博客已经有 Postfix 的安装教程,在此就不再重复。
Linux 下安装 Postfix 教程,请看:
http://www.server110.com/postfix/201308/389.html

配置 Postfix 启用 SMTP 认证 

1. 安装 cyrus-sasl 软件包,它牵涉的依赖关系包比较多,命令如下:
yum -y install cyrus-sasl*
命令的结果显示,如下:
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
 * epel: ftp.kddilabs.jp
Setting up Install Process
Package cyrus-sasl-lib-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-ntlm-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-plain-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-md5-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-devel-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-ldap-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-sql-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Package cyrus-sasl-gssapi-2.1.23-13.el6_3.1.x86_64 already installed and latest version
Nothing to do
因为我已经安装过了,所以显示的是 Nothing to do,你第一次安装,会有几个 Complete。
我们可以用以下命令启动 saslauthd 服务,并配置成自启动服务:
service saslauthd start
chkconfig saslauthd on
2. 添加下列内容到主配置文件,让 Postfix 启用 SMTP 认证,
# 把此代码添加到 /etc/ postfix /main.cf 最后
# smtp
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = ''
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
broken_sasl_auth_clients = yes
smtpd_client_restrictions = permit_sasl_authenticated
smtpd_sasl_security_options = noanonymous
完成以上步骤后,全新加载配置,命令如下:
postfix reload

测试 

使用 phpmailer 测试 Postfix 的 SMTP 是否有效,
phpmailer 请自行下载,下载地址: github.com/Synchro/PHPMailer
PHP 测试代码:
test.php
<?php
set_time_limit (0);
require_once('./class.phpmailer.php');
// 测试发送附件专用
$tmp_file = '/tmp/test.jpg';
$mail = new PHPMailer(true);
// 是否为 SMTP 认证
$mail->IsSMTP();
try {
        // 服务器 IP,程序在本地使用,所以填写 127.0.0.1,
        $mail->Host       = "127.0.0.1";
        // 调试模式关闭
        $mail->SMTPDebug  = 0;
        // 是否开户 SMTP 认证
        $mail->SMTPAuth   = true;
        // SMTP 认证用户名,此处填写系统用户名,可以专门创建一个普通用户用于发送邮件
        $mail->Username   = "username";
        // SMTP 认证用户密码,此处填写与系统用户名对应的系统用户密码
        $mail->Password   = 'password';
        // 设置邮件内容字符集,防止中文乱码
        $mail->CharSet = 'UTF-8';
        // 接收邮件地址
        $mail->AddAddress( 'abcdef@126.com', '忙碌的松鼠');
        // 设置我的邮件地址
        $mail->SetFrom( 'info@example.com', '忙碌的松鼠');
        // 邮件标题
        $mail->Subject = '欢迎大家关注 忙碌的松鼠';
        // 邮件内容,可以是HTML代码
        $mail->MsgHTML('欢迎大家关注 忙碌的松鼠 博客,博客地址:http;//www.gretheer.com');
        // 添加附件
        $mail->AddAttachment($tmp_file);
        // 发送
        $mail->Send();
} catch (Exception $e) {
        // 报错信息
        echo $mail->ErrorInfo;
}
?>

注意:
1. 可以在命令行下测试,最好使用普通用户测试。
php test.php
2. 用浏览器访问PHP文件测试时,如果没有接收到邮件,看是否是PHP文件权限问题,包括PHP文件内,include,require 等包含的文件的权限。

转载请注明原文地址:http://www.server110.com/postfix/201308/389.html


使用的时候出现的问题:

1.Could not access file: /tmp/test.jpg

解决方式:在/tmp目录下面放一张test.jsp的图片。

2.PHP Fatal error:  Class 'SMTP' not found in /develop/chisj/php/PHPMailer-master/class.phpmailer.php on line 1439

解决方式:在文件class.phpmailer.php中加载这个文件就可以了 require 'PHPMailerAutoload.php';

3.SMTP Error: Could not authenticate.

解决方式:

修改文件test.php的$mail->Username   = "username";和$mail->Password   = 'password';

4.如何确认可以发送邮件呢?

最后就是 $mail->AddAddress('abcdef@126.com', '忙碌的松鼠');修改为自己的邮箱,发送之后去自己的邮箱查收,如果在收件箱没有看到,有可能在垃圾箱里面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值