PHP code example of emailit / emailit-laravel

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

    

emailit / emailit-laravel example snippets


'mailers' => [
    // ...

    'emailit' => [
        'transport' => 'emailit',
    ],
],

use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeEmail;

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

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class WelcomeEmail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public readonly User $user,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Welcome to Our App',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'emails.welcome',
        );
    }
}

use Emailit\Laravel\Facades\Emailit;

// Send an email via the API directly
$email = Emailit::emails()->send([
    'from'    => '[email protected]',
    'to'      => ['[email protected]'],
    'subject' => 'Hello from Emailit',
    'html'    => '<h1>Welcome!</h1>',
]);

echo $email->id;
echo $email->status;

use Emailit\Laravel\Facades\Emailit;

$email = Emailit::emails()->send([
    'from'      => '[email protected]',
    'to'        => '[email protected]',
    'template'  => 'welcome_email',
    'variables' => [
        'name'    => 'John Doe',
        'company' => 'Acme Inc',
    ],
]);

use Emailit\Laravel\Facades\Emailit;

$domain = Emailit::domains()->create(['name' => 'example.com']);
$domains = Emailit::domains()->list();

use Emailit\Laravel\Facades\Emailit;

$contact = Emailit::contacts()->create([
    'email' => '[email protected]',
    'first_name' => 'John',
]);

$contacts = Emailit::contacts()->list();

use Emailit\Laravel\Facades\Emailit;

$result = Emailit::emailVerifications()->verify([
    'email' => '[email protected]',
]);

echo $result->status; // valid
echo $result->risk;   // low

use Emailit\Exceptions\AuthenticationException;
use Emailit\Exceptions\RateLimitException;
use Emailit\Exceptions\ApiErrorException;
use Emailit\Laravel\Facades\Emailit;

try {
    Emailit::emails()->send([...]);
} catch (AuthenticationException $e) {
    // Invalid API key (401)
} catch (RateLimitException $e) {
    // Too many requests (429)
} catch (ApiErrorException $e) {
    // Any other API error
    echo $e->getHttpStatus();
}

use Emailit\EmailitClient;

class EmailController extends Controller
{
    public function send(EmailitClient $emailit)
    {
        $email = $emailit->emails()->send([
            'from'    => '[email protected]',
            'to'      => ['[email protected]'],
            'subject' => 'Hello',
            'html'    => '<p>Hi there!</p>',
        ]);

        return response()->json(['id' => $email->id]);
    }
}
bash
php artisan vendor:publish --tag=emailit-config