src/Jobs/EmailSender/SendEmail.php
public function handle()
    {
        $data = $this->data;
        $email = $this->email;

        $esm = new EmailSenderManager();
        $dbm = new DataBuilderManager();

        $result = $dbm->build($email->data_builder, $data);

        if (!$result->ok()) {
            return event(new EmailFailed($email, $result->getErrors()[0], $this->agent));
        }

        $data = $result->getResource();
        $result = $esm->render($email->data_builder, [
            'body'        => $email->body,
            'subject'     => $email->subject,
            'sender'      => $email->sender,
            'recipients'  => $email->recipients,
            'attachments' => $email->attachments,
        ], $data);

        if (!$result->ok()) {
            return event(new EmailFailed($email, $result->getErrors()[0], $this->agent));
        }

        $bag = new Bag($result->getResource());

        Mail::send([], [], function ($message) use ($bag) {
            $message->to($bag->get('recipients'))
                ->subject($bag->get('subject'))
                ->setBody($bag->get('body'), 'text/html');

            foreach ($bag->get('attachments') as $attachment) {
                if (!empty($attachment['source']) && !empty($attachment['as'])) {
                    $source = file_get_contents($attachment['source']);

                    $message->attach($attachment['source'], ['as' => $attachment['as']]);
                }
            }
        });

        event(new EmailSent($email, $this->agent));
    }
public function handle()
    {
        $data = $this->data;
        $email = $this->email;

        $esm = new EmailSenderManager();
        $dbm = new DataBuilderManager();

        $result = $dbm->build($email->data_builder, $data);

        if (!$result->ok()) {
            return event(new EmailFailed($email, $result->getErrors()[0], $this->agent));
        }

        $data = $result->getResource();
        $result = $esm->render($email->data_builder, [
            'body'        => $email->body,
            'subject'     => $email->subject,
            'sender'      => $email->sender,
            'recipients'  => $email->recipients,
            'attachments' => $email->attachments,
        ], $data);

        if (!$result->ok()) {
            return event(new EmailFailed($email, $result->getErrors()[0], $this->agent));
        }

        $bag = new Bag($result->getResource());

        Mail::send([], [], function ($message) use ($bag) {
            $message->to($bag->get('recipients'))
                ->subject($bag->get('subject'))
                ->setBody($bag->get('body'), 'text/html');

            foreach ($bag->get('attachments') as $attachment) {
                if (!empty($attachment['source']) && !empty($attachment['as'])) {
                    $source = file_get_contents($attachment['source']);

                    $message->attach($attachment['source'], ['as' => $attachment['as']]);
                }
            }
        });

        event(new EmailSent($email, $this->agent));
    }
Execute the job.