<?php
require_once 'Mail.php';
$conf['mail'] = array(
'host' => 'xx.xx.xx.xx', //smtp服务器地址,可以用ip地址或者域名
'auth' => true, //true表示smtp服务器需要验证,false代码不需要
'username' => 'tester', //用户名
'password' => 'retset' //密码
);
/***
* 使用$headers数组,可以定义邮件头的内容,比如使用$headers['Reply-To']可以定义回复地址
* 通过这种方式,可以很方便的定制待发送邮件的邮件头
***/
$headers['From'] = 'tester@domain.com'; //发信地址
$headers['To'] = 'tester@domain.com'; //收信地址
$headers['Subject'] = 'test mail send by php'; //邮件标题
$mail_object = &Mail::factory('smtp', $conf['mail']);
$body = <<< MSG //邮件正文
hello world!!!
MSG;
$mail_res = $mail_object->send($headers['To'], $headers, $body); //发送
if( Mail::isError($mail_res) ){ //检测错误
die($mail_res->getMessage());
}
?>
require_once 'Mail.php';
$conf['mail'] = array(
'host' => 'xx.xx.xx.xx', //smtp服务器地址,可以用ip地址或者域名
'auth' => true, //true表示smtp服务器需要验证,false代码不需要
'username' => 'tester', //用户名
'password' => 'retset' //密码
);
/***
* 使用$headers数组,可以定义邮件头的内容,比如使用$headers['Reply-To']可以定义回复地址
* 通过这种方式,可以很方便的定制待发送邮件的邮件头
***/
$headers['From'] = 'tester@domain.com'; //发信地址
$headers['To'] = 'tester@domain.com'; //收信地址
$headers['Subject'] = 'test mail send by php'; //邮件标题
$mail_object = &Mail::factory('smtp', $conf['mail']);
$body = <<< MSG //邮件正文
hello world!!!
MSG;
$mail_res = $mail_object->send($headers['To'], $headers, $body); //发送
if( Mail::isError($mail_res) ){ //检测错误
die($mail_res->getMessage());
}
?>
此博客展示了使用PHP实现邮件发送的代码。通过配置SMTP服务器地址、用户名、密码等信息,定义邮件头和正文内容,利用Mail类的相关方法发送邮件,并对发送结果进行错误检测。

2万+

被折叠的 条评论
为什么被折叠?



