added mailgun librarie
This commit is contained in:
@@ -2,49 +2,23 @@
|
||||
|
||||
namespace Mailgun\Messages;
|
||||
|
||||
use Mailgun\Constants\Api;
|
||||
use Mailgun\Constants\ExceptionMessages;
|
||||
use Mailgun\Messages\MessageBuilder;
|
||||
use Mailgun\Messages\Exceptions\TooManyParameters;
|
||||
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
|
||||
|
||||
/**
|
||||
* This class is used for batch sending. See the official documentation (link below)
|
||||
* for usage instructions.
|
||||
*
|
||||
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
|
||||
*/
|
||||
/*
|
||||
This class is used for batch sending. See the official documentation
|
||||
for usage instructions.
|
||||
*/
|
||||
|
||||
class BatchMessage extends MessageBuilder{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $batchRecipientAttributes;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
private $autoSend;
|
||||
|
||||
/**
|
||||
* @var \Mailgun\Connection\RestClient
|
||||
*/
|
||||
private $restClient;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $workingDomain;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $messageIds = array();
|
||||
|
||||
/**
|
||||
* @param \Mailgun\Connection\RestClient $restClient
|
||||
* @param string $workingDomain
|
||||
* @param boolean $autoSend
|
||||
*/
|
||||
public function __construct($restClient, $workingDomain, $autoSend){
|
||||
$this->batchRecipientAttributes = array();
|
||||
$this->autoSend = $autoSend;
|
||||
@@ -53,18 +27,11 @@ class BatchMessage extends MessageBuilder{
|
||||
$this->endpointUrl = $workingDomain . "/messages";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $headerName
|
||||
* @param string $address
|
||||
* @param array $variables
|
||||
* @throws MissingRequiredMIMEParameters
|
||||
* @throws TooManyParameters
|
||||
*/
|
||||
protected function addRecipient($headerName, $address, $variables){
|
||||
if(array_key_exists($headerName, $this->counters['recipients'])){
|
||||
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){
|
||||
if($this->counters['recipients'][$headerName] == RECIPIENT_COUNT_LIMIT){
|
||||
if($this->autoSend == false){
|
||||
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
|
||||
throw new TooManyParameters(TOO_MANY_RECIPIENTS);
|
||||
}
|
||||
$this->sendMessage();
|
||||
}
|
||||
@@ -91,27 +58,22 @@ class BatchMessage extends MessageBuilder{
|
||||
$this->batchRecipientAttributes["$address"] = $variables;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $message
|
||||
* @param array $files
|
||||
* @throws MissingRequiredMIMEParameters
|
||||
*/
|
||||
public function sendMessage($message = array(), $files = array()){
|
||||
if(count($message) < 1){
|
||||
$message = $this->message;
|
||||
$files = $this->files;
|
||||
}
|
||||
if(!array_key_exists("from", $message)){
|
||||
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
elseif(!array_key_exists("to", $message)){
|
||||
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
elseif(!array_key_exists("subject", $message)){
|
||||
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){
|
||||
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
||||
}
|
||||
else{
|
||||
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
|
||||
@@ -125,16 +87,10 @@ class BatchMessage extends MessageBuilder{
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws MissingRequiredMIMEParameters
|
||||
*/
|
||||
public function finalize(){
|
||||
$this->sendMessage();
|
||||
return $this->sendMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getMessageIds(){
|
||||
return $this->messageIds;
|
||||
}
|
||||
|
@@ -2,39 +2,23 @@
|
||||
|
||||
namespace Mailgun\Messages;
|
||||
|
||||
use Mailgun\Constants\Api;
|
||||
use Mailgun\Constants\ExceptionMessages;
|
||||
use Mailgun\Messages\Exceptions\InvalidParameter;
|
||||
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
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
|
||||
protected $message = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $variables = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $files = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $counters = array(
|
||||
'recipients' => array(
|
||||
'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)
|
||||
{
|
||||
if (array_key_exists($key, $params)) {
|
||||
@@ -64,10 +42,6 @@ class MessageBuilder
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected function getFullName($params)
|
||||
{
|
||||
if (array_key_exists("first", $params)) {
|
||||
@@ -80,11 +54,6 @@ class MessageBuilder
|
||||
return $this->safeGet($params, "full_name", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param array $variables
|
||||
* @return string
|
||||
*/
|
||||
protected function parseAddress($address, $variables)
|
||||
{
|
||||
if (!is_array($variables)) {
|
||||
@@ -98,11 +67,6 @@ class MessageBuilder
|
||||
return $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $headerName
|
||||
* @param string $address
|
||||
* @param array $variables
|
||||
*/
|
||||
protected function addRecipient($headerName, $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)
|
||||
{
|
||||
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);
|
||||
|
||||
return end($this->message['to']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param array|null $variables
|
||||
* @return mixed
|
||||
* @throws TooManyParameters
|
||||
*/
|
||||
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);
|
||||
|
||||
return end($this->message['cc']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param array|null $variables
|
||||
* @return mixed
|
||||
* @throws TooManyParameters
|
||||
*/
|
||||
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);
|
||||
|
||||
return end($this->message['bcc']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param array|null $variables
|
||||
* @return mixed
|
||||
*/
|
||||
public function setFromAddress($address, $variables = null)
|
||||
{
|
||||
$this->addRecipient("from", $address, $variables);
|
||||
@@ -179,11 +120,6 @@ class MessageBuilder
|
||||
return $this->message['from'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
* @param array|null $variables
|
||||
* @return mixed
|
||||
*/
|
||||
public function setReplyToAddress($address, $variables = null)
|
||||
{
|
||||
$this->addRecipient("h:reply-to", $address, $variables);
|
||||
@@ -191,11 +127,7 @@ class MessageBuilder
|
||||
return $this->message['h:reply-to'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $subject
|
||||
* @return mixed
|
||||
*/
|
||||
public function setSubject($subject = "")
|
||||
public function setSubject($subject = null)
|
||||
{
|
||||
if ($subject == null || $subject == "") {
|
||||
$subject = " ";
|
||||
@@ -205,11 +137,6 @@ class MessageBuilder
|
||||
return $this->message['subject'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $headerName
|
||||
* @param mixed $headerData
|
||||
* @return mixed
|
||||
*/
|
||||
public function addCustomHeader($headerName, $headerData)
|
||||
{
|
||||
if (!preg_match("/^h:/i", $headerName)) {
|
||||
@@ -220,10 +147,6 @@ class MessageBuilder
|
||||
return $this->message[$headerName];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $textBody
|
||||
* @return string
|
||||
*/
|
||||
public function setTextBody($textBody)
|
||||
{
|
||||
if ($textBody == null || $textBody == "") {
|
||||
@@ -234,10 +157,6 @@ class MessageBuilder
|
||||
return $this->message['text'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $htmlBody
|
||||
* @return string
|
||||
*/
|
||||
public function setHtmlBody($htmlBody)
|
||||
{
|
||||
if ($htmlBody == null || $htmlBody == "") {
|
||||
@@ -248,11 +167,6 @@ class MessageBuilder
|
||||
return $this->message['html'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $attachmentPath
|
||||
* @param string|null $attachmentName
|
||||
* @return bool
|
||||
*/
|
||||
public function addAttachment($attachmentPath, $attachmentName = null)
|
||||
{
|
||||
if (isset($this->files["attachment"])) {
|
||||
@@ -273,11 +187,6 @@ class MessageBuilder
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $inlineImagePath
|
||||
* @param string|null $inlineImageName
|
||||
* @throws InvalidParameter
|
||||
*/
|
||||
public function addInlineImage($inlineImagePath, $inlineImageName = null)
|
||||
{
|
||||
if (preg_match("/^@/", $inlineImagePath)) {
|
||||
@@ -298,14 +207,10 @@ class MessageBuilder
|
||||
|
||||
return true;
|
||||
} else {
|
||||
throw new InvalidParameter(ExceptionMessages::INVALID_PARAMETER_INLINE);
|
||||
throw new InvalidParameter(INVALID_PARAMETER_INLINE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $testMode
|
||||
* @return string
|
||||
*/
|
||||
public function setTestMode($testMode)
|
||||
{
|
||||
if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
|
||||
@@ -318,14 +223,9 @@ class MessageBuilder
|
||||
return $this->message['o:testmode'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|int $campaignId
|
||||
* @return string|int
|
||||
* @throws TooManyParameters
|
||||
*/
|
||||
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'])) {
|
||||
array_push($this->message['o:campaign'], $campaignId);
|
||||
} else {
|
||||
@@ -335,17 +235,13 @@ class MessageBuilder
|
||||
|
||||
return $this->message['o:campaign'];
|
||||
} 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)
|
||||
{
|
||||
if ($this->counters['attributes']['tag'] < Api::TAG_LIMIT) {
|
||||
if ($this->counters['attributes']['tag'] < TAG_LIMIT) {
|
||||
if (isset($this->message['o:tag'])) {
|
||||
array_push($this->message['o:tag'], $tag);
|
||||
} else {
|
||||
@@ -355,14 +251,10 @@ class MessageBuilder
|
||||
|
||||
return $this->message['o:tag'];
|
||||
} 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)
|
||||
{
|
||||
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
||||
@@ -375,10 +267,6 @@ class MessageBuilder
|
||||
return $this->message["o:dkim"];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $enabled
|
||||
* @return string
|
||||
*/
|
||||
public function setOpenTracking($enabled)
|
||||
{
|
||||
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
||||
@@ -391,10 +279,6 @@ class MessageBuilder
|
||||
return $this->message['o:tracking-opens'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $enabled
|
||||
* @return string
|
||||
*/
|
||||
public function setClickTracking($enabled)
|
||||
{
|
||||
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
|
||||
@@ -409,17 +293,12 @@ class MessageBuilder
|
||||
return $this->message['o:tracking-clicks'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $timeDate
|
||||
* @param string|null $timeZone
|
||||
* @return string
|
||||
*/
|
||||
public function setDeliveryTime($timeDate, $timeZone = null)
|
||||
{
|
||||
if (isset($timeZone)) {
|
||||
$timeZoneObj = new \DateTimeZone("$timeZone");
|
||||
} else {
|
||||
$timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
|
||||
$timeZoneObj = new \DateTimeZone(\DEFAULT_TIME_ZONE);
|
||||
}
|
||||
|
||||
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
|
||||
@@ -429,20 +308,11 @@ class MessageBuilder
|
||||
return $this->message['o:deliverytime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $customName
|
||||
* @param mixed $data
|
||||
*/
|
||||
public function addCustomData($customName, $data)
|
||||
{
|
||||
$this->message['v:' . $customName] = json_encode($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $parameterName
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
public function addCustomParameter($parameterName, $data)
|
||||
{
|
||||
if (isset($this->message[$parameterName])) {
|
||||
@@ -456,25 +326,16 @@ class MessageBuilder
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $message
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFiles()
|
||||
{
|
||||
return $this->files;
|
||||
|
Reference in New Issue
Block a user