PHP code example of buildcode / laravel-database-emails

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

    

buildcode / laravel-database-emails example snippets


protected function schedule(Schedule $schedule)
{
     $schedule->command('email:send')->everyMinute()->withoutOverlapping(5);
}

use Stackkit\LaravelDatabaseEmails\Email;
use Illuminate\Mail\Mailables\Content;
use Stackkit\LaravelDatabaseEmails\Attachment;
use Illuminate\Mail\Mailables\Envelope;

Email::compose()
    ->content(fn (Content $content) => $content
        ->view('tests::dummy')
        ->with(['name' => 'John Doe'])
    )
    ->envelope(fn (Envelope $envelope) => $envelope
        ->subject('Hello')
        ->from('[email protected]', 'John Doe')
        ->to('[email protected]', 'Jane Doe')
    )
    ->attachments([
        Attachment::fromStorageDisk('s3', '/invoices/john-doe/march-2024.pdf'),
    ])
    ->send();
])

Email::compose()
    ->user($user)
    ->send();

class User extends Model
{
    public function preferredEmailName(): string
    {
        return $this->first_name;
    }
}

class User extends Model
{
    public function preferredEmailAddress(): string
    {
        return $this->work_email;
    }
}

class User extends Model implements HasLocalePreference
{
    public function preferredLocale(): string
    {
        return $this->locale;
    }
}

Email::compose()
    ->mailable(new OrderShipped())
    ->send();

use Stackkit\LaravelDatabaseEmails\Attachment;

Email::compose()
    ->attachments([
        Attachment::fromPath(__DIR__.'/files/pdf-sample.pdf'),
        Attachment::fromPath(__DIR__.'/files/my-file.txt')->as('Test123 file'),
        Attachment::fromStorageDisk('my-custom-disk', 'test.txt'),
    ])
    ->send();

Email::compose()
    ->model(User::find(1));

Email::compose()
    ->later('+2 hours');

Email::compose()->queue();

// On a specific connection
Email::compose()->queue(connection: 'sqs');

// On a specific queue
Email::compose()->queue(queue: 'email-queue');

// Delay (send mail in 10 minutes)
Email::compose()->queue(delay: now()->addMinutes(10));

Email::compose()->queue(jobClass: CustomSendEmailJob::class);



namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Stackkit\LaravelDatabaseEmails\SendEmailJob;

class CustomSendEmailJob extends SendEmailJob implements ShouldQueue
{
    // Define custom retries, backoff, etc...
}

use Stackkit\LaravelDatabaseEmails\Email;

$schedule->command('model:prune', [
    '--model' => [Email::class],
])->daily();

use Stackkit\LaravelDatabaseEmails\Email;

public function register(): void
{
    Email::pruneWhen(function (Email $email) {
        return $email->where(...);
    });
}
shell
php artisan vendor:publish --tag=database-emails-config
php artisan vendor:publish --tag=database-emails-migrations
shell
php artisan migrate