PHP code example of ankitfromindia / mx18-laravel

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

    

ankitfromindia / mx18-laravel example snippets


use AnkitFromIndia\MX18\Mail\MX18Mail;
use AnkitFromIndia\MX18\Facades\MX18;

$mail = (new MX18Mail())
    ->from('[email protected]', 'Your Name')
    ->to('[email protected]', 'Recipient Name')
    ->subject('Welcome to Our Service')
    ->html('<h1>Hello {{name}}!</h1><p>Welcome to our platform.</p>')
    ->text('Hello {{name}}! Welcome to our platform.');

$response = MX18::send($mail);

$mail = (new MX18Mail())
    ->from('[email protected]', 'Your Company')
    ->to('[email protected]', 'John Doe', ['name' => 'John', 'plan' => 'Premium'])
    ->cc('[email protected]', 'Manager')
    ->replyTo('[email protected]', 'Support Team')
    ->subject('Your {{plan}} account is ready!')
    ->html('<h1>Hi {{name}}</h1><p>Your {{plan}} plan is now active.</p>')
    ->globalPersonalization(['company' => 'Your Company']);

$response = MX18::send($mail);

$pdfContent = base64_encode(file_get_contents('/path/to/file.pdf'));

$mail = (new MX18Mail())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Invoice Attached')
    ->html('<p>Please find your invoice attached.</p>')
    ->attach($pdfContent, 'invoice.pdf', 'application/pdf');

$response = MX18::send($mail);

$mail = (new MX18Mail())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Email with Custom Headers')
    ->html('<p>Email content</p>')
    ->headers([
        'X-Custom-Header' => 'CustomValue',
        'X-Mailer' => 'MX18 API'
    ])
    ->customArguments([
        'customerAccountNumber' => '12345',
        'campaignId' => 'summer2024'
    ]);

$response = MX18::send($mail);

$mail = (new MX18Mail())
    ->from('[email protected]', 'Your Company')
    ->addRecipient('[email protected]', 'John', ['name' => 'John', 'plan' => 'Premium'])
    ->addRecipient('[email protected]', 'Jane', ['name' => 'Jane', 'plan' => 'Basic'])
    ->subject('Welcome {{name}}!')
    ->html('<h1>Hi {{name}}</h1><p>Your {{plan}} plan is ready.</p>');

$response = MX18::send($mail);

$mails = [
    (new MX18Mail())
        ->from('[email protected]')
        ->to('[email protected]', 'User One')
        ->subject('Newsletter #1')
        ->html('<h1>Newsletter Content</h1>'),
    
    (new MX18Mail())
        ->from('[email protected]')
        ->to('[email protected]', 'User Two')
        ->subject('Newsletter #2')
        ->html('<h1>Different Content</h1>'),
];

$responses = MX18::sendBulk($mails);

foreach ($responses as $response) {
    echo "Transaction ID: " . $response['transactionId'] . "\n";
}

$webhookUrl = MX18::getWebhookUrl();
// Returns: https://yourdomain.com/mx18/webhook

// In EventServiceProvider.php
use AnkitFromIndia\MX18\Webhooks\WebhookReceived;

protected $listen = [
    WebhookReceived::class => [
        'App\Listeners\HandleMX18Webhook',
    ],
];

// app/Listeners/HandleMX18Webhook.php


namespace App\Listeners;

use AnkitFromIndia\MX18\Webhooks\WebhookReceived;

class HandleMX18Webhook
{
    public function handle(WebhookReceived $event)
    {
        $payload = $event->payload;
        
        // Handle different event types
        switch ($payload['event']) {
            case 'delivered':
                // Email was delivered
                break;
            case 'opened':
                // Email was opened
                break;
            case 'clicked':
                // Link was clicked
                break;
            case 'bounced':
                // Email bounced
                break;
        }
    }
}

[
    'transactionId' => 'abc123xyz456',
    'status' => 'Accepted',
    'message' => 'Email request has been accepted for delivery.'
]

try {
    $response = MX18::send($mail);
    echo "Email sent! Transaction ID: " . $response['transactionId'];
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

// config/mx18.php
return [
    'api_token' => env('MX18_API_TOKEN'),
    'api_url' => env('MX18_API_URL', 'https://api.mx18.com/api/1'),
    'webhook_secret' => env('MX18_WEBHOOK_SECRET'),
    'webhook_path' => env('MX18_WEBHOOK_PATH', '/mx18/webhook'),
];

// Used Authorization: Bearer header (incorrect)

// Now uses X-Api-Key header (correct)
// No code changes needed - just update your package version

// Custom headers
$mail->headers([
    'X-Campaign-ID' => 'summer2024',
    'X-Mailer' => 'My App'
]);

// Custom arguments for tracking
$mail->customArguments([
    'userId' => '12345',
    'source' => 'newsletter'
]);
bash
php artisan vendor:publish --tag=mx18-config