In Laravel, the Mail class and SMTP (Simple Mail Transfer Protocol) are integral parts of the framework's email functionality. Here's a brief explanation of each:
a. Mail Class:
b. SMTP (Simple Mail Transfer Protocol):
1) Setting up Gmail SMTP in Laravel:
Example :
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=sndpbagg@gmail.com
MAIL_PASSWORD=vezknaguuqigzez
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=sndpbagg@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
php artisan make:mail TestMail
Route::POST('job_applications',[MailController ::class,'job_applied'])->name('job_applied');
Create a new controller named MailController using the following command:
php artisan make:controller MailController
Open the MailController located at app/Http/Controllers/MailController.php, and add a method named sendEmail:
import :
use Illuminate\Support\Facades\Mail;
use App\Mail\TestMail;
// notification Email wihen apply job application
$employer = User::where('id',$employer_id)->first();
$mailData = [
'job' => $job,
'user' => Auth::user(),
'employer'=>$employer
];
Mail::to($employer->email)->send(new TestMail($mailData));
5) Go To TestMail class and open (app→mail→TestMail)
here you can see 4 method -
public $mailData;
public function __construct($mailData)
{
$this -> mailData = $mailData;
}
2.envelope() - here write email Subject
public function envelope()
{
return new Envelope(
subject: 'Job Notification Mail',
);
}
public function content()
{
return new Content(
view: 'email.job-notification-email', // this is view page path which is send by email
);
}
make a view page which is send by email and all details . view page name is job-notification-email.blade.php
<body>
<h2>Hello {{$mailData['employer']->name}}</h2>
<p>Title : {{$mailData['job']->title}}</p>
<p>Employee Details :</p>
<p>Name : {{$mailData['user']->name}} </p>
<p>Email : {{$mailData['user']->email}} </p>
<p>Mobile : {{$mailData['user']->mobile}} </p>
</body>
This setup allows you to send emails using Gmail SMTP in Laravel by leveraging the Mail class and configuring SMTP settings. The controller method triggers the email sending process, and the Mail class handles the creation and delivery of the email using the specified view file for the email content.
I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.
helllo