How to send mail in laravel 6
In Laravel 6, you can send mail using the Mail
facade or the Illuminate\Support\Facades\Mail
class. Here are the steps to send a mail:
Using the Mail
facade:
- First, you need to create a mail class that extends
Illuminate\Mail\Mailable
. For example, let's create aWelcomeEmail
class:// app/Mail/WelcomeEmail.php
namespace App\Mail;
use Illuminate\Mail\Mailable;
class WelcomeEmail extends Mailable { public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->markdown('emails.welcome');
}
}
In this example, we're creating a `WelcomeEmail` class that takes a `user` object in its constructor. The `build` method returns a `Mailable` instance that uses the `markdown` method to render the email template.
2. Next, you need to create an email template in the `resources/views/emails` directory. For example, let's create a `welcome.blade.php` file:
```php
<!-- resources/views/emails/welcome.blade.php -->
Hello {{ $user->name }},
Welcome to our application!
Best regards,
[Your Name]
- Now, you can use the
Mail
facade to send the email:// app/Http/Controllers/WelcomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Mail\WelcomeEmail;
class WelcomeController extends Controller { public function sendWelcomeEmail(Request $request) { $user = User::find(1); // Replace with your user model and ID
Mail::to($user->email)->send(new WelcomeEmail($user));
return redirect()->route('welcome');
}
}
In this example, we're using the `Mail` facade to send a `WelcomeEmail` instance to the user's email address.
**Using the `Illuminate\Support\Facades\Mail` class:**
1. First, you need to create a mail class that extends `Illuminate\Mail\Mailable`. For example, let's create a `WelcomeEmail` class:
```php
// app/Mail/WelcomeEmail.php
namespace App\Mail;
use Illuminate\Mail\Mailable;
class WelcomeEmail extends Mailable
{
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function build()
{
return $this->markdown('emails.welcome');
}
}
- Next, you need to create an email template in the
resources/views/emails
directory. For example, let's create awelcome.blade.php
file:<!-- resources/views/emails/welcome.blade.php -->
Hello {{ $user->name }},
Welcome to our application!
Best regards, [Your Name]
3. Now, you can use the `Illuminate\Support\Facades\Mail` class to send the email:
```php
// app/Http/Controllers/WelcomeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class WelcomeController extends Controller
{
public function sendWelcomeEmail(Request $request)
{
$user = User::find(1); // Replace with your user model and ID
$mail = new WelcomeEmail($user);
Mail::send($mail);
return redirect()->route('welcome');
}
}
In this example, we're creating a new instance of the WelcomeEmail
class and passing it to the Mail::send
method.
Configuring the mail settings:
Before you can send mail, you need to configure the mail settings in your Laravel application. You can do this by publishing the config/mail.php
file:
php artisan vendor:publish --provider="Illuminate\Mail\MailServiceProvider" --tag="mail-config"
This will create a config/mail.php
file in your application's configuration directory. You can then configure the mail settings in this file, such as the mail driver, host, and port.
For example, you can set the mail driver to smtp
and configure the SMTP settings:
'mail' => [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'Your Name'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
],
Make sure to set the MAIL_USERNAME
and MAIL_PASSWORD
environment variables to your SMTP credentials.
That's it! With these steps, you should be able to send mail using Laravel 6.