### Install Mail Sandbox Package via Composer Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md This command installs the mail-sandbox package using Composer. Ensure you have Composer installed and your project is set up as a Laravel application. ```bash composer require ashishdhakal/mail-sandbox ``` -------------------------------- ### Publish Mail Sandbox Configuration and Migrations Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md These commands publish the package's configuration file to config/mail-sandbox.php and migration files for database setup. These are optional steps. ```bash php artisan vendor:publish --tag=mail-sandbox-config php artisan vendor:publish --tag=mail-sandbox-migrations ``` -------------------------------- ### Access Captured Emails via Web Interface (List and Search) Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Demonstrates how to access the list of all captured emails through the web interface and how to perform searches using query parameters. Includes an example of an AJAX request for live search functionality and its expected HTML response. ```bash # View all captured emails in browser GET /mail-sandbox # Search emails by subject, from, to, cc, or attachment names GET /mail-sandbox?search=welcome # AJAX request for live search (returns partial HTML) curl -X GET "http://localhost:8000/mail-sandbox?search=invoice" \ -H "X-Requested-With: XMLHttpRequest" # Response: HTML partial with filtered email list #
# Invoice #12345 # customer@example.com # ... #
``` -------------------------------- ### Download or Preview Email Attachment Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Details how to download or preview a specific attachment from a captured email using its email ID and the attachment's index. Includes an example cURL command to download an attachment and the expected response headers for inline display. ```bash # Download attachment by index (0-based) GET /mail-sandbox/{email_id}/attachments/{index} # Example: Get first attachment from email 42 curl -X GET "http://localhost:8000/mail-sandbox/42/attachments/0" \ --output attachment.pdf # Response headers: # Content-Type: application/pdf # Content-Disposition: inline; filename="invoice.pdf" ``` -------------------------------- ### Run Mail Sandbox Package Tests with Pest Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md This command executes the test suite for the mail-sandbox package. It requires the development dependencies to be installed via Composer and utilizes the Pest testing framework. ```bash composer test ``` -------------------------------- ### View Specific Captured Email Details Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Provides the method to view the detailed content of a specific captured email using its unique ID. Includes an example using cURL to fetch the email details, which includes an iframe for content preview. ```bash # View email details by ID GET /mail-sandbox/{email_id} # Example: View email with ID 5 curl -X GET "http://localhost:8000/mail-sandbox/5" # Response: Full page with email headers and iframe for content preview ``` -------------------------------- ### Retrieve Processed HTML Content of Captured Email Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Explains how to get the rendered HTML body of a captured email, suitable for embedding in an iframe. This endpoint resolves CID attachments to local URLs, ensuring embedded images are displayed correctly. Includes an example cURL request and the expected HTML response structure. ```bash # Get rendered HTML content for iframe embedding GET /mail-sandbox/{email_id}/content # Example curl -X GET "http://localhost:8000/mail-sandbox/42/content" \ -H "Accept: text/html" # Response: text/html # # # #

Welcome to Our Service

# #

Thank you for signing up!

# # ``` -------------------------------- ### Run Mail Sandbox Database Migrations Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md This Artisan command executes the database migrations published by the mail-sandbox package, setting up the necessary tables to store captured emails. ```bash php artisan migrate ``` -------------------------------- ### Configure Mail Driver and Environment Variables Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Sets up the 'sandbox' mailer in Laravel's mail configuration and defines environment variables to control email capture and UI enablement. The capture and UI are automatically disabled in production environments. ```php // config/mail.php 'mailers' => [ // ... other mailers 'sandbox' => [ 'transport' => 'sandbox', ], ], ``` ```dotenv # .env MAIL_MAILER=sandbox # Optional: Control capture and UI independently MAIL_SANDBOX_CAPTURE=true # Enable email interception (default: true in non-production) MAIL_SANDBOX_UI=true # Enable web interface (default: true in non-production) ``` -------------------------------- ### Mail Sandbox Web Interface Routes Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This section details the available routes for interacting with the Mail Sandbox web interface, allowing you to list, view, and manage captured emails. ```APIDOC ## GET /mail-sandbox ### Description Lists all captured emails with search and pagination support. ### Method GET ### Endpoint /mail-sandbox ### Query Parameters - **search** (string) - Optional - Search emails by subject, from, to, cc, or attachment names. ### Request Example ```bash # View all captured emails GET /mail-sandbox # Search emails by subject GET /mail-sandbox?search=welcome # AJAX request for live search curl -X GET "http://localhost:8000/mail-sandbox?search=invoice" -H "X-Requested-With: XMLHttpRequest" ``` ### Response #### Success Response (200) - **HTML** - Returns an HTML partial with a filtered list of emails for AJAX requests, or a full page for standard requests. #### Response Example (AJAX Search) ```html
Invoice #12345 customer@example.com ...
``` ## GET /mail-sandbox/{email_id} ### Description Displays a specific captured email with full details, including an HTML preview. ### Method GET ### Endpoint /mail-sandbox/{email_id} ### Path Parameters - **email_id** (integer) - Required - The unique identifier of the email to view. ### Request Example ```bash # View email details by ID GET /mail-sandbox/5 # Example using curl curl -X GET "http://localhost:8000/mail-sandbox/5" ``` ### Response #### Success Response (200) - **HTML** - A full HTML page displaying email headers and an iframe for content preview. ## GET /mail-sandbox/{email_id}/content ### Description Retrieves the processed HTML body of an email, with CID attachments resolved to local URLs. ### Method GET ### Endpoint /mail-sandbox/{email_id}/content ### Path Parameters - **email_id** (integer) - Required - The unique identifier of the email. ### Request Example ```bash # Get rendered HTML content for iframe embedding GET /mail-sandbox/42/content # Example using curl curl -X GET "http://localhost:8000/mail-sandbox/42/content" -H "Accept: text/html" ``` ### Response #### Success Response (200) - **text/html** - The rendered HTML content of the email. #### Response Example ```html

