PHP code example of tihloh / prefab-messaging

1. Go to this page and download the library: Download tihloh/prefab-messaging 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/ */

    

tihloh / prefab-messaging example snippets


$messaging = new MessagingManager([
    $mailChannel,
    $smsChannel,
]);

$result = $messaging->send(
    'sms',
    $recipient,
    new Message('OTP', 'Your code is 123456'),
);

$recipient = new Recipient([
    'mail' => '[email protected]',
    'sms' => '+639000000000',
]);

$message = new Message(
    subject: 'Annual Report',
    text: 'Your report is ready.',
);

$channel = new CallableChannel(
    'test',
    function (Recipient $recipient, Message $message) {
        return 'TEST-123';
    }
);

$result = $messaging->mail(
    '[email protected]',
    'Annual Report',
    'Your report is ready.',
);

$messaging = new MessagingManager([
    new MailChannel($transport),
]);

$smtp = new SmtpTransport([
    'host' => 'smtp.example.com',
    'port' => 587,
    'username' => '[email protected]',
    'password' => 'secret',
    'encryption' => 'tls',
    'from' => [
        'address' => '[email protected]',
        'name' => 'My System',
    ],
]);

$message = new Message(
    subject: 'Report',
    text: 'Your report is attached.',
    attachments: [
        Attachment::fromPath('/tmp/report.pdf'),
    ],
);

$template = new Template(
    subject: 'Hello {{name}}',
    text: 'Your report {{report}} is ready.'
);

$message = $template->render([
    'name' => 'Demo User',
    'report' => 'AR-2026',
]);

$messaging->on('sending', $listener);
$messaging->on('sent', $listener);
$messaging->on('failed', $listener);

$messaging->on('sending', function ($channel, $recipient, $message) {});
$messaging->on('sent', function ($channel, $recipient, $message, $result) {});
$messaging->on('failed', function ($channel, $recipient, $message, $result) {});

$messaging->send('mail', $recipient, $message);

prefab_trace();

prefab_trace(true);