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:
Joel Koch 2022-01-18 11:51:25 +02:00 committed by GitHub
parent 4db9d9bb63
commit f25a22344b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View File

@ -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);

View File

@ -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)) {

View File

@ -30,8 +30,10 @@ $builder->addToRecipient("john.doe@example.com", array("first" => "John", "last"
$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");
@ -61,8 +63,10 @@ $batchMessage = $mg->messages()->getBatchMessage("example.com");
$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"));