Files
KI/common/Implementation/Message.php

101 lines
2.5 KiB
PHP

<?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];
}
}