PHPMailer类库的安装及使用
下载PHPMailer类库及使用demo:
地址:https://download.youkuaiyun.com/download/fenqing666/10681461
前置条件:
首先:在php.ini中去掉下面的两个分号
;extension=php_sockets.dll
;extension=php_openssl.dll
并重启PHP服务器检查两个服务是否开启。
其次:准备好一个可以放松邮件的邮箱账号
例如:wolfwangluo@163.com
具体如何使用上面的demo中有实例。
邮件发送代码:
/**
* 发送邮件
* @param $user_mail
* @param $title
* @param $content
* @return bool
* @throws src\Exception
*/
public function send($user_mail , $title, $content)
{
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet ="UTF-8";//编码
$mail->Debugoutput = 'html';// 支持HTML格式
$mail->Host = $this->Host;//HOST 地址
$mail->Port = $this->Port;//端口
$mail->SMTPAuth = true;
$mail->Username = $this->Username;//用户名
$mail->Password = $this->Password;//密码
$mail->SetFrom($this->FromNum, 'WOLF');//发件人地址, 发件人名称
$mail->AddAddress($user_mail);//收信人地址
if (!empty($type)) {
$mail->AddAttachment($type,''); // 添加附件,并指定名称
}
$mail->Subject = $title;//邮件标题
$str = "<h3>此消息来源于<a href='http://www.wofuwl.com'>沃夫网络科技</a>:</h3>";
$str .= $content;
$mail->MsgHTML($str);
if ($mail->Send()){
return true;
}else{
return $mail->errorMessage();
}
}
特别注意:类库引用到框架中时要根据当前框架调整命名空间