<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class Example extends Mailable
{
  use Queueable, SerializesModels;

  public $data;

  /**
   * Create a new message instance.
   *
   * @return void
   */
  public function __construct($data)
  {
    $this->data = $data;
  }

  /**
   * Build the message.
   *
   * @return $this
   */
  public function build()
  {
    $email_log = new \App\Models\EmailLog();
    $email_log->to = $this->data['to'];
    $email_log->view = 'emails.example';
    unset($this->data['to']);
    $email_log->data = json_encode($this->data, JSON_UNESCAPED_UNICODE);
    $email_log->save();
    return $this->view('emails.example')->subject('Example Email')->with('data', $this->data);
  }
}
