'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); } }