一、使用PHP内置的mail函数发邮件有诸多限制:
For the Mail functions to be available, PHP must have access to the sendmail binary
on your system during compile time. If you use another mail program,
such as qmail or postfix, be sure to use the appropriate sendmail
wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
而且邮件服务器对发邮件有验证要求的时候,也无法寄出。
在参考了网上诸多实例,并根据RFC821规范,原理如下:
与服务器建立Socket连接->向服务器端口发送相关命令-> 与服务器交互->Action OK
根据以上原理,首先要建立Socket连接,可参考PHP手册,这里使用Pear类库:Net_Smtp、Net_Socket、Mail这三个类库,首
先Pear install
(--alldeps)安装三个Pear类库,记得要在PHP.ini里检查一下include_path有没有把pear的路径包含进来。
require_once 'Mail.php';
$conf['mail'] = array(
'host' => '**.**.**.**', //smtp服务器地址,可以用ip地址或者域名
'auth' =>true, //true表示smtp服务器需要验证,false代码不需要
'username' => '***', //用户名
'password' => '******' //密码
);
$headers['From'] = ; //发信地址
$headers['To'] = ; //收信地址
$headers['Subject'] = 'test mail send by php'; //邮件标题
$mail_object = &Mail::factory('smtp', $conf['mail']);
$body = 'hello world!!!'; //邮件正文
$mail_res = $mail_object->send($headers['To'], $headers, $body); //发送
if($mail_res) { //检测错误
print_r($mail_res);
echo "send mail is OK";
}else{
echo "send mail is something wrong!";
}
阅读(905) | 评论(0) | 转发(0) |