PHP code example of matchory / laravel-mailgun-templated-messages

1. Go to this page and download the library: Download matchory/laravel-mailgun-templated-messages 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/ */

    

matchory / laravel-mailgun-templated-messages example snippets


    // ...

    'mailgun' => [
    
        // Add your mailing domain as registered on Mailgun
        'domain' => env('MAILGUN_DOMAIN', 'mailing.example.com'),

        // Add your Mailgun secret
        'secret' => env('MAILGUN_SECRET'),
        
        // Optional: Specify the endpoint of Mailgun's EU API if you're a EU
        // customer and need to comply to the GDPR
        'endpoint' => env('MAILGUN_ENDPOINT', 'https://api.eu.mailgun.net'),
    ],

    // ...

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

class TestNotification extends Notification
{
    use Queueable;
    
    public function __construct(private readonly int $randomNumber) {}

    public function toMailgun(mixed $notifiable): MailgunTemplatedMessage
    {
        return (new MailgunTemplatedMessage('your_template_name'))
            ->from('[email protected]')
            ->subject('Test Subject')
            ->param('foo', 'bar')
            ->params([
                'some' => 'more data',
                'available' => 'in your template',
                'name' => $notifiable->name,
                'number' => $this->randomNumber
            ]);
    }
}

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;

// number chosen by fair dice roll
Notification::sendNow(Auth::user(), new TestNotification(4));

use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

$message = new MailgunTemplatedMessage();

// Add an option.
// Note that the value can be anything that can be converted to JSON!
$message->addOption(name: 'skip-verification', value: false);

// Use the fluent methods for chaining several operations together. They all
// have an equivalent getter and setter.
$message->option(name: 'skip-verification', value: false)
        ->option('equire-tls');

use Matchory\MailgunTemplatedMessages\Messages\MailgunTemplatedMessage;

$message = new MailgunTemplatedMessage();

// Add an param.
// Note that the value can be anything that can be converted to JSON!
$message->addParam(name: 'foo', value: 'bar');

// Use the fluent methods for chaining several operations together. They all
// have an equivalent getter and setter.
$message->param(name: 'foo', value: 'bar')
        ->param('baz', true)

// Set multiple params at once
$message->params([
    'foo' => false,
    'bar' => true,
]);

// Check whether params are set
$message->hasParam('foo'); // true

// Retrieve all params
$params = $message->getParams(); 

// Remove a previously set param. If the param isn't set, this does nothing
$message->removeParam('foo');

// Equivalent to the above removeParam() call
$message = $message->withoutParam('foo');