Testing: Use Mailtrap.io and update .env file to get started within minutes
php artisan make:mail ProjectCreated --markdown="emails.project-created"
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\ProjectCreated;
use App\Project;
use Mail;
class ProjectsController extends Controller
{
// ...
// ...
public function store() {
// ...
$project = Project::create($attributes);
Mail::to($project->owner->email)->send(
new ProjectCreated($project)
);
return redirect('/projects');
}
// ...
}
<?php // Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $fillable = [
'title',
'description'
];
public function owner() {
return $this->belongsTo(User::class);
}
// ...
// ...
}
<?php
namespace App\Mail;
// ...
// ...
class ProjectCreated extends Mailable
{
use Queueable, SerializesModels;
public $project;
// public $foo = 'bar';
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($project)
{
$this->project = $project;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->markdown('emails.project-created');
}
}