mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-29 16:36:07 +03:00
Add template support when using BatchMessage (#786)
* Add support for Mailgun templates when using BatchMessages * Fixed quatation marks in error message * Comment change * Change function comment style
This commit is contained in:
parent
4db9d9bb63
commit
f25a22344b
@ -114,8 +114,8 @@ class BatchMessage extends MessageBuilder
|
|||||||
throw MissingRequiredParameter::create('subject');
|
throw MissingRequiredParameter::create('subject');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($message['text']) && empty($message['html'])) {
|
if (empty($message['text']) && empty($message['html']) && empty($message['template'])) {
|
||||||
throw MissingRequiredParameter::create('text" or "html');
|
throw MissingRequiredParameter::create('text", "html" or "template');
|
||||||
}
|
}
|
||||||
|
|
||||||
$message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
|
$message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
|
||||||
|
@ -231,6 +231,16 @@ class MessageBuilder
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $template Name of the Mailgun template
|
||||||
|
*/
|
||||||
|
public function setTemplate(string $template): self
|
||||||
|
{
|
||||||
|
$this->message['template'] = $template;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function addCustomHeader(string $headerName, $headerData): self
|
public function addCustomHeader(string $headerName, $headerData): self
|
||||||
{
|
{
|
||||||
if (!preg_match('/^h:/i', $headerName)) {
|
if (!preg_match('/^h:/i', $headerName)) {
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
Mailgun - Messages
|
Mailgun - Messages
|
||||||
==================
|
==================
|
||||||
|
|
||||||
This is the Mailgun PHP *Message* utilities.
|
This is the Mailgun PHP *Message* utilities.
|
||||||
|
|
||||||
The below assumes you've already installed the Mailgun PHP SDK in to your
|
The below assumes you've already installed the Mailgun PHP SDK in to your
|
||||||
project. If not, go back to the master README for instructions.
|
project. If not, go back to the master README for instructions.
|
||||||
|
|
||||||
There are two utilities included, `MessageBuilder` and `BatchMessage`.
|
There are two utilities included, `MessageBuilder` and `BatchMessage`.
|
||||||
|
|
||||||
* `MessageBuilder`: Allows you to build a message object by calling methods for
|
* `MessageBuilder`: Allows you to build a message object by calling methods for
|
||||||
each MIME attribute.
|
each MIME attribute.
|
||||||
* `BatchMessage`: Extends `MessageBuilder` and allows you to iterate through
|
* `BatchMessage`: Extends `MessageBuilder` and allows you to iterate through
|
||||||
recipients from a list. Messages will fire after the 1,000th recipient has been
|
recipients from a list. Messages will fire after the 1,000th recipient has been
|
||||||
added.
|
added.
|
||||||
|
|
||||||
Usage - Message Builder
|
Usage - Message Builder
|
||||||
-----------------------
|
-----------------------
|
||||||
Here's how to use Message Builder to build your Message.
|
Here's how to use Message Builder to build your Message.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
# Next, instantiate a Message Builder object from the SDK.
|
# Next, instantiate a Message Builder object from the SDK.
|
||||||
@ -28,10 +28,12 @@ $builder->setFromAddress("me@example.com", array("first"=>"PHP", "last" => "SDK"
|
|||||||
$builder->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
|
$builder->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
|
||||||
# Define a cc recipient.
|
# Define a cc recipient.
|
||||||
$builder->addCcRecipient("sally.doe@example.com", array("full_name" => "Sally Doe"));
|
$builder->addCcRecipient("sally.doe@example.com", array("full_name" => "Sally Doe"));
|
||||||
# Define the subject.
|
# Define the subject.
|
||||||
$builder->setSubject("A message from the PHP SDK using Message Builder!");
|
$builder->setSubject("A message from the PHP SDK using Message Builder!");
|
||||||
# Define the body of the message.
|
# Define the body of the message (One is required).
|
||||||
$builder->setTextBody("This is the text body of the message!");
|
$builder->setTextBody("This is the text body of the message!");
|
||||||
|
$builder->setHtmlBody("<html><p>This is the HTML body of the message</p></html>");
|
||||||
|
$builder->setTemplate("template_name");
|
||||||
|
|
||||||
# Other Optional Parameters.
|
# Other Optional Parameters.
|
||||||
$builder->addCampaignId("My-Awesome-Campaign");
|
$builder->addCampaignId("My-Awesome-Campaign");
|
||||||
@ -48,10 +50,10 @@ $mg->messages()->send("example.com", $builder->getMessage());
|
|||||||
|
|
||||||
Usage - Batch Message
|
Usage - Batch Message
|
||||||
---------------------
|
---------------------
|
||||||
Here's how to use Batch Message to easily handle batch sending jobs.
|
Here's how to use Batch Message to easily handle batch sending jobs.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
# First, instantiate the SDK with your API credentials and define your domain.
|
# First, instantiate the SDK with your API credentials and define your domain.
|
||||||
$mg = new Mailgun("key-example");
|
$mg = new Mailgun("key-example");
|
||||||
|
|
||||||
# Next, instantiate a Message Builder object from the SDK, pass in your sending domain.
|
# Next, instantiate a Message Builder object from the SDK, pass in your sending domain.
|
||||||
@ -59,17 +61,19 @@ $batchMessage = $mg->messages()->getBatchMessage("example.com");
|
|||||||
|
|
||||||
# Define the from address.
|
# Define the from address.
|
||||||
$batchMessage->setFromAddress("me@example.com", array("first"=>"PHP", "last" => "SDK"));
|
$batchMessage->setFromAddress("me@example.com", array("first"=>"PHP", "last" => "SDK"));
|
||||||
# Define the subject.
|
# Define the subject.
|
||||||
$batchMessage->setSubject("A Batch Message from the PHP SDK!");
|
$batchMessage->setSubject("A Batch Message from the PHP SDK!");
|
||||||
# Define the body of the message.
|
# Define the body of the message (One is required).
|
||||||
$batchMessage->setTextBody("This is the text body of the message!");
|
$builder->setTextBody("This is the text body of the message!");
|
||||||
|
$builder->setHtmlBody("<html><p>This is the HTML body of the message</p></html>");
|
||||||
|
$builder->setTemplate("template_name");
|
||||||
|
|
||||||
# Next, let's add a few recipients to the batch job.
|
# Next, let's add a few recipients to the batch job.
|
||||||
$batchMessage->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
|
$batchMessage->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
|
||||||
$batchMessage->addToRecipient("sally.doe@example.com", array("first" => "Sally", "last" => "Doe"));
|
$batchMessage->addToRecipient("sally.doe@example.com", array("first" => "Sally", "last" => "Doe"));
|
||||||
$batchMessage->addToRecipient("mike.jones@example.com", array("first" => "Mike", "last" => "Jones"));
|
$batchMessage->addToRecipient("mike.jones@example.com", array("first" => "Mike", "last" => "Jones"));
|
||||||
...
|
...
|
||||||
// After 1,000 recipients, Batch Message will automatically post your message to the messages endpoint.
|
// After 1,000 recipients, Batch Message will automatically post your message to the messages endpoint.
|
||||||
|
|
||||||
// Call finalize() to send any remaining recipients still in the buffer.
|
// Call finalize() to send any remaining recipients still in the buffer.
|
||||||
$batchMessage->finalize();
|
$batchMessage->finalize();
|
||||||
@ -80,5 +84,5 @@ $messageIds = $batchMessage->getMessageIds();
|
|||||||
|
|
||||||
More Documentation
|
More Documentation
|
||||||
------------------
|
------------------
|
||||||
See the official [Mailgun Docs](https://documentation.mailgun.com/en/latest/api-sending.html)
|
See the official [Mailgun Docs](https://documentation.mailgun.com/en/latest/api-sending.html)
|
||||||
for more information.
|
for more information.
|
||||||
|
Loading…
Reference in New Issue
Block a user