Files
ntonpower/extend/utils/Mailer.php
2026-10-14 14:00:29 +08:00

87 lines
2.3 KiB
PHP

<?php
namespace utils;
require_once root_path() . '/vendor/phpmailer/class.phpmailer.php';
require_once root_path() . '/vendor/phpmailer/class.smtp.php';
use PHPMailer;
class Mailer
{
const CHARTSET = 'UTF-8';
const PORT = 456;
const HOST = 'smtp.qq.com';
private $username = '735273025@qq.com';
private $password = 'gtxknrzhmctlbbfb';
private $fromAccount = '735273025@qq.com';
private $fromName = 'Jsasg';
private $toAccount = '735273025@qq.com';
private $toName = 'Jsasg';
private $subject = '';
private $body = '';
private $altBody = '';
/**
* 设置主题
* @param string $subject
*/
public function setSubject($subject)
{
$this->subject = $subject;
return $this;
}
/**
* 设置内容
* @param string $body
*/
public function setBody($body)
{
$this->body = $body;
return $this;
}
/**
* 设置不支持html时的内容
* @param string $string 内容
*/
public function setAltBody($string)
{
$this->altBody = $string;
return $this;
}
/**
* 发送
*/
public function send()
{
$success = false;
$mail = new PHPMailer(true);
try {
$mail->CharSet = self::CHARTSET; //设定邮件编码
$mail->SMTPDebug = 0; // 调试模式输出
$mail->isSMTP(); // 使用SMTP
$mail->Host = self::HOST; // SMTP服务器
$mail->SMTPAuth = true; // 允许 SMTP 认证
$mail->Username = $this->username; // SMTP 用户名 即邮箱的用户名
$mail->Password = $this->password; // SMTP 密码 部分邮箱是授权码(例如163邮箱)
$mail->SMTPSecure = 'ssl'; // 允许 TLS 或者ssl协议
$mail->Port = 465; // 服务器端口 25 或者 465 具体要看邮箱服务器支持
$mail->setFrom($this->fromAccount, $this->fromName); //发件人
$mail->addAddress($this->toAccount, $this->toName); // 收件人
$mail->isHTML(true);
$mail->Subject = $this->subject;
$mail->Body = $this->body;
$mail->AltBody = $this->altBody;
$success = $mail->send();
} catch (\Exception $error) {
exit($mail->ErrorInfo);
}
return $success;
}
}