PHP code example of rnevarezc / postal

1. Go to this page and download the library: Download rnevarezc/postal library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rnevarezc / postal example snippets


// Create a new Postal client using the server key of your Postal Installation.
$client = new \Postal\Client('https://postal.yourdomain.com', 'your-api-key');

// Optional: You can add any aditional Headers for your API installation (Maybe Authorization)
// You just add an array of headers:
$headers = [
    'Authorization' => 'Basic RTYtaO54BGBtcG9yYWwyMDIw'
];

$client = new \Postal\Client('https://postal.yourdomain.com', 'your-api-key', $headers);

// Or you can add them manually to a Client Instance:
$client->addHeaders($headers);

// Create a new Mail message with a Hash of attributes:
$mail = new \Postal\Mail\Mail([
    'to' => ['[email protected]', '[email protected]'],
    'cc' => '[email protected]',
    'from' => '[email protected]',
    'subject' => 'A Postal Email!',
    'plain_body' => 'This is a test of new Postal Client',
    'html_body' => '<p>This is a HTML body!</p>'
]);

$mail = new \Postal\Mail\Mail;

$mail->addTo('[email protected]');

// You can Add TO, CC, BCC recipients using strings or arrays:
$mail->addTo(['[email protected]', '[email protected]']);
$mail->addCc('[email protected]');

// Add any custom headers
$mail->addHeader('X-PHP-Test', 'value');

// Set the Subject, Plain Body, HTML Body, from, and sender manually:
$mail->setSubject('My new subject');
$mail->setPlainBody('This is a new text');
$mail->setHtmlBody('<p>This is a new text</p>');

// Finally, when you are ready, send the Message using the client.
// You can capture the API Response if you like.
$response = $client->send($mail);
// This is a instance of \Postal\Response\Response;

//This will return a Hash of 'recipient' => \Postal\Message\Message Instance
$messages = $mail->getMessages();

foreach ($messages as $recipient => $message){
    $recipient = $recipient; //[email protected]
    $id = $message->getId(); // Ex.: 653621
    $token = $message->getToken() // abcdef123
}

// Get the Details of the Message: 653621 of the previous example:
$response = $client->getMessageDetails(653621);

// Then use the details:
$data = $response->getData(); // This is an array of the Message Details.

// Additionaly you can specify individual _expansions for the Message Detail,
// or use "true" to retreive them all:
$response = $client->getMessageDetails(653621, ['status', 'details', 'inspection']);

// OR:
$response = $client->getMessageDetails(653621, true);
// This will get all the expansions provided by the Postal API.

// Get the Details of the Message: 653621 of the previous example:
$response = $client->getMessageDeliveries(653621);
// This will return a \Postal\Response\Response instance

// Then use the deliveries structure:
$data = $response->getData(); // This is an array of the Message Deliveries

...
use Postal\Events\Message\Events;
use Postal\Events\Message\MessageEvent;
use Psr\Http\Message\RequestInterface;

class MessageEventController extends Controller
{
    /**
     * Handle Message Event Webhooks
     *
     * Postal sends the events via a POST method.
     *
     * @param Request $request
     * ...
     */
    public function post(RequestInterface $request) {

       // Here we capture the Event payload provided in the PSR-7
       // Request and parse it into an Event using the Message Events Factory.

       // @var \Postal\Events\Message\MessageEvent
       $event = Events::fromRequest($request);

       // ... Do some Stuff...
    }
}
...

$event = Events::fromPayload($array);

...
interface MessageEvent extends Event
{
    const SENT = 'Sent';
    const DELAYED = 'Delayed';
    const DELIVERY_FAILED = 'DeliveryFailed';
    const HELD = 'Held';
    const BOUNCED = 'Bounced';
    const CLICKED = 'LinkClicked';
    ...
}

$event = DomainDNSError::fromPayload($array);

$event = SendLimitApproaching::fromPayload($array);

$event = SendLimitExceeded::fromPayload($array);