94 lines
2.4 KiB
PHP
94 lines
2.4 KiB
PHP
<?php
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\SMTP;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
require_once __DIR__ . '/PHPMailer/Exception.php';
|
|
require_once __DIR__ . '/PHPMailer/PHPMailer.php';
|
|
require_once __DIR__ . '/PHPMailer/SMTP.php';
|
|
|
|
class SendMail {
|
|
private $to;
|
|
private $toName;
|
|
private $subject;
|
|
private $body;
|
|
private $reply;
|
|
private $replyName;
|
|
|
|
private $config;
|
|
|
|
public function __construct() {
|
|
$this->config = include __DIR__ . '/../config/config.php';
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @param string|null $name
|
|
*/
|
|
public function setTo($email, $name = null) {
|
|
$this->to = $email;
|
|
$this->toName = $name;
|
|
}
|
|
|
|
/**
|
|
* @param string $email
|
|
* @param string|null $name
|
|
*/
|
|
public function setReplyTo($email, $name = null) {
|
|
$this->reply = $email;
|
|
$this->replyName = $name;
|
|
}
|
|
|
|
/**
|
|
* @param string $subject
|
|
*/
|
|
public function setSubject($subject) {
|
|
$this->subject = $subject;
|
|
}
|
|
|
|
/**
|
|
* @param string $body
|
|
*/
|
|
public function setBody($body) {
|
|
$this->body = $body;
|
|
}
|
|
|
|
public function send() {
|
|
/* @var PHPMailer $mail */
|
|
$mail = new PHPMailer(true);
|
|
|
|
$mail->isSMTP();
|
|
$mail->isHTML();
|
|
//$mail->SMTPDebug = SMTP::DEBUG_LOWLEVEL;
|
|
$mail->SMTPDebug = SMTP::DEBUG_OFF;
|
|
$mail->SMTPAuth = true;
|
|
$mail->SMTPOptions = [
|
|
'ssl' => [
|
|
'verify_peer' => false,
|
|
'verify_peer_name' => false,
|
|
'allow_self_signed' => true
|
|
]
|
|
];
|
|
$mail->Host = $this->config['smtp']['host'];
|
|
$mail->Port = $this->config['smtp']['port'];
|
|
$mail->Username = $this->config['smtp']['username'];
|
|
$mail->Password = $this->config['smtp']['password'];
|
|
//$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
|
$mail->addAddress($this->to, $this->toName);
|
|
if($this->reply) {
|
|
$mail->addReplyTo($this->reply, $this->replyName);
|
|
}
|
|
$mail->setFrom($this->config['smtp']['username']);
|
|
$mail->Subject = $this->subject;
|
|
$mail->Body = htmlentities($this->body, ENT_QUOTES);
|
|
|
|
try {
|
|
return $mail->send();
|
|
} catch(\Exception $ex) {
|
|
var_dump($ex);
|
|
return false;
|
|
}
|
|
}
|
|
}
|