\app\Http\Controllers\TestController.php This is the file which will invoke email sending logic.
php artisan make:mail TestEmailSender
- The above command will create \app\Mail\TestEmailSender.php with below code
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestEmailSender extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('view.name');
}
}
- Let's Modify TestEmailSender.php with below code
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Config;
class TestEmailSender extends Mailable
{
use Queueable, SerializesModels;
private $emailParams;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($params)
{
$this->emailParams = $params;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
$this->from(Config::get('app.senderEmail'),Config::get('app.senderName'))
->subject($this->emailParams->subject)
->view('mail.TestEmail')
->with(['emailParams' => $this->emailParams]);
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
use App\Mail\TestEmailSender;
use Illuminate\Support\Facades\Log;
class TestController extends Controller
{
public $name;
public $email;
public function __construct(){}
public function sendEmail()
{
$this->name = "Deepak"; //recipient name
$this->email = "deepak.cotocus@gmail.com"; //recipient email id
/**
*creating an empty object of of stdClass
*
*/
$emailParams = new \stdClass();
$emailParams->usersName = $this->name;
$emailParams->usersEmail = $this->email;
$emailParams->subject = "Testing Email sending feature";
Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams));
}
public function test(){
$this->sendEmail();
}
}
- In The above code
Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams));
is invocking email sending logic and passing user's name and user's email along with Subject of email which is assigned to $emailParams object.
NOTE
Mail::to($emailParams->usersEmail)->send(new TestEmailSender($emailParams));
This line will invoke build() function of TestEmailSender and an email with subject and message will be sent to usersEmail successfully.
Welcome, {{ $emailParams->usersName }}
Sending mail for Testing Purpose.
Thank You
Route::get('/sendemail', 'TestController@test')->name('sendemail');
- open your .env file and checkout for this settings:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<your-Gmail-Id>
MAIL_PASSWORD=yorgeneretedpassword
MAIL_ENCRYPTION=tls
MAIL_FROM=<Sender's-Gmail-Id>
MAIL_FROM_NAME=<Sender's-Name>
- open /config/app.php and add below 2 lines in retun statement
return [
'senderEmail' => env('MAIL_FROM', '<Sender's-Gmail-Id>),
'senderName' => env('MAIL_FROM_NAME', '<Sender's-Name>'),
];
NOTE- Check Out for Gmail Email server set up
- All Done! Open your browser and hit the below url http://localhost:8000/sendemail and check your mail box.



MotoShare.in is your go-to platform for adventure and exploration. Rent premium bikes for epic journeys or simple scooters for your daily errands—all with the MotoShare.in advantage of affordability and ease.