|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Mail;
|
|
|
|
|
|
|
|
|
|
use App\Services\Config;
|
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
|
|
|
|
|
|
|
|
class Smtp extends Base
|
|
|
|
|
{
|
|
|
|
|
public function getConfig()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
"host" => Config::get('smtp_host'),
|
|
|
|
|
"username" => Config::get('smtp_username'),
|
|
|
|
|
"port" => Config::get('smtp_port'),
|
|
|
|
|
"sender" => Config::get('smtp_sender'),
|
|
|
|
|
"name" => Config::get('smtp_name'),
|
|
|
|
|
"passsword" => Config::get('smtp_passsword')
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function initializeMailer($config)
|
|
|
|
|
{
|
|
|
|
|
$mail = new PHPMailer;
|
|
|
|
|
//$mail->SMTPDebug = 3;
|
|
|
|
|
$mail->isSMTP();
|
|
|
|
|
$mail->Host = $config['host'];
|
|
|
|
|
$mail->SMTPAuth = true;
|
|
|
|
|
$mail->Username = $config['username'];
|
|
|
|
|
$mail->Password = $config['passsword'];
|
|
|
|
|
if (Config::get('smtp_ssl') == 'true') {
|
|
|
|
|
if(Config::get('smtp_port')=='587'){
|
|
|
|
|
$mail->SMTPSecure='tls';
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
$mail->SMTPSecure='ssl';
|
|
|
|
|
$mail->SMTPOptions = array(
|
|
|
|
|
'ssl' => array(
|
|
|
|
|
'verify_peer' => true,
|
|
|
|
|
'verify_peer_name' => true,
|
|
|
|
|
'allow_self_signed' => false
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$mail->Port = $config['port'];
|
|
|
|
|
$mail->Timeout = 15;
|
|
|
|
|
$mail->Timelimit = 20;
|
|
|
|
|
$mail->setFrom($config['sender'], $config['name']);
|
|
|
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
|
return $mail;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send($to, $subject, $text, $files)
|
|
|
|
|
{
|
|
|
|
|
$mail = $this->initializeMailer($this->getConfig());
|
|
|
|
|
$mail->addAddress($to); // Add a recipient
|
|
|
|
|
$mail->isHTML(true);
|
|
|
|
|
$mail->Subject = $subject;
|
|
|
|
|
$mail->Body = $text;
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
$mail->addAttachment($file);
|
|
|
|
|
}
|
|
|
|
|
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
|
|
|
|
|
return $mail->send();
|
|
|
|
|
}
|
|
|
|
|
}
|