Category : #laravel
Tags : #laravel, #laravel mail
In this Laravel 9 email tutorial, I will help you to let you know of Laravel 9 send email example. From this Laravel 9 email tutorial, you will learn Laravel 9 send mail example. I will explain step by step explain send emails in Laravel 9 SMTP. We will look at an example of Laravel 9 send mail SMTP example.
Laravel 9 provides a predefined mail configuration for sending emails. We can use several drivers for sending emails in Laravel 9. We can use SMTP, Mailgun, Postmark, and Amazon SES, and send email. We have to configure on the env file what driver we are going to use to send email in the Laravel 9 application.
In this example, I will give you step-by-step instructions to send emails in Laravel 9. We can create a blade file design and also with dynamic information for mail layout. So let's see the step-by-step process and send an email from your Laravel applications.
Step 1: Install Laravel 9
If you do not have the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Make Configuration
In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, and mail password so Laravel 9 will use that sender configuration for sending an email. So you can simply add as like following.
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_mail_address
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Read also: Laravel 9 Throttle Rate Limiters Middleware Example
Step 3: Create Mail
There is an artisan command to create a mail class in Laravel. We can run make:mail
command to create a mail class. Run the below command to create a mail class.
php artisan make:mail DemoMail
now, let's update the code on the DemoMail.php file as below:
app/Mail/DemoMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DemoMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from laravelia.com')
->view('emails.demoMail');
}
}
Step 4: Create Controller
In this step, we will create MailController with the index() method where we write code for sending mail to the given email address. so first let's create a controller by following the command and updating the code on it.
php artisan make:controller MailController
Now, update the code on the MailController file.
app/Http/Controllers/MailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from laraveia.com',
'body' => 'This is for testing email using smtp.'
];
Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
Step 5: Add Routes
In this step, we need to create routes for a list of sending emails. so open your "routes/web.php" file and add the following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('send-mail', [MailController::class, 'index']);
Step 6: Create Email View
In this step, we will create a blade view file and write the email that we want to send. now we just write some dummy text. create the below files in the "emails" folder.
resources/views/emails/demoMail.blade.php
<!DOCTYPE html>
<html>
<head>
<title>laravelia.com</title>
</head>
<body>
<h1>{{ $mailData['title'] }}</h1>
<p>{{ $mailData['body'] }}</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Thank you</p>
</body>
</html>
Now you can test by visiting the below URL:
url
Laravel Mail with Attachment
To add attachments to an email, use the attach
a method within the mailable class build
method.
app/Mail/DemoMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DemoMail extends Mailable
{
use Queueable, SerializesModels;
public $mailData;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($mailData)
{
$this->mailData = $mailData;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->subject('Mail from laravelia.com')
->view('emails.demoMail')
->attach('/path/to/file');
}
}
We can define the MIME type by passing an array
as the second argument to the attach
method:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attach('/path/to/file', [
'as' => 'name.pdf',
'mime' => 'application/pdf',
]);
}
Attaching Files From Disk
If you have stored a file on one of your storage, we can attach it to the email using the attachFromStorage
method:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachFromStorage('/path/to/file');
}
Here we can also define MIME type like:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachFromStorage('/path/to/file', 'name.pdf', [
'mime' => 'application/pdf'
]);
}
The attachFromStorageDisk
method can be used if we need to specify a storage disk other than your default disk:
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.orders.shipped')
->attachFromStorageDisk('s3', '/path/to/file');
}
Inline Attachments in Laravel Mail
We can attach an inline image in our Laravel 9 mail template. Laravel provides embed method on the $message
variable within your email template like:
<body>
Here is an image:
<img src="{{ $message->embed($pathToImage) }}">
</body>
Remember:
$message
variable is not available in Laravel mail template in plain-text message since plain-text messages do not utilize inline attachments.
Embedding Raw Data Attachments
We can attach a raw data attachment or image in our Laravel 9 mail template. Laravel provides embedData method on the $message
variable within your email template like:
<body>
Here is an image from raw data:
<img src="{{ $message->embedData($data, 'example-image.jpg') }}">
</body>
Read also: Central Place Of Laravel | Service Provider Explanation
Hope it can help you.