Desacople e implementacion de email
This commit is contained in:
102
common/Implementation/Mailer.php
Normal file
102
common/Implementation/Mailer.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace ProVM\KI\Common\Implementation;
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer as BaseMailer;
|
||||
use PHPMailer\PHPMailer\SMTP;
|
||||
use ProVM\KI\Common\Alias\Mailer as MailerInterface;
|
||||
use ProVM\KI\Common\Alias\Message;
|
||||
|
||||
class Mailer implements MailerInterface {
|
||||
protected $host;
|
||||
protected $port;
|
||||
protected $user;
|
||||
protected $is_secure = false;
|
||||
public function __construct(array $settings) {
|
||||
if (!isset($settings['host'])) {
|
||||
throw new \Exception('Host is missing in ' . get_called_class());
|
||||
}
|
||||
$this->host = $settings['host'];
|
||||
if (!isset($settings['username'])) {
|
||||
throw new \Exception('User name is missing in ' . get_called_class());
|
||||
}
|
||||
$this->user = (object) ['name' => null, 'password' => null];
|
||||
$this->user->name = $settings['username'];
|
||||
if (!isset($settings['password'])) {
|
||||
throw new \Exception('User password is missing in ' . get_called_class());
|
||||
}
|
||||
$this->user->password = $settings['password'];
|
||||
$this->port = 25;
|
||||
if (isset($settings['secure']) and $settings['secure'] == 'ssl') {
|
||||
$this->is_secure = true;
|
||||
$this->port = 465;
|
||||
}
|
||||
if (isset($settings['port'])) {
|
||||
$this->port = $settings['port'];
|
||||
}
|
||||
}
|
||||
public function isSecure(): bool {
|
||||
return $this->is_secure;
|
||||
}
|
||||
public function send(Message $message): bool {
|
||||
$mailer = new BaseMailer();
|
||||
$mailer->isSMTP();
|
||||
//$mailer->SMTPDebug = SMTP::DEBUG_SERVER;
|
||||
$mailer->Host = $this->host;
|
||||
$mailer->SMTPAuth = true;
|
||||
$mailer->Username = $this->user->name;
|
||||
$mailer->Password = $this->user->password;
|
||||
$mailer->Port = $this->port;
|
||||
if ($this->isSecure()) {
|
||||
$mailer->SMTPSecure = BaseMailer::ENCRYPTION_STARTTLS;
|
||||
}
|
||||
|
||||
try {
|
||||
$from = $message->getFrom();
|
||||
if ($from->name !== null) {
|
||||
$mailer->setFrom($from->email, $from->name);
|
||||
} else {
|
||||
$mailer->setFrom($from->email);
|
||||
}
|
||||
foreach ($message->getTo() as $to) {
|
||||
if ($to->name !== null) {
|
||||
$mailer->addAddress($to->email, $to->name);
|
||||
} else {
|
||||
$mailer->addAddress($to->email);
|
||||
}
|
||||
}
|
||||
$ccs = $message->getCc();
|
||||
if (!empty($ccs)) {
|
||||
foreach ($ccs as $cc) {
|
||||
if ($cc->name !== null) {
|
||||
$mailer->addCC($cc->email, $cc->name);
|
||||
} else {
|
||||
$mailer->addCC($cc->email);
|
||||
}
|
||||
}
|
||||
}
|
||||
$bccs = $message->getBcc();
|
||||
if (!empty($bccs)) {
|
||||
foreach ($bccs as $bcc) {
|
||||
if ($bcc->name !== null) {
|
||||
$mailer->addBCC($bcc->email, $bcc->name);
|
||||
} else {
|
||||
$mailer->addBCC($bcc->email);
|
||||
}
|
||||
}
|
||||
}
|
||||
$mailer->Subject = $message->getSubject();
|
||||
if ($message->isHtml()) {
|
||||
$mailer->Body = $message->getHtmlBody();
|
||||
$mailer->isHTML();
|
||||
$mailer->AltBody = $message->getBody();
|
||||
} else {
|
||||
$mailer->Body = $message->getBody();
|
||||
}
|
||||
|
||||
$mailer->send();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
100
common/Implementation/Message.php
Normal file
100
common/Implementation/Message.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace ProVM\KI\Common\Implementation;
|
||||
|
||||
use ProVM\KI\Common\Alias\Message as MessageInterface;
|
||||
|
||||
class Message implements MessageInterface {
|
||||
protected $from;
|
||||
public function setFrom(string $email, string $name = null): Message {
|
||||
$this->from = $this->formatEmail($email, $name);
|
||||
return $this;
|
||||
}
|
||||
public function getFrom() {
|
||||
return $this->from;
|
||||
}
|
||||
protected $reply_to;
|
||||
public function addReplyTo(string $email, string $name = null): Message {
|
||||
if ($this->reply_to === null) {
|
||||
$this->reply_to = [];
|
||||
}
|
||||
$this->reply_to []= $this->formatEmail($email, $name);
|
||||
return $this;
|
||||
}
|
||||
public function getReplyTo() {
|
||||
return $this->reply_to;
|
||||
}
|
||||
protected $subject;
|
||||
public function setSubject(string $subject): Message {
|
||||
$this->subject = $subject;
|
||||
return $this;
|
||||
}
|
||||
public function getSubject(): string {
|
||||
return $this->subject;
|
||||
}
|
||||
protected $to;
|
||||
public function addTo(string $email, string $name = null): Message {
|
||||
if ($this->to === null) {
|
||||
$this->to = [];
|
||||
}
|
||||
$this->to []= $this->formatEmail($email, $name);
|
||||
return $this;
|
||||
}
|
||||
public function getTo(): array {
|
||||
return $this->to;
|
||||
}
|
||||
protected $cc;
|
||||
public function addCc(string $email, string $name = null): Message {
|
||||
if ($this->cc === null) {
|
||||
$this->cc = [];
|
||||
}
|
||||
$this->cc []= $this->formatEmail($email, $name);
|
||||
return $this;
|
||||
}
|
||||
public function getCc() {
|
||||
return $this->cc;
|
||||
}
|
||||
protected $bcc;
|
||||
public function addBcc(string $email, string $name = null): Message {
|
||||
if ($this->bcc === null) {
|
||||
$this->bcc = [];
|
||||
}
|
||||
$this->bcc []= $this->formatEmail($email, $name);
|
||||
return $this;
|
||||
}
|
||||
public function getBcc() {
|
||||
return $this->bcc;
|
||||
}
|
||||
protected $body;
|
||||
public function setBody(string $body): Message {
|
||||
$this->body = $body;
|
||||
return $this;
|
||||
}
|
||||
public function getBody(): string {
|
||||
return $this->body;
|
||||
}
|
||||
protected $html;
|
||||
protected $is_html = false;
|
||||
public function setHtmlBody(string $body): Message {
|
||||
$this->html = $body;
|
||||
$this->is_html = true;
|
||||
return $this;
|
||||
}
|
||||
public function getHtmlBody(): string {
|
||||
return $this->html;
|
||||
}
|
||||
public function isHtml(): bool {
|
||||
return $this->is_html;
|
||||
}
|
||||
|
||||
protected function formatEmail(string $email, string $name = null) {
|
||||
if (!$name && preg_match('#^(.+) +<(.*)>$#D', $email, $matches)) {
|
||||
[, $name, $email] = $matches;
|
||||
$name = stripslashes($name);
|
||||
$tmp = substr($name, 1, -1);
|
||||
if ($name === '"' . $tmp . '"') {
|
||||
$name = $tmp;
|
||||
}
|
||||
}
|
||||
return (object) ['name' => $name, 'email' => $email];
|
||||
}
|
||||
}
|
7
common/Implementation/View.php
Normal file
7
common/Implementation/View.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
namespace ProVM\KI\Common\Implementation;
|
||||
|
||||
use Slim\Views\Blade as BaseView;
|
||||
use ProVM\KI\Common\Alias\View as ViewInterface;
|
||||
|
||||
class View extends BaseView implements ViewInterface {}
|
Reference in New Issue
Block a user