How to send email in Laravel 11
To send email in laravel 11 you have to first install laravel 11 application using composer create-project laravel/laravel demo
command. Now can follow below instructions step by step to send a email in Laravel 11.
Add mail configuration deatils in your .env file. Basically we have to add Outgoing Server details for MAIL_HOST and port and mail_encryption as per your mail account details.
MAIL_MAILER=smtp
MAIL_HOST=mail.yourwebsite.com
MAIL_PORT=465
MAIL_USERNAME=mail_username
MAIL_PASSWORD=mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mail_from_address"
MAIL_FROM_NAME="${APP_NAME}"
Create mail using php artisan make:mail TestMail
command and it will a file app\Mail\TestMail.php in your laravel project. Paste below code in it.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*/
public function __construct(public User $user)
{
//
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Test Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'mail.test',
with: [
'userName' => $this->user->name,
'userEmail' => $this->user->email,
],
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Create a controller named TestController in /app/Http/Controllers
directory and paste below code in it. You can also create controller using php artisan make:controller TestController
command and make changes in file as per below code. You have atleast one user in your users table and enter your email address in place of testing@example.com
in Mail::to('testing@example.com')->send(new TestMail($user));
.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use App\Mail\TestMail;
use Illuminate\Support\Facades\Mail;
class TestController extends Controller
{
//
public function sendEmail(){
$user = User::first();
try{
$mail = Mail::to('testing@example.com')->send(new TestMail($user));
echo "Mail has been sent successfully";
}catch(\Exception $e){
echo $e->getMessage();
Log::info(['Mal error on account approval'=> 'Mail not sent successfully']);
}
}
}
Add below code in your web.php file under routes directory
use App\Http\Controllers\TestController;
Route::get('/test-mail', [TestController::class, 'sendEmail'])->name('sendEmail');
Create a test.blade.php file in resources/views/mail
directory and place below code in it.
<div>
Name: {{ $userName }}
Email: {{ $userEmail }}
</div>
Now you can visit https://yourwebsite.com/test-mail
URL to see the output.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Link storage folder in laravel 8
- How to access the nth object from Laravel collection object ?
- Insert values in pivot table dynamically in laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- How to pass query string with pagination in laravel
- How to start websocket server in laravel
- Print last executed query in laravel
- Laravel pagination links with query string
- How to get specific columns using with method in laravel Eloquent relationship
- Display option of select as selected with blade directive Laravel
- How to create and run user seeder in laravel
- Recursive function example code PHP Laravel
- How to get random string in Laravel
- Attempt to read property "avatar" on null in Laravel
- How to call model in blade laravel
- How to check if user has created any post or not in laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- Json encode method in laravel
- How to remove package from laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- Laravel 11 project setup on localhost using breeze with blade step by step
- How to disable timestamps in laravel
- How to get specific columns using Laravel eloquent methods
- Import/Use Storage facade in laravel