first version
This commit is contained in:
51
kirby/src/Email/Body.php
Executable file
51
kirby/src/Email/Body.php
Executable file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Email;
|
||||
|
||||
use Kirby\Toolkit\Properties;
|
||||
|
||||
/**
|
||||
* Representation of a an Email body
|
||||
* with a text and optional html version
|
||||
*
|
||||
* @package Kirby Email
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>,
|
||||
* Nico Hoffmann <nico@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Body
|
||||
{
|
||||
use Properties;
|
||||
|
||||
protected $html;
|
||||
protected $text;
|
||||
|
||||
public function __construct(array $props = [])
|
||||
{
|
||||
$this->setProperties($props);
|
||||
}
|
||||
|
||||
public function html()
|
||||
{
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
public function text(): string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
|
||||
protected function setHtml(string $html = null)
|
||||
{
|
||||
$this->html = $html;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setText(string $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
return $this;
|
||||
}
|
||||
}
|
187
kirby/src/Email/Email.php
Executable file
187
kirby/src/Email/Email.php
Executable file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Email;
|
||||
|
||||
use Kirby\Toolkit\Properties;
|
||||
use Kirby\Toolkit\V;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Wrapper for email libraries
|
||||
*
|
||||
* @package Kirby Email
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>,
|
||||
* Nico Hoffmann <nico@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class Email
|
||||
{
|
||||
use Properties;
|
||||
|
||||
protected $attachments;
|
||||
protected $body;
|
||||
protected $bcc;
|
||||
protected $cc;
|
||||
protected $from;
|
||||
protected $replyTo;
|
||||
protected $isSent = false;
|
||||
protected $subject;
|
||||
protected $to;
|
||||
protected $transport;
|
||||
|
||||
public function __construct(array $props = [], bool $debug = false)
|
||||
{
|
||||
$this->setProperties($props);
|
||||
|
||||
if ($debug === false) {
|
||||
$this->send();
|
||||
}
|
||||
}
|
||||
|
||||
public function attachments(): array
|
||||
{
|
||||
return $this->attachments;
|
||||
}
|
||||
|
||||
public function body(): Body
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function bcc(): array
|
||||
{
|
||||
return $this->bcc;
|
||||
}
|
||||
|
||||
public function cc(): array
|
||||
{
|
||||
return $this->cc;
|
||||
}
|
||||
|
||||
protected function defaultTransport(): array
|
||||
{
|
||||
return [
|
||||
'type' => 'mail'
|
||||
];
|
||||
}
|
||||
|
||||
public function from(): string
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
public function isHtml()
|
||||
{
|
||||
return $this->body()->html() !== null;
|
||||
}
|
||||
|
||||
public function isSent(): bool
|
||||
{
|
||||
return $this->isSent;
|
||||
}
|
||||
|
||||
public function replyTo(): string
|
||||
{
|
||||
return $this->replyTo;
|
||||
}
|
||||
|
||||
protected function resolveEmail($email = null, bool $multiple = true)
|
||||
{
|
||||
if ($email === null) {
|
||||
return $multiple === true ? [] : '';
|
||||
}
|
||||
|
||||
if (is_array($email) === false) {
|
||||
$email = [$email];
|
||||
}
|
||||
|
||||
foreach ($email as $address) {
|
||||
if (V::email($address) === false) {
|
||||
throw new Exception(sprintf('"%s" is not a valid email address', $address));
|
||||
}
|
||||
}
|
||||
|
||||
return $multiple === true ? $email : $email[0];
|
||||
}
|
||||
|
||||
public function send(): bool
|
||||
{
|
||||
return $this->isSent = true;
|
||||
}
|
||||
|
||||
protected function setAttachments($attachments = null)
|
||||
{
|
||||
$this->attachments = $attachments ?? [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setBody($body)
|
||||
{
|
||||
if (is_string($body) === true) {
|
||||
$body = ['text' => $body];
|
||||
}
|
||||
|
||||
$this->body = new Body($body);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setBcc($bcc = null)
|
||||
{
|
||||
$this->bcc = $this->resolveEmail($bcc);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setCc($cc = null)
|
||||
{
|
||||
$this->cc = $this->resolveEmail($cc);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setFrom(string $from)
|
||||
{
|
||||
$this->from = $this->resolveEmail($from, false);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setReplyTo(string $replyTo = null)
|
||||
{
|
||||
$this->replyTo = $this->resolveEmail($replyTo, false);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setSubject(string $subject)
|
||||
{
|
||||
$this->subject = $subject;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setTo($to)
|
||||
{
|
||||
$this->to = $this->resolveEmail($to);
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function setTransport($transport = null)
|
||||
{
|
||||
$this->transport = $transport;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function subject(): string
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
public function to(): array
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
public function transport(): array
|
||||
{
|
||||
return $this->transport ?? $this->defaultTransport();
|
||||
}
|
||||
}
|
76
kirby/src/Email/PHPMailer.php
Executable file
76
kirby/src/Email/PHPMailer.php
Executable file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Kirby\Email;
|
||||
|
||||
use PHPMailer\PHPMailer\PHPMailer as Mailer;
|
||||
|
||||
/**
|
||||
* Wrapper for PHPMailer library
|
||||
*
|
||||
* @package Kirby Email
|
||||
* @author Bastian Allgeier <bastian@getkirby.com>,
|
||||
* Nico Hoffmann <nico@getkirby.com>
|
||||
* @link http://getkirby.com
|
||||
* @copyright Bastian Allgeier
|
||||
* @license MIT
|
||||
*/
|
||||
class PHPMailer extends Email
|
||||
{
|
||||
public function send(bool $debug = false): bool
|
||||
{
|
||||
$mailer = new Mailer(true);
|
||||
|
||||
// set sender's address
|
||||
$mailer->setFrom($this->from());
|
||||
|
||||
// optional reply-to address
|
||||
if ($replyTo = $this->replyTo()) {
|
||||
$mailer->addReplyTo($replyTo);
|
||||
}
|
||||
|
||||
// add (multiple) recepient, CC & BCC addresses
|
||||
foreach ($this->to() as $to) {
|
||||
$mailer->addAddress($to);
|
||||
}
|
||||
foreach ($this->cc() as $cc) {
|
||||
$mailer->addCC($cc);
|
||||
}
|
||||
foreach ($this->bcc() as $bcc) {
|
||||
$mailer->addBCC($bcc);
|
||||
}
|
||||
|
||||
$mailer->Subject = $this->subject();
|
||||
$mailer->CharSet = 'UTF-8';
|
||||
|
||||
// set body according to html/text
|
||||
if ($this->isHtml()) {
|
||||
$mailer->isHTML(true);
|
||||
$mailer->Body = $this->body()->html();
|
||||
$mailer->AltBody = $this->body()->text();
|
||||
} else {
|
||||
$mailer->Body = $this->body()->text();
|
||||
}
|
||||
|
||||
// add attachments
|
||||
foreach ($this->attachments() as $attachment) {
|
||||
$mailer->addAttachment($attachment);
|
||||
}
|
||||
|
||||
// smtp transport settings
|
||||
if (($this->transport()['type'] ?? 'mail') === 'smtp') {
|
||||
$mailer->isSMTP();
|
||||
$mailer->Host = $this->transport()['host'] ?? null;
|
||||
$mailer->SMTPAuth = $this->transport()['auth'] ?? false;
|
||||
$mailer->Username = $this->transport()['username'] ?? null;
|
||||
$mailer->Password = $this->transport()['password'] ?? null;
|
||||
$mailer->SMTPSecure = $this->transport()['security'] ?? 'ssl';
|
||||
$mailer->Port = $this->transport()['port'] ?? null;
|
||||
}
|
||||
|
||||
if ($debug === true) {
|
||||
return $this->isSent = true;
|
||||
}
|
||||
|
||||
return $this->isSent = $mailer->send();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user