> For the complete documentation index, see [llms.txt](https://idt-nida.gitbook.io/developer-guideline/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://idt-nida.gitbook.io/developer-guideline/laravel/feature/mailables.md).

# Mailables

## 1️⃣ Database Migrations

{% code title="create\_envelopes\_table.php" %}

```php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('envelopes', function (Blueprint $table) {
            $table->id();
            $table->string('subject');
            $table->text('body');
            $table->enum('type', ['user', 'admin']);
            $table->boolean('is_sendmail');
            $table->unsignedBigInteger('status_id');
            $table->foreign('status_id')->references('id')->on('statuses');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('envelopes');
    }
};
```

{% endcode %}

{% code title="create\_statuses\_table.php" %}

```php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('statuses', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('user');
            $table->string('notes');
            $table->string('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('statuses');
    }
};
```

{% endcode %}

## 2️⃣ Model

{% code title="Models/Envelopes.php" %}

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Envelopes extends Model
{
    use HasFactory;

    protected $fillable = [
        'subject',
        'body',
        'type',
        'is_sendmail',
        'status_id',
    ];
}
```

{% endcode %}

{% code title="Models/Status.php" %}

```php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Status extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'user',
        'notes',
        'description'
    ];
}
```

{% endcode %}

## 3️⃣ Mailable Class

{% code title="Mail/UserEmail.php" %}

```php
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use App\Models\Envelopes;
use App\Models\Statuses;

class UserEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     */
    public function __construct($status_id)
    {
        $data = Envelopes::join('statuses', 'envelopes.status_id', '=', 'statuses.id')->where('envelopes.id', $status_id)->select('envelopes.*', 'statuses.name as status_name')->first();
        $this->subject = $data->subject;
        $this->body = $data->body;
        $this->status = $data->status_name;
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'NIDA: ' . $this->subject,
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            markdown: 'emails.user',
            with: ['subject' => $this->subject, 'body' => $this->body, 'status' => $this->status]
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
    }
}
```

{% endcode %}

## 4️⃣ Controller

{% code title="SendmailController.php" %}

```php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\UserEmail;
use App\Models\Envelopes;

class SendmailController extends Controller
{
    public function update($email, $id, $status_id)
    {
        $envelope = Envelopes::find($status_id);

        $envelope->is_sendmail == 1 ? Mail::to($email)->send(new UserEmail($status_id)) : '';
        return True;
    }
}
```

{% endcode %}

## 5️⃣ View

{% code title="views/emails/user.blade.php" %}

```php
<x-mail::message>
# สถานะ: {!! $status !!}<br>
# {!! $subject !!}<br>

เรียน ({{ Auth::user()->name }})

{!! $body !!}

<x-mail::button :url="'https://nida.ac.th/'">
Link
</x-mail::button>

<hr>

ขอแสดงความนับถือ<br>
สถาบันบัณฑิตพัฒนบริหารศาสตร์<br>
https://idt.nida.ac.th/<br>
0 2727 3777, 0 2727 3778<br>

</x-mail::message>
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://idt-nida.gitbook.io/developer-guideline/laravel/feature/mailables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
