DDEV with Laravel mail
Installation
This describe steps taken for the project
-
Install Laravel
$ laravel new ddev-smtp-mailhog $ php artisan --version Laravel Framework 8.68.1
-
Configure DDEV project, accepting defaults
$ ddev config Project name (ddev-smpt-mailhog): Docroot Location (public): Project Type (laravel):
-
Start project and confirm "Welcome page" is accessible from browser
ddev launch
-
Configure Laravel for DDEV via updating
.env
# Use default DDEV database
DB_CONNECTION=mysql
DB_HOST=db
DB_DATABASE=db
DB_USERNAME=db
DB_PASSWORD=db
# Use Mailhog
MAIL_MAILER=smtp
MAIL_HOST=localhost
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Testing
-
Create new mailable
php artisan make:mail WelcomeMail
-
Add a view
./resources/views/mail/welcome-mail.blade.php
This is an example
-
Configure the
./app/Mail/WelcomeMail.php
mailable to use the viewpublic function build() { return $this->view('mail.wlecome-mail'); }
-
Create a test route to easier send mail a user
Route::get('/', function () { Mail::to('[email protected]')->send(new WelcomeMail()); return view('welcome'); });
-
Visiting the route will trigger the mail
vendor/laravel/framework/src/Illuminate/Mail/Mailer.php
if ($this->shouldSendMessage($swiftMessage, $data)) { // This following line is run $this->sendSwiftMessage($swiftMessage); $this->dispatchSentEvent($message, $data); }