PHP code example of beziworld / laravel-emaillabs

1. Go to this page and download the library: Download beziworld/laravel-emaillabs 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/ */

    

beziworld / laravel-emaillabs example snippets


'mailers' => [
    'emaillabs' => [
        'transport' => 'emaillabs',
        // 'connection' => 'default',          // connection from config/emaillabs.php
        // 'smtp_account' => '2.yourpanel.smtp', // another SMTP account of the panel
    ],
],

use Illuminate\Mail\Mailables\Envelope;

public function envelope(): Envelope
{
    return new Envelope(
        subject: 'Your order has shipped',
        tags: ['orders', 'shipping'],          // EmailLabs tags (reports, webhooks)
        metadata: ['order_id' => $this->order->id], // sent as X-Metadata-order_id
    );
}

use Beziworld\EmailLabs\Transport\EmailLabsTransport;

public function headers(): Headers
{
    return new Headers(text: [
        EmailLabsTransport::HEADER_TEMPLATE => 'welcome-v2',
        EmailLabsTransport::HEADER_VARS => json_encode(['name' => $this->user->name]),
        'X-TRACKING-OFF' => '1', // disable link tracking for this message
    ]);
}

use Beziworld\EmailLabs\Facades\EmailLabs;
use Beziworld\EmailLabs\Messages\EmailLabsMessage;

$result = EmailLabs::send(
    EmailLabsMessage::create('Hello {{ name }}')
        ->from('[email protected]', 'Shop')
        ->to('[email protected]', 'Jane', vars: ['name' => 'Jane'], messageId: '[email protected]')
        ->to('[email protected]', 'John', vars: ['name' => 'John'])
        ->html('<p>Hi {{ name }}</p>')
        ->text('Hi {{ name }}')
        ->tag('orders')
        ->header('X-Order', '1001')
        ->attachData($pdf, 'invoice.pdf', 'application/pdf')
);

$result->messageIdFor('[email protected]'); // [email protected]
$result->isPartial();                       // true when EmailLabs answered 207
$result->errors;                            // rejected recipients

$id = EmailLabs::addTemplate('Welcome', '<p>Dear {{ name }}</p>', 'Dear {{ name }}', externalId: 'welcome-v2');

EmailLabs::send(EmailLabsMessage::create('Welcome')->from('[email protected]')
    ->to('[email protected]', vars: ['name' => 'Jane'])
    ->template('welcome-v2', ['shop' => 'Example']));

EmailLabs::templates();
EmailLabs::deleteTemplates('welcome-v2');

EmailLabs::email('[email protected]');           // EmailEvent: status, statusTime, history
EmailLabs::emails(['to' => '[email protected]', 'tags' => ['orders'], 'dateFrom' => now()->subDay()]);
EmailLabs::allEmails(['smtpAccount' => '1.yourpanel.smtp'])->each(fn ($event) => ...); // LazyCollection
EmailLabs::opens(['messageId' => '[email protected]']);   // kept 7 days
EmailLabs::clicks(['dateFrom' => now()->subDays(2)]);
EmailLabs::statistics(now()->subWeek(), now(), withTags: true); // aggregated data as returned
EmailLabs::smtpAccounts();

use Beziworld\EmailLabs\Enums\BlacklistReason;

EmailLabs::addToBlacklist('[email protected]', BlacklistReason::Unsubscribe, 'GDPR request');
EmailLabs::isBlacklisted('[email protected]');
EmailLabs::blacklist();                       // first page (up to 1000)
EmailLabs::allBlacklisted();                  // LazyCollection over every page
EmailLabs::removeFromBlacklist(['[email protected]']);
EmailLabs::addDomainToBlacklist('spam.example');
EmailLabs::blacklistReasons();                // hardbounce, unsubscribe, spam_complaint, other

   use Beziworld\EmailLabs\Events\EmailBounced;
   use Beziworld\EmailLabs\Events\EmailDelivered;

   Event::listen(function (EmailBounced $event) {
       if ($event->event->status->isPermanentFailure()) {
           User::where('email', $event->event->to->email)->update(['email_bounced_at' => now()]);
       }
   });

   Event::listen(fn (EmailDelivered $event) => MailLog::where('message_id', $event->event->messageId)
       ->update(['delivered_at' => $event->event->statusTime]));
   

'connections' => [
    'default' => ['app_key' => env('EMAILLABS_APP_KEY'), 'secret_key' => env('EMAILLABS_SECRET_KEY'), 'smtp_account' => '1.shop.smtp'],
    'marketing' => ['app_key' => env('EMAILLABS_MARKETING_APP_KEY'), 'secret_key' => env('EMAILLABS_MARKETING_SECRET_KEY'), 'smtp_account' => '1.news.smtp'],
],

EmailLabs::connection('marketing')->send($message);
EmailLabs::connection()->forSmtpAccount('2.shop.smtp')->send($message);
EmailLabs::using(['app_key' => $tenant->el_app_key, 'secret_key' => decrypt($tenant->el_secret), 'smtp_account' => $tenant->el_smtp]);

Http::fake(['api.emaillabs.io/*' => Http::response(['meta' => ['status' => 200], 'data' => []])]);

Mail::to('[email protected]')->send(new OrderShipped($order));

Http::assertSent(fn ($request) => $request->url() === 'https://api.emaillabs.io/v2.1/email');