mailgun-php/src/Message
Tony Thomas 1fd018d61c
fix(#642) HttpApi: Expect client to be of type ClientInterface|PluginClient (#660)
* HttpApi: Expect client to be of type ClientInterface|PluginClient

* Format with pretty

* pretty-fix run on the whole repo

* User is_a instead of instance_of for php-stan

* Use the namespaced classname in instanceof checks

* Fix param documentation for MessageBuilder::parseAddress

* Use ClassName::class approach in is_a
2020-10-16 16:46:06 +01:00
..
Exceptions Added new CS rules 2019-01-10 08:25:29 +00:00
BatchMessage.php Minor bugfixes detected with PHPStan 2019-01-10 08:25:29 +00:00
MessageBuilder.php fix(#642) HttpApi: Expect client to be of type ClientInterface|PluginClient (#660) 2020-10-16 16:46:06 +01:00
README.md Use PSR-4 (#517) 2019-01-06 08:34:49 +01:00

Mailgun - Messages

This is the Mailgun PHP Message utilities.

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.

There are two utilities included, MessageBuilder and BatchMessage.

  • MessageBuilder: Allows you to build a message object by calling methods for each MIME attribute.
  • BatchMessage: Extends MessageBuilder and allows you to iterate through recipients from a list. Messages will fire after the 1,000th recipient has been added.

Usage - Message Builder

Here's how to use Message Builder to build your Message.

# Next, instantiate a Message Builder object from the SDK.
$builder = new MessageBuilder();

# Define the from address.
$builder->setFromAddress("me@example.com", array("first"=>"PHP", "last" => "SDK"));
# Define a to recipient.
$builder->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
# Define a cc recipient.
$builder->addCcRecipient("sally.doe@example.com", array("full_name" => "Sally Doe"));
# Define the subject. 
$builder->setSubject("A message from the PHP SDK using Message Builder!");
# Define the body of the message.
$builder->setTextBody("This is the text body of the message!");

# Other Optional Parameters.
$builder->addCampaignId("My-Awesome-Campaign");
$builder->addCustomHeader("Customer-Id", "12345");
$builder->addAttachment("@/tron.jpg");
$builder->setDeliveryTime("tomorrow 8:00AM", "PST");
$builder->setClickTracking(true);

# Finally, send the message.
$mg = Mailgun::create('key-example');
$domain = ;
$mg->messages()->send("example.com", $builder->getMessage());

Usage - Batch Message

Here's how to use Batch Message to easily handle batch sending jobs.

# First, instantiate the SDK with your API credentials and define your domain. 
$mg = new Mailgun("key-example");

# Next, instantiate a Message Builder object from the SDK, pass in your sending domain.
$batchMessage = $mg->messages()->getBatchMessage("example.com");

# Define the from address.
$batchMessage->setFromAddress("me@example.com", array("first"=>"PHP", "last" => "SDK"));
# Define the subject. 
$batchMessage->setSubject("A Batch Message from the PHP SDK!");
# Define the body of the message.
$batchMessage->setTextBody("This is the text body of the message!");

# Next, let's add a few recipients to the batch job.
$batchMessage->addToRecipient("john.doe@example.com", array("first" => "John", "last" => "Doe"));
$batchMessage->addToRecipient("sally.doe@example.com", array("first" => "Sally", "last" => "Doe"));
$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. 

// Call finalize() to send any remaining recipients still in the buffer.
$batchMessage->finalize();

$messageIds = $batchMessage->getMessageIds();

More Documentation

See the official Mailgun Docs for more information.