PHP使用sendmail插件发送邮件
原文:https://blog.csdn.net/u011022919/article/details/80485140
1.从http://glob.com.au/sendmail ; 下载sendmail.zip
2.解压到C:下,例如C:PHPsendmail,最好短路径,长路径名有可能产生问题。
3.修改php.ini如下:
SMTP = smtp.163.com
sendmail_from = aa@163.com
windows配置(Linux同理)
sendmail_path = "C:PHPsendmail/sendmail.exe -t"
4.根据你自己的配置环境修改sendmail.ini。
smtp_server=smtp.163.com
auth_username=aa@163.com
auth_password=cs123123
force_sender=aa@163.com
5.重启apache
<?php
require_once('../phpMail/class.phpmailer.php');
require_once("../phpMail/class.smtp.php");
header("Content-type:text/html;charset=utf-8");
$mail = new PHPMailer();
$mail->CharSet ="UTF-8"; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置为 UTF-8
$mail->IsSMTP(); // 设定使用SMTP服务
$mail->SMTPAuth = true; // 启用 SMTP 验证功能
$mail->SMTPSecure = "ssl"; // SMTP 安全协议
$mail->Host = "smtp.163.com"; // SMTP 服务器
$mail->Port = 25; // SMTP服务器的端口号
$mail->Username = ""; // SMTP服务器用户名
$mail->Password = ""; // SMTP服务器密码
$mail->SetFrom('', 'zhouchenwen'); // 设置发件人地址和名称
$mail->AddReplyTo("","zhouchenwen");
// 设置邮件回复人地址和名称
$mail->Subject = 'test'; // 设置邮件标题
$mail->AltBody = "为了查看该邮件,请切换到支持 HTML 的邮件客户端";
// 可选项,向下兼容考虑
$mail->MsgHTML('test'); // 设置邮件内容
$mail->AddAddress('', "zhouchenwen");
//$mail->AddAttachment("images/phpmailer.gif"); // 附件
if(!$mail->Send()) {
echo "发送失败:" . $mail->ErrorInfo;
} else {
echo "恭喜,邮件发送成功!";
}
<?php
header("content-type:text/html;charset=utf-8");
error_reporting(-1);
ini_set("display_errors", 0);
/**
* 1,通过 set_error_handler() 函数设置用户自定义的错误处理程序,然后触发错误(通过 trigger_error())
* @param $errno
* @param $errmsg
* @param $file
* @param $line
*/
function customError($errno, $errmsg, $file, $line)
{
echo "<b>错误代码:</b>[{$errno}] {$errmsg}<br/>" . PHP_EOL;
echo "<b>错误行号:</b>{$file}文件中的第{$line}行<br/>" . PHP_EOL;
echo "<b>PHP版本:</b>" . PHP_VERSION . "(" . PHP_OS . ")" . "<br/>" . PHP_EOL;
}
set_error_handler("customError");
echo $test;
echo "<hr/>";
settype($var, "king");
echo "<hr/>";
// test();
trigger_error("这是个错误", E_USER_ERROR);
echo "<hr/>";
/**
*,2,在通过 set_error_handler() 函数更改错误处理函数之后对其进行恢复
* 自定义错误关闭,可以随时关随时开启;自定义错误也是有作用范围的
*/
restore_error_handler();
echo "contince...";
echo "<hr/>";
// 3,
set_error_handler("customError", E_ALL & E_NOTICE);
?>
<?php
/**
* 自定义封装错误函数
*/
class MyErrorHandler
{
public $message = "";
public $filename = "";
public $line = 0;
public $vars = array();
protected $_noticeLog = "G:\\notice\\noticelog.log";
public function __construct($message, $filename, $line, $vars)
{
$this->message = $message;
$this->filename = $filename;
$this->line = $line;
$this->vars = $vars;
}
public static function deal($errno, $errmsg, $filename, $line, $vars)
{
$self = new self($errmsg, $filename, $line, $vars);
switch ($errno) {
case E_USER_ERROR:
return $self->dealError();
break;
case E_USER_WARNING:
case E_WARNING:
return $self->dealWarning();
break;
case E_NOTICE:
case E_USER_NOTICE:
return $self->dealNotice();
break;
default:
return false;
}
}
public function dealError()
{
// ob_start — 打开输出控制缓冲
ob_start();
// debug_print_backtrace() 函数打印 PHP 回溯(backtrace)
debug_print_backtrace();
// ob_get_flush — 刷出(送出)缓冲区内容,以字符串形式返回内容,并关闭输出缓冲区
$backtrace = ob_get_flush();
$errorMsg = <<<EOF
出现了致命错误,如下:
产生错误的文件:{$this->filename}
产生错误的信息:{$this->message}
产生错误的行号:{$this->line}
追踪信息:{$backtrace}
EOF;
error_log($errorMsg, 1, "123456@qq.com");// 发送错误邮件到管理员
exit(1);
}
/**
* 警告错误
*/
public function dealWarning()
{
$errorMsg = <<<EOF
出现了警告错误,如下:
产生错误的文件:{$this->filename}
产生错误的信息:{$this->message}
产生错误的行号:{$this->line}
EOF;
error_log($errorMsg, 1, "123456@qq.com");// 发送错误邮件到管理员
}
/**
* 通知错误
*/
public function dealNotice()
{
$datetime = date("Y-m-d H:i:s", time());
$errorMsg = <<<EOF
出现了通知错误,如下:
产生通知的文件:{$this->filename}
产生通知的信息:{$this->message}
产生通知的行号:{$this->line}
产生通知的时间:{$datetime}
EOF;
// error_log — 发送错误信息到某个地方
error_log($errorMsg, 3, $this->_noticeLog);// 发送错误邮件到管理员
}
}
?>