Sending email has become an essential part of modern web applications. For example, they are a great way to communicate with users, when they register, when verifying registrations, and when resetting passwords.
You will get this things by default in laravel .env file
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="funystory23654@gmail.com"
MAIL_PASSWORD="kzfk zsvj xsgx pars"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="wordpress12389@gmail.com"
MAIL_FROM_NAME="${APP_NAME}"
cmd
php artisan make:mail Demomail
you will get this file here: app/Mail/Demomail.php
Demomail.php
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*/
public function __construct($mailData)
{
$this->mailData = $mailData; // add this tings
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Demomail',
);
}
public function content(): Content
{
return new Content(
view: 'mails.testmail', // this is the path of view of email template
); // add this thing also
}
php artisan make:controller Mailcontroller
Mailcontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail; // add this thing for sending mail][]
use App\Mail\Demomail; // add this path of Demomail.php file
class Mailcontroller extends Controller
{
function index()
{
$mailData = [
'title' => 'This mail from pktechnology.in',
'body' => 'This is timepass mail body',
];
Mail::to('upwork12389@gmail.com')->send(new Demomail($mailData));
dd("Email sent successfully !");
}
}
web.php
Route::get('send-mail', [Mailcontroller::class, 'index']);
testmail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Email Sending</title>
</head>
<body>
The title of this email is {{$mailData['title']}} and body is {{$mailData['body']}}
</body>
</html>