PHP code example of mattstein / usesend-laravel

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

    

mattstein / usesend-laravel example snippets


Mail::to($user)->send(new MeetupReminder($meetup));

Mail::mailer('usesend')->send(new MeetupReminder($meetup));

// config/mail.php
'mailers' => [
    'usesend-staging' => [
        'transport' => 'usesend',
        'api_key' => env('USESEND_STAGING_API_KEY'),
        'base_url' => env('USESEND_STAGING_BASE_URL'),
    ],
],

Mail::mailer('usesend-staging')->send(new MeetupReminder($meetup));

use Illuminate\Mail\Mailable;
use MattStein\UseSend\Concerns\HasUseSendTemplate;

class WelcomeMail extends Mailable
{
    use HasUseSendTemplate;

    public function build(): self
    {
        return $this->from('[email protected]')
            ->to($this->user)
            ->useSendTemplate('welcome_template', [
                'name' => $this->user->name,
                'attempts' => $this->user->attempts, // cast to a string for you
            ]);
    }
}

use Illuminate\Mail\Mailable;
use MattStein\UseSend\Concerns\HasUseSendSchedule;

class MeetupReminder extends Mailable
{
    use HasUseSendSchedule;

    public function build(): self
    {
        return $this->subject('Your meetup is tomorrow')
            ->view('mail.reminder')
            ->useSendScheduledAt($this->meetup->starts_at->subDay());
    }
}

$this->useSendTemplate('reply_template', inReplyToId: $ticket->usesend_email_id);

use MattStein\UseSend\UseSendTransport;

$this->withSymfonyMessage(function (Email $email) use ($ticket): void {
    $email->getHeaders()->addTextHeader(UseSendTransport::IN_REPLY_TO_ID_HEADER, $ticket->usesend_email_id);
});

use Illuminate\Mail\Mailable;
use MattStein\UseSend\Concerns\HasUseSendIdempotencyKey;

class OrderConfirmation extends Mailable
{
    use HasUseSendIdempotencyKey;

    public function build(): self
    {
        return $this->to($this->order->customer)
            ->subject('Your order')
            ->view('mail.order')
            ->useSendIdempotencyKey('order-confirmation-'.$this->order->id);
    }
}

use Illuminate\Mail\Mailables\Headers;
use MattStein\UseSend\UseSendTransport;

public function headers(): Headers
{
    return new Headers(text: [
        UseSendTransport::IDEMPOTENCY_KEY_HEADER => 'order-confirmation-'.$this->order->id,
    ]);
}

$sent = Mail::to($user)->send(new MeetupReminder($meetup));

$emailId = $sent?->getMessageId();

use Illuminate\Mail\Events\MessageSent;
use MattStein\UseSend\UseSendTransport;

Event::listen(function (MessageSent $event) {
    $emailId = $event->message->getHeaders()
        ->get(UseSendTransport::EMAIL_ID_HEADER)?->getBodyAsString();
});

Mail::to($user)->send(new InviteMail($user)); // with this on the mailable

$this->withSymfonyMessage(function (Email $email): void {
    $email->getHeaders()->addTextHeader('X-Campaign', 'spring-invite');
    $email->getHeaders()->addTextHeader('List-Unsubscribe', '<mailto:[email protected]>');
});

try {
    Mail::to($user)->send(new MeetupReminder($meetup));
} catch (ApiRequestFailedException $e) {
    Log::warning('useSend rejected an email', [
        'status' => $e->status,
        'code' => $e->errorCode,
        'body' => $e->responseBody,
    ]);
}

use Illuminate\Support\Facades\Http;

Http::fake();

Mail::to('[email protected]')->send(new PlainMailable);

Http::assertSent(fn ($request) => $request->url() === 'https://app.usesend.com/api/v1/emails'
    && $request->hasHeader('Authorization', 'Bearer us_test_key')
    && $request['to'] === ['[email protected]']);

Http::fake(['*' => Http::response(['message' => 'Invalid "from" address'], 422)]);

use Illuminate\Http\Client\Factory;
use MattStein\UseSend\UseSendTransport;

$http = (new Factory)->fake();

(new UseSendTransport(['api_key' => 'us_test_key'], $http))->send($email);

$http->assertSentCount(1);
bash
php artisan usesend:install