Welcome to Our Service

Thank you for signing up!

``` ## GET /mail-sandbox/{email_id}/attachments/{index} ### Description Downloads or previews an email attachment by its index position. ### Method GET ### Endpoint /mail-sandbox/{email_id}/attachments/{index} ### Path Parameters - **email_id** (integer) - Required - The unique identifier of the email. - **index** (integer) - Required - The 0-based index of the attachment to retrieve. ### Request Example ```bash # Get first attachment from email 42 GET /mail-sandbox/42/attachments/0 # Example using curl to download curl -X GET "http://localhost:8000/mail-sandbox/42/attachments/0" \ --output attachment.pdf ``` ### Response #### Success Response (200) - **File** - The content of the attachment. #### Response Headers - **Content-Type** (string) - The MIME type of the attachment (e.g., `application/pdf`). - **Content-Disposition** (string) - Specifies how to display the attachment (e.g., `inline; filename="invoice.pdf"`). ## DELETE /mail-sandbox ### Description Deletes all captured emails from the database. ### Method DELETE ### Endpoint /mail-sandbox ### Response #### Success Response (204) - No content is returned upon successful deletion. ``` -------------------------------- ### Activate Mail Sandbox via .env File Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md Setting MAIL_MAILER to 'sandbox' in your .env file ensures that your Laravel application uses the mail-sandbox driver for sending emails, enabling the capture functionality. ```bash MAIL_MAILER=sandbox ``` -------------------------------- ### Query Captured Emails using MailSandboxMessage Model Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This PHP code demonstrates how to programmatically access and query captured emails using the `MailSandboxMessage` Eloquent model. It shows how to retrieve all emails, search by subject, access email components like subject, recipients, body, and attachments, clear all emails, and delete old emails. ```php get(); // Search by subject $welcomeEmails = MailSandboxMessage::where('subject', 'like', '%Welcome%')->get(); // Get email with attachments $email = MailSandboxMessage::find(1); echo $email->subject; // "Welcome to Our Service" echo $email->to; // "user@example.com" echo $email->from; // "noreply@company.com" echo $email->cc; // "support@company.com" echo $email->bcc; // "admin@company.com" echo $email->body_html; // "..." echo $email->body_text; // "Plain text version..." // Access attachments (automatically cast to array) foreach ($email->attachments as $attachment) { echo $attachment['name']; // "logo.png" echo $attachment['content_type']; // "image/png" echo $attachment['size']; // 12345 (bytes) echo $attachment['cid']; // "logo@company.com" (for inline images) // Decode attachment content $binary = base64_decode($attachment['content']); file_put_contents('/tmp/' . $attachment['name'], $binary); } // Clear all emails MailSandboxMessage::truncate(); // Delete emails older than 7 days MailSandboxMessage::where('created_at', '<', now()->subDays(7))->delete(); ``` -------------------------------- ### Customize Mail Sandbox Package Configuration Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Defines the core behavior of the Mail Sandbox package, including enabling/disabling email capture and the web UI, setting the URL path for the interface, and specifying middleware for route protection. ```php // config/mail-sandbox.php return [ // Enable/disable email interception (auto-disabled in production) 'capture_enabled' => env('MAIL_SANDBOX_CAPTURE', config('app.env') !== 'production'), // Enable/disable web UI (auto-disabled in production) 'ui_enabled' => env('MAIL_SANDBOX_UI', config('app.env') !== 'production'), // URL path for the web interface (e.g., /mail-sandbox) 'path' => 'mail-sandbox', // Middleware applied to all sandbox routes 'middleware' => ['web'], ]; ``` -------------------------------- ### Send Emails using Laravel Mail Facade Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This section shows how to send emails using Laravel's Mail facade, which will be automatically intercepted by the Mail Sandbox package when active. It covers sending simple text emails, emails using Mailable classes, and emails with attachments. No emails are actually sent to recipients; they are captured by the sandbox. ```php to('user@example.com') ->subject('Test Email'); }); // Mailable class Mail::to('customer@example.com') ->cc('support@company.com') ->bcc('admin@company.com') ->send(new WelcomeEmail($user)); // With attachments Mail::send('emails.invoice', ['invoice' => $invoice], function ($message) use ($pdfPath) { $message->to('billing@customer.com') ->subject('Invoice #' . $invoice->number) ->attach($pdfPath, [ 'as' => 'invoice.pdf', 'mime' => 'application/pdf', ]); }); // Email is now captured and viewable at /mail-sandbox // No email is actually sent to the recipient ``` -------------------------------- ### Configure Mail Sandbox Settings Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md This is the default configuration for the mail-sandbox package found in config/mail-sandbox.php. It controls email capturing and UI visibility based on environment variables and the application's environment. ```php return [ 'capture_enabled' => env('MAIL_SANDBOX_CAPTURE', config('app.env') !== 'production'), 'ui_enabled' => env('MAIL_SANDBOX_UI', config('app.env') !== 'production'), 'path' => 'mail-sandbox', 'middleware' => ['web'], ]; ``` -------------------------------- ### Configure Mailer to Use Sandbox Driver Source: https://github.com/ashish-dhakal/laravel-mailsandbox-package/blob/main/README.md This snippet shows how to add a custom 'sandbox' mailer to your Laravel application's config/mail.php file. This directs outgoing emails to be handled by the mail-sandbox transport. ```php 'mailers' => [ 'sandbox' => [ 'transport' => 'sandbox', ], ], ``` -------------------------------- ### Verify Email Sending in Feature Tests using Mail Sandbox Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This PHP code demonstrates how to test email sending functionality within a Laravel feature test suite. It utilizes the Mail Sandbox package to capture and assert properties of sent emails, such as recipients, subjects, and body content, without actually sending them externally. It also includes assertions for email attachments. ```php create(); // Act Mail::to('customer@example.com')->send(new OrderConfirmation($order)); // Assert $this->assertDatabaseCount('mail_sandbox_emails', 1); $email = MailSandboxMessage::first(); $this->assertEquals('customer@example.com', $email->to); $this->assertStringContainsString('Order Confirmation', $email->subject); $this->assertStringContainsString($order->number, $email->body_html); } public function test_invoice_attachment_is_included(): void { // Act Mail::send('emails.invoice', [], function ($message) { $message->to('billing@example.com') ->subject('Invoice') ->attach(storage_path('invoices/test.pdf')); }); // Assert $email = MailSandboxMessage::first(); $this->assertNotEmpty($email->attachments); $this->assertEquals('test.pdf', $email->attachments[0]['name']); $this->assertEquals('application/pdf', $email->attachments[0]['content_type']); } } ``` -------------------------------- ### Mail Sandbox Emails Table Schema Definition Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This PHP code defines the database schema for the `mail_sandbox_emails` table, which is created by the package's migration. It details the columns used to store email information, including subject, recipients, body content (HTML and plain text), and attachments in JSON format. ```php id(); $table->string('subject')->nullable(); // Email subject line $table->text('to')->nullable(); // Comma-separated recipients $table->text('from')->nullable(); // Sender address $table->text('cc')->nullable(); // CC recipients $table->text('bcc')->nullable(); // BCC recipients $table->longText('body_html')->nullable(); // HTML body content $table->longText('body_text')->nullable(); // Plain text body content $table->json('attachments')->nullable(); // JSON array of attachments $table->timestamps(); // created_at, updated_at }); // Attachment JSON structure example: // [ // { // "name": "document.pdf", // "cid": null, // "content_type": "application/pdf", // "size": 102400, // "content": "base64encodedcontent..." // }, // { // "name": "logo.png", // "cid": "logo@company.com", // "content_type": "image/png", // "size": 5120, // "content": "base64encodedcontent..." // } // ] ``` -------------------------------- ### Clear All Captured Emails Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt Provides the method to delete all stored captured emails from the database via a DELETE request to the /mail-sandbox endpoint. ```bash # Clear inbox (DELETE request) DELETE /mail-sandbox ``` -------------------------------- ### Delete All Captured Emails using cURL Source: https://context7.com/ashish-dhakal/laravel-mailsandbox-package/llms.txt This snippet demonstrates how to delete all captured emails using a cURL command. It sends a DELETE request to the /mail-sandbox endpoint with the necessary CSRF token. The expected response is a redirect to /mail-sandbox with a success flash message. ```bash curl -X DELETE "http://localhost:8000/mail-sandbox" \ -H "X-CSRF-TOKEN: your-csrf-token" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.