PHP code example of sfneal / post-office

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

    

sfneal / post-office example snippets

 php
php artisan vendor:publish --provider="Sfneal\PostOffice\Providers\PostOfficeServiceProvider"
 php
use Sfneal\PostOffice\Mailables\Interfaces\CallToAction;
use Sfneal\PostOffice\Mailables\Interfaces\Email;
use Sfneal\PostOffice\Mailables\Interfaces\Greeting;
use Sfneal\PostOffice\Mailables\Interfaces\Message;
use Sfneal\PostOffice\Mailables\Interfaces\Title;
use Sfneal\PostOffice\Mailables\Mailable;
use Sfneal\PostOffice\Mailables\Traits\UserMailable;
use Sfneal\Users\Models\User;

class InvoiceUnpaidMailable extends Mailable implements Greeting, Email, Title, Message, CallToAction
{
    /**
     * @var User
     */
    private $user;

    /**
     * @var int
     */
    private $invoice_id;

    /**
     * InvoicePaidMailable constructor.
     *
     * @param User $user
     * @param int $invoice_id
     */
    public function __construct(User $user, int $invoice_id)
    {
        $this->user = $user;
        $this->invoice_id = $invoice_id;
        parent::__construct(
            $this->getGreeting(),
            $this->getEmail(),
            $this->getTitle(),
            $this->getMessages(),
            $this->getCallToAction()
        );
    }
    
    /**
     * @return string First line of email
     */
    public function getGreeting(): string
    {
        return "Hi {$this->user->first_name}";
    }

    /**
     * Email recipient.
     *
     * @return string
     */
    public function getEmail(): string
    {
        return $this->user->email;
    }

    /**
     * Retrieve the Mailable's subject/title.
     *
     * @return string
     */
    public function getTitle(): string
    {
        return "Unpaid Invoice: #{$this->invoice_id}";
    }

    /**
     * Retrieve an array of messages to 
 php
use Sfneal\PostOffice\Notifications\Notification;
use Sfneal\Users\Models\User;

class InvoiceUnpaidNotification extends Notification
{
    /**
     * @var User
     */
    public $user;

    /**
     * @var int
     */
    public $invoice_id;

    /**
     * InvoicePaidMailable constructor.
     *
     * @param User $user
     * @param int $invoice_id
     */
    public function __construct(User $user, int $invoice_id)
    {
        $this->user = $user;
        $this->invoice_id = $invoice_id;
        parent::__construct();
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return InvoiceUnpaidMailable
     */
    public function toMail($notifiable): InvoiceUnpaidMailable
    {
        return (new InvoiceUnpaidMailable($this->user, $this->invoice_id))->to($notifiable->email);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable): array
    {
        return [
            'user_id' => $this->user->getKey(),
            'invoice_id' => $this->invoice_id,
        ];
    }
}
 php
$notification = new InvoiceUnpaidNotification($user, $invoice_id);

// Send using the Job queue
$notification->send($user);

// Send synchronously
$notification->sendNow($user);