|
@@ -2,39 +2,23 @@
|
|
|
|
|
|
namespace Mailgun\Messages;
|
|
namespace Mailgun\Messages;
|
|
|
|
|
|
-use Mailgun\Constants\Api;
|
|
|
|
-use Mailgun\Constants\ExceptionMessages;
|
|
|
|
use Mailgun\Messages\Exceptions\InvalidParameter;
|
|
use Mailgun\Messages\Exceptions\InvalidParameter;
|
|
use Mailgun\Messages\Exceptions\TooManyParameters;
|
|
use Mailgun\Messages\Exceptions\TooManyParameters;
|
|
|
|
+use Mailgun\Messages\Exceptions\InvalidParameterType;
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ This class is used for composing a properly formed
|
|
|
|
+ message object. Dealing with arrays can be cumbersome,
|
|
|
|
+ this class makes the process easier. See the official
|
|
|
|
+ documentation for usage instructions.
|
|
|
|
+*/
|
|
|
|
|
|
-/**
|
|
|
|
- * This class is used for composing a properly formed
|
|
|
|
- * message object. Dealing with arrays can be cumbersome,
|
|
|
|
- * this class makes the process easier. See the official
|
|
|
|
- * documentation (link below) for usage instructions.
|
|
|
|
- *
|
|
|
|
- * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
|
|
|
|
- */
|
|
|
|
class MessageBuilder
|
|
class MessageBuilder
|
|
{
|
|
{
|
|
- /**
|
|
|
|
- * @var array
|
|
|
|
- */
|
|
|
|
- protected $message = array();
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * @var array
|
|
|
|
- */
|
|
|
|
|
|
+ protected $message = array();
|
|
protected $variables = array();
|
|
protected $variables = array();
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @var array
|
|
|
|
- */
|
|
|
|
protected $files = array();
|
|
protected $files = array();
|
|
-
|
|
|
|
- /**
|
|
|
|
- * @var array
|
|
|
|
- */
|
|
|
|
protected $counters = array(
|
|
protected $counters = array(
|
|
'recipients' => array(
|
|
'recipients' => array(
|
|
'to' => 0,
|
|
'to' => 0,
|
|
@@ -49,12 +33,6 @@ class MessageBuilder
|
|
)
|
|
)
|
|
);
|
|
);
|
|
|
|
|
|
- /**
|
|
|
|
- * @param array $params
|
|
|
|
- * @param string $key
|
|
|
|
- * @param mixed $default
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
protected function safeGet($params, $key, $default)
|
|
protected function safeGet($params, $key, $default)
|
|
{
|
|
{
|
|
if (array_key_exists($key, $params)) {
|
|
if (array_key_exists($key, $params)) {
|
|
@@ -64,10 +42,6 @@ class MessageBuilder
|
|
return $default;
|
|
return $default;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param array $params
|
|
|
|
- * @return mixed|string
|
|
|
|
- */
|
|
|
|
protected function getFullName($params)
|
|
protected function getFullName($params)
|
|
{
|
|
{
|
|
if (array_key_exists("first", $params)) {
|
|
if (array_key_exists("first", $params)) {
|
|
@@ -80,11 +54,6 @@ class MessageBuilder
|
|
return $this->safeGet($params, "full_name", "");
|
|
return $this->safeGet($params, "full_name", "");
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array $variables
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
protected function parseAddress($address, $variables)
|
|
protected function parseAddress($address, $variables)
|
|
{
|
|
{
|
|
if (!is_array($variables)) {
|
|
if (!is_array($variables)) {
|
|
@@ -98,11 +67,6 @@ class MessageBuilder
|
|
return $address;
|
|
return $address;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $headerName
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array $variables
|
|
|
|
- */
|
|
|
|
protected function addRecipient($headerName, $address, $variables)
|
|
protected function addRecipient($headerName, $address, $variables)
|
|
{
|
|
{
|
|
$compiledAddress = $this->parseAddress($address, $variables);
|
|
$compiledAddress = $this->parseAddress($address, $variables);
|
|
@@ -119,59 +83,36 @@ class MessageBuilder
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array|null $variables
|
|
|
|
- * @return mixed
|
|
|
|
- * @throws TooManyParameters
|
|
|
|
- */
|
|
|
|
public function addToRecipient($address, $variables = null)
|
|
public function addToRecipient($address, $variables = null)
|
|
{
|
|
{
|
|
- if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) {
|
|
|
|
- throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
|
|
|
|
|
|
+ if ($this->counters['recipients']['to'] > RECIPIENT_COUNT_LIMIT) {
|
|
|
|
+ throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
|
|
}
|
|
}
|
|
$this->addRecipient("to", $address, $variables);
|
|
$this->addRecipient("to", $address, $variables);
|
|
|
|
|
|
return end($this->message['to']);
|
|
return end($this->message['to']);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array|null $variables
|
|
|
|
- * @return mixed
|
|
|
|
- * @throws TooManyParameters
|
|
|
|
- */
|
|
|
|
public function addCcRecipient($address, $variables = null)
|
|
public function addCcRecipient($address, $variables = null)
|
|
{
|
|
{
|
|
- if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) {
|
|
|
|
- throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
|
|
|
|
|
|
+ if ($this->counters['recipients']['cc'] > RECIPIENT_COUNT_LIMIT) {
|
|
|
|
+ throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
|
|
}
|
|
}
|
|
$this->addRecipient("cc", $address, $variables);
|
|
$this->addRecipient("cc", $address, $variables);
|
|
|
|
|
|
return end($this->message['cc']);
|
|
return end($this->message['cc']);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array|null $variables
|
|
|
|
- * @return mixed
|
|
|
|
- * @throws TooManyParameters
|
|
|
|
- */
|
|
|
|
public function addBccRecipient($address, $variables = null)
|
|
public function addBccRecipient($address, $variables = null)
|
|
{
|
|
{
|
|
- if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) {
|
|
|
|
- throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
|
|
|
|
|
|
+ if ($this->counters['recipients']['bcc'] > RECIPIENT_COUNT_LIMIT) {
|
|
|
|
+ throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
|
|
}
|
|
}
|
|
$this->addRecipient("bcc", $address, $variables);
|
|
$this->addRecipient("bcc", $address, $variables);
|
|
|
|
|
|
return end($this->message['bcc']);
|
|
return end($this->message['bcc']);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array|null $variables
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
public function setFromAddress($address, $variables = null)
|
|
public function setFromAddress($address, $variables = null)
|
|
{
|
|
{
|
|
$this->addRecipient("from", $address, $variables);
|
|
$this->addRecipient("from", $address, $variables);
|
|
@@ -179,11 +120,6 @@ class MessageBuilder
|
|
return $this->message['from'];
|
|
return $this->message['from'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $address
|
|
|
|
- * @param array|null $variables
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
public function setReplyToAddress($address, $variables = null)
|
|
public function setReplyToAddress($address, $variables = null)
|
|
{
|
|
{
|
|
$this->addRecipient("h:reply-to", $address, $variables);
|
|
$this->addRecipient("h:reply-to", $address, $variables);
|
|
@@ -191,11 +127,7 @@ class MessageBuilder
|
|
return $this->message['h:reply-to'];
|
|
return $this->message['h:reply-to'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $subject
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
- public function setSubject($subject = "")
|
|
|
|
|
|
+ public function setSubject($subject = null)
|
|
{
|
|
{
|
|
if ($subject == null || $subject == "") {
|
|
if ($subject == null || $subject == "") {
|
|
$subject = " ";
|
|
$subject = " ";
|
|
@@ -205,11 +137,6 @@ class MessageBuilder
|
|
return $this->message['subject'];
|
|
return $this->message['subject'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $headerName
|
|
|
|
- * @param mixed $headerData
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
public function addCustomHeader($headerName, $headerData)
|
|
public function addCustomHeader($headerName, $headerData)
|
|
{
|
|
{
|
|
if (!preg_match("/^h:/i", $headerName)) {
|
|
if (!preg_match("/^h:/i", $headerName)) {
|
|
@@ -220,10 +147,6 @@ class MessageBuilder
|
|
return $this->message[$headerName];
|
|
return $this->message[$headerName];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $textBody
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setTextBody($textBody)
|
|
public function setTextBody($textBody)
|
|
{
|
|
{
|
|
if ($textBody == null || $textBody == "") {
|
|
if ($textBody == null || $textBody == "") {
|
|
@@ -234,10 +157,6 @@ class MessageBuilder
|
|
return $this->message['text'];
|
|
return $this->message['text'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $htmlBody
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setHtmlBody($htmlBody)
|
|
public function setHtmlBody($htmlBody)
|
|
{
|
|
{
|
|
if ($htmlBody == null || $htmlBody == "") {
|
|
if ($htmlBody == null || $htmlBody == "") {
|
|
@@ -248,11 +167,6 @@ class MessageBuilder
|
|
return $this->message['html'];
|
|
return $this->message['html'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $attachmentPath
|
|
|
|
- * @param string|null $attachmentName
|
|
|
|
- * @return bool
|
|
|
|
- */
|
|
|
|
public function addAttachment($attachmentPath, $attachmentName = null)
|
|
public function addAttachment($attachmentPath, $attachmentName = null)
|
|
{
|
|
{
|
|
if (isset($this->files["attachment"])) {
|
|
if (isset($this->files["attachment"])) {
|
|
@@ -273,11 +187,6 @@ class MessageBuilder
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $inlineImagePath
|
|
|
|
- * @param string|null $inlineImageName
|
|
|
|
- * @throws InvalidParameter
|
|
|
|
- */
|
|
|
|
public function addInlineImage($inlineImagePath, $inlineImageName = null)
|
|
public function addInlineImage($inlineImagePath, $inlineImageName = null)
|
|
{
|
|
{
|
|
if (preg_match("/^@/", $inlineImagePath)) {
|
|
if (preg_match("/^@/", $inlineImagePath)) {
|
|
@@ -298,14 +207,10 @@ class MessageBuilder
|
|
|
|
|
|
return true;
|
|
return true;
|
|
} else {
|
|
} else {
|
|
- throw new InvalidParameter(ExceptionMessages::INVALID_PARAMETER_INLINE);
|
|
|
|
|
|
+ throw new InvalidParameter(INVALID_PARAMETER_INLINE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param boolean $testMode
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setTestMode($testMode)
|
|
public function setTestMode($testMode)
|
|
{
|
|
{
|
|
if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
|
|
if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
|
|
@@ -318,14 +223,9 @@ class MessageBuilder
|
|
return $this->message['o:testmode'];
|
|
return $this->message['o:testmode'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string|int $campaignId
|
|
|
|
- * @return string|int
|
|
|
|
- * @throws TooManyParameters
|
|
|
|
- */
|
|
|
|
public function addCampaignId($campaignId)
|
|
public function addCampaignId($campaignId)
|
|
{
|
|
{
|
|
- if ($this->counters['attributes']['campaign_id'] < Api::CAMPAIGN_ID_LIMIT) {
|
|
|
|
|
|
+ if ($this->counters['attributes']['campaign_id'] < CAMPAIGN_ID_LIMIT) {
|
|
if (isset($this->message['o:campaign'])) {
|
|
if (isset($this->message['o:campaign'])) {
|
|
array_push($this->message['o:campaign'], $campaignId);
|
|
array_push($this->message['o:campaign'], $campaignId);
|
|
} else {
|
|
} else {
|
|
@@ -335,17 +235,13 @@ class MessageBuilder
|
|
|
|
|
|
return $this->message['o:campaign'];
|
|
return $this->message['o:campaign'];
|
|
} else {
|
|
} else {
|
|
- throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_CAMPAIGNS);
|
|
|
|
|
|
+ throw new TooManyParameters(TOO_MANY_PARAMETERS_CAMPAIGNS);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $tag
|
|
|
|
- * @throws TooManyParameters
|
|
|
|
- */
|
|
|
|
public function addTag($tag)
|
|
public function addTag($tag)
|
|
{
|
|
{
|
|
- if ($this->counters['attributes']['tag'] < Api::TAG_LIMIT) {
|
|
|
|
|
|
+ if ($this->counters['attributes']['tag'] < TAG_LIMIT) {
|
|
if (isset($this->message['o:tag'])) {
|
|
if (isset($this->message['o:tag'])) {
|
|
array_push($this->message['o:tag'], $tag);
|
|
array_push($this->message['o:tag'], $tag);
|
|
} else {
|
|
} else {
|
|
@@ -355,14 +251,10 @@ class MessageBuilder
|
|
|
|
|
|
return $this->message['o:tag'];
|
|
return $this->message['o:tag'];
|
|
} else {
|
|
} else {
|
|
- throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_TAGS);
|
|
|
|
|
|
+ throw new TooManyParameters(TOO_MANY_PARAMETERS_TAGS);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param boolean $enabled
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
public function setDkim($enabled)
|
|
public function setDkim($enabled)
|
|
{
|
|
{
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
@@ -375,10 +267,6 @@ class MessageBuilder
|
|
return $this->message["o:dkim"];
|
|
return $this->message["o:dkim"];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param boolean $enabled
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setOpenTracking($enabled)
|
|
public function setOpenTracking($enabled)
|
|
{
|
|
{
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
@@ -391,10 +279,6 @@ class MessageBuilder
|
|
return $this->message['o:tracking-opens'];
|
|
return $this->message['o:tracking-opens'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param boolean $enabled
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setClickTracking($enabled)
|
|
public function setClickTracking($enabled)
|
|
{
|
|
{
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
|
@@ -409,17 +293,12 @@ class MessageBuilder
|
|
return $this->message['o:tracking-clicks'];
|
|
return $this->message['o:tracking-clicks'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $timeDate
|
|
|
|
- * @param string|null $timeZone
|
|
|
|
- * @return string
|
|
|
|
- */
|
|
|
|
public function setDeliveryTime($timeDate, $timeZone = null)
|
|
public function setDeliveryTime($timeDate, $timeZone = null)
|
|
{
|
|
{
|
|
if (isset($timeZone)) {
|
|
if (isset($timeZone)) {
|
|
$timeZoneObj = new \DateTimeZone("$timeZone");
|
|
$timeZoneObj = new \DateTimeZone("$timeZone");
|
|
} else {
|
|
} else {
|
|
- $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
|
|
|
|
|
|
+ $timeZoneObj = new \DateTimeZone(\DEFAULT_TIME_ZONE);
|
|
}
|
|
}
|
|
|
|
|
|
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
|
|
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
|
|
@@ -429,20 +308,11 @@ class MessageBuilder
|
|
return $this->message['o:deliverytime'];
|
|
return $this->message['o:deliverytime'];
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $customName
|
|
|
|
- * @param mixed $data
|
|
|
|
- */
|
|
|
|
public function addCustomData($customName, $data)
|
|
public function addCustomData($customName, $data)
|
|
{
|
|
{
|
|
$this->message['v:' . $customName] = json_encode($data);
|
|
$this->message['v:' . $customName] = json_encode($data);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param string $parameterName
|
|
|
|
- * @param mixed $data
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
public function addCustomParameter($parameterName, $data)
|
|
public function addCustomParameter($parameterName, $data)
|
|
{
|
|
{
|
|
if (isset($this->message[$parameterName])) {
|
|
if (isset($this->message[$parameterName])) {
|
|
@@ -456,25 +326,16 @@ class MessageBuilder
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @param array $message
|
|
|
|
- */
|
|
|
|
public function setMessage($message)
|
|
public function setMessage($message)
|
|
{
|
|
{
|
|
$this->message = $message;
|
|
$this->message = $message;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @return array
|
|
|
|
- */
|
|
|
|
public function getMessage()
|
|
public function getMessage()
|
|
{
|
|
{
|
|
return $this->message;
|
|
return $this->message;
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * @return array
|
|
|
|
- */
|
|
|
|
public function getFiles()
|
|
public function getFiles()
|
|
{
|
|
{
|
|
return $this->files;
|
|
return $this->files;
|