December 7, 2016 —John Koster
The make:mail
command is used to generate a new mail class. A name
must be supplied to the make:mail
command; this value will be the name of the newly generated class.
The following would generate a new mail class named UserSubscribedMail
mail class:
1# Generate a new UserSubscribedMail class.2php artisan make:make UserSubscribedMail
After the command has been executed, a new file will be created at app/Mail/UserSubscribedMail.php
. The file will contain a new class named UserSubscribedMail
. The following would be the generated class:
1<?php 2 3namespace App\Mail; 4 5use Illuminate\Bus\Queueable; 6use Illuminate\Mail\Mailable; 7use Illuminate\Queue\SerializesModels; 8 9class UserSubscribedMail extends Mailable10{11 use Queueable, SerializesModels;12 13 /**14 * Create a new message instance.15 *16 * @return void17 */18 public function __construct()19 {20 //21 }22 23 /**24 * Build the message.25 *26 * @return $this27 */28 public function build()29 {30 return $this->view('view.name');31 }32}
If the provided class name has already been taken by another mail class, the command will issue an error stating something similar to "Mail already exists!". Additionally, this command is capable of handling nested namespaces and directories; to take advantage of this, separate the namespace parts and class name with a directory separator:
1# Generate a new UserSubscribedMail class in a nested directory2# with the namespace already set on the generated class.3php artisan make:mail Users/UserSubscribedMail
∎