PHP code example of metabytes-sro / epost-api

1. Go to this page and download the library: Download metabytes-sro/epost-api 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/ */

    

metabytes-sro / epost-api example snippets


// Authenticate
/** @var League\OAuth2\Client\Token\AccessToken $token */
$token = $this->fetchAccessToken();

// Create letter and envelope
$letter = new MetabytesSRO\EPost\Api\Letter();
$envelope = new MetabytesSRO\EPost\Api\Metadata\Envelope();
$envelope
    ->setSystemMessageTypeNormal()  // For sending an electronic letter *OR*
    ->setSystemMessageTypeHybrid()  // For sending a physical letter
    ->setSubject('Example letter');

// Add recipients for normal letter
$recipient = new MetabytesSRO\EPost\Api\Metadata\Envelope\Recipient\Normal::createFromFriendlyEmail('John Doe <[email protected]>');

$envelope->addRecipientNormal($recipient);

// Set recipients and delivery options for printed letter
$recipient = new MetabytesSRO\EPost\Api\Metadata\Envelope\Recipient\Hybrid();
$recipient
    ->setFirstName('John')
    ->setLastName('Doe')
    ->setStreetName('…')
    ->setZipCode('1234')
    ->setCity('…');

$envelope->addRecipientPrinted($recipient);

// Set delivery options
$deliveryOptions = new MetabytesSRO\EPost\Api\Metadata\DeliveryOptions();
$deliveryOptions
    ->setRegisteredStandard()   // This will make the letter sent as "Einschreiben ohne Optionen"
    ->setColorColored()         // To make it expensive
    ->setCoverLetterIncluded(); // The cover letter (with recipient address block) is 

// Prepare letter
$letter
    ->setTestEnvironment(true)
    ->setAccessToken($token)
    ->setEnvelope($envelope)
    ->setCoverLetter('This is an example');

// Set attachments
$letter->addAttachment('/var/www/test.pdf');

// Create and send letter
try {
    $letter
        ->create()
        ->send();

} catch (GuzzleHttp\Exception\ClientException $e) {
    $errorInformation = \GuzzleHttp\json_decode($e->getResponse()->getBody());
}

$priceInformation = $letter->queryPriceInformation();

var_dump($priceInformation);

$postageInfo = new MetabytesSRO\EPost\Api\Metadata\PostageInfo();
$postageInfo
    ->setLetterTypeHybrid()
    ->setLetterSize(3)
    ->setDeliveryOptions($deliveryOptions);
    
$letter = new MetabytesSRO\EPost\Api\Letter();
$letter->setPostageInfo($postageInfo);
$priceInformation = $letter->queryPriceInformation();

var_dump($priceInformation);

$letter
    ->create() // Yeah, it must be created beforehand, so we have a "letterId"
    ->delete();

$letter = new EPost\Api\Letter();
$letter
    ->setLetterId('asdf-124-asdf')
    ->delete();