Skip to content

Commit

Permalink
Test mailable can have attachments at runtime
Browse files Browse the repository at this point in the history
Unclexo
  • Loading branch information
unclexo authored Jan 6, 2023
2 parents db1720e + 8d8962d commit 4a3873b
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/OrderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class OrderController extends Controller
{
//
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/PostPublishController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Controllers;

use App\Mail\PostPublished;
use App\Models\Post;

class PostPublishController extends Controller
{
public function previewMailTemplate(int $id)
{
$post = Post::find($id);

return (new PostPublished($post))->render();
}
}
57 changes: 57 additions & 0 deletions app/Mail/OrderShipped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Mail;

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

class OrderShipped extends Mailable
{
use Queueable, SerializesModels;

public Order $order;

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

/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
markdown: 'emails.orders.shipped',
with: [
'url' => url("/orders/{$this->order->id}"),
],
);
}

/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [
Attachment::fromPath(storage_path('app/public/some.pdf')),
Attachment::fromStorageDisk('public', 'other.pdf'),
];
}
}
76 changes: 76 additions & 0 deletions app/Mail/PostPublished.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Mail;

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

class PostPublished extends Mailable
{
use Queueable, SerializesModels;

public Post $post;

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

/**
* Get the message envelope.
*
* @return \Illuminate\Mail\Mailables\Envelope
*/
public function envelope()
{
return new Envelope(
from: new Address('[email protected]', 'Medium Online Publication'),
to: new Address('[email protected]', 'Abu Jobaer'),
bcc: new Address('[email protected]', 'Ria Jobaer'),
replyTo: [
new Address('[email protected]', 'Taylor Otwell'),
],
subject: 'Post Published',
tags: ['architecture', 'design-patterns'],
metadata: [
'post_id' => $this->post->id,
],
);
}

/**
* Get the message content definition.
*
* @return \Illuminate\Mail\Mailables\Content
*/
public function content()
{
return new Content(
markdown: 'emails.posts.published',
with: [
'url' => url("/posts/{$this->post->id}"),
],
);
}

/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments()
{
return [];
}
}
11 changes: 11 additions & 0 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

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

class Order extends Model
{
use HasFactory;
}
27 changes: 27 additions & 0 deletions database/factories/OrderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
*/
class OrderFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'user_id' => User::factory()->create(),
'shipper_id' => $this->faker->numberBetween(1, 10),
'name' => $this->faker->userName,
'price' => $this->faker->numberBetween(100, 10000),
];
}
}
35 changes: 35 additions & 0 deletions database/migrations/2023_01_06_152053_create_orders_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('shipper_id');
$table->string('name');
$table->unsignedDecimal('price');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
};
16 changes: 16 additions & 0 deletions resources/views/emails/orders/shipped.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<x-mail::message>
# Order Shipped

Your order has been shipped. The order summary:

Order ID: {{ $order->id }}
Name: {{ $order->name }}
Price: {{ $order->price }}

<x-mail::button :url="$url">
View Order
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
12 changes: 12 additions & 0 deletions resources/views/emails/posts/published.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<x-mail::message>
# Post Published

Your post has been published on Medium

<x-mail::button :url="$url">
View Post
</x-mail::button>

Thanks,<br>
{{ config('app.name') }}
</x-mail::message>
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use App\Http\Controllers\HomeController;
use App\Http\Controllers\MediaUploaderController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\PostPublishController;
use Illuminate\Support\Facades\Route;

/*
Expand Down Expand Up @@ -56,4 +57,9 @@

Route::get('/upload/download/{filename}', 'download')->name('upload.download');
});

Route::prefix('mailable')->group(function() {
Route::get('template/{id}', [PostPublishController::class, 'previewMailTemplate'])
->name('mailable.preview');
});
});
Loading

0 comments on commit 4a3873b

Please sign in to comment.