Files
2022-09-29 14:30:16 +08:00

104 lines
3.1 KiB
PHP

<?php
require_once(dirname(dirname(__FILE__)).'/third_party/PHPMailer.php');
/**
* 发送邮件封装类
* Created by PhpStorm.
* User: xuxb
* Date: 2018/9/12
* Time: 11:11
*/
class Mymailer
{
private $host;//地址
private $port;//发送邮件端口号
private $username;//登录的账号
private $pwd;//登录的密码
private $debug;
private $mailer;
private $fromname;
private $config = array(
'host' => 'smtp.163.com',
'port' => '25',
'username' => '18350451617@163.com',
'pwd' => 'lin2821808',
'fromname' => '小鱼网',
);
public function __construct($params = array())
{
$params && $this->init($params);
}
/**
* 初始化
* @param array $params 未设置默认server配置mail数组第一项
* @param boolean $debug
*/
public function init($params = array(), $debug = false){
if($params){
$this->host = $params['host'];
$this->port = $params['port'];
$this->username = $params['username'];
$this->pwd = $params['pwd'];
$this->fromname = $params['fromname'];
$this->debug = $debug;
} else {
$params = $this->config;
$this->host = $params['host'];
$this->port = $params['port'];
$this->username = $params['username'];
$this->pwd = $params['pwd'];
$this->fromname = $params['fromname'];
}
$mailer = new PHPMailer();
/* Server Settings */
$mailer->IsSMTP(); // 使用 SMTP 方式发送邮件
$mailer->SMTPAuth = true; //启用SMTP认证
$mailer->Host = $this->host; //SMTP服务器 以163邮箱为例子
$mailer->Port = $this->port; //邮件发送端口,163的是25,设置ssl连接smtp服务器的远程服务器端口号465
// $mailer->SMTPSecure = 'ssl';
$mailer->SMTPDebug = $this->debug;// 是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
/* Account Settings */
$mailer->Username = $this->username;
$mailer->Password = $this->pwd;
$mailer->From = $this->username;
/* Content Setting */
$mailer->IsHTML(true); //支持html格式内容
$mailer->CharSet = 'UTF-8';
$this->mailer = $mailer;
}
/**
* 添加附件
* @param $path (文件路径
* @param string $name (指定名称
*/
public function add_attachment($path, $name=''){
$this->mailer->AddAttachment($path, $name);
}
/**
* 发送邮件
* @param $email (接收邮件地址
* @param $title (邮件标题
* @param $content (邮件内容
* @return bool
*/
public function send($email, $title, $content){
$this->mailer->FromName = $this->fromname;
$this->mailer->addAddress($email);
$this->mailer->Subject = $title;
$this->mailer->Body = $content;
return (bool)$this->mailer->send(); // 发送邮件
}
public function error(){
var_dump($this->mailer->ErrorInfo);
}
}