---
description: https://morioh.com/p/5ab56958c1d9
---

# Laravel 7 Tutorial for Beginners - Laravel 7 Send Email Example (ok)

Một ví dụ đã hoàn thành :) thậm chí chưa động vào cấu hình xampp :)

C:\xampp\htdocs\blog

.env

```
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:nlTM+z4nZJJaAahLWWkyV3Xkh7T4htZM2kZl3ZCZPY8=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=phamngoctuong1805@gmail.com
MAIL_PASSWORD=ztkrbjoysfeqnidv
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=phamngoctuong1805@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

```

C:\xampp\htdocs\blog\routes\web.php

```
<?php
use Illuminate\Support\Facades\Route;
Route::get('send-mail', function () {
  $details = [
    'title' => 'Mail from ItSolutionStuff.com',
    'body'  => 'This is for testing email using smtp',
  ];
  Mail::to('phamngoctuong1805@gmail.com')->send(new App\Mail\MyTestMail($details));
  dd("Email is Sent.");
});
```

C:\xampp\htdocs\blog\app\Mail\MyTestMail.php

```
php artisan make:mail MyTestMail
```

```
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyTestMail extends Mailable {
  use Queueable, SerializesModels;
  public $details;
  public function __construct($details) {
    $this->details = $details;
  }
  public function build() {
    return $this->subject('Lorem ipsum dolor sit amet, consectetur adipisicing elit.')->view('emails.myTestMail');
  }
}
```

C:\xampp\htdocs\blog\resources\views\emails\myTestMail.blade.php

```
php artisan make:view emails.myTestMail
```

```
<!DOCTYPE html>
<html>
<head>
    <title>ItsolutionStuff.com</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
    <p>Thank you</p>
</body>
</html>
```

Và đây là kết quả:

![](https://1957079826-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCBeDUD-PK_RF-YgJRe%2F-MDIQt41Hu0ydtyrkios%2F-MDIT-iXpPPspZZAiBQA%2FIMG_0784.PNG?alt=media\&token=6451d6a5-4688-4050-8fbd-6e405e160e19)

## Laravel 7 Tutorial for Beginners - Laravel 7 Send Email Example

In this tutorial we will cover an Laravel 7 send mail example smtp. I would like to show you send email in Laravel 7. I would like to show you Laravel 7 send email smtp example. In this article, we will implement a Laravel 7 send mail example. Follow bellow step for send mail in laravel 7 example.

Laravel 7 provide mail class to send email. You can use several drivers for sending email in Laravel 7. You can use smtp, Mailgun, Postmark, Amazon SES, and sendmail. you have to configure on env file what driver you want to use.

In this tutorial, i will give you step by step instruction to send email in Laravel 7. you can create blade file design and also with dynamic information for mail layout. so let's see step by step guide and send email to your requirement.

### **Step 1: Make Configuration** <a href="#step-1-make-configuration" id="step-1-make-configuration"></a>

In first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 7 will use those sender details on email. So you can simply add as like following.

**.env**

```
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
```

### **Step 2: Create Mail** <a href="#step-2-create-mail" id="step-2-create-mail"></a>

In this step we will create mail class MyTestMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow command.

```
php artisan make:mail MyTestMail
```

**app/Mail/MyTestMail.php**

```
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;

    public $details;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from ItSolutionStuff.com')
                    ->view('emails.myTestMail');
    }
}
```

### **Step 3: Create Blade View** <a href="#step-3-create-blade-view" id="step-3-create-blade-view"></a>

In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create bellow files on "emails" folder.

**resources/views/emails/myTestMail.blade.php**

```
<!DOCTYPE html>
<html>
<head>
    <title>ItsolutionStuff.com</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>

    <p>Thank you</p>
</body>
</html>
```

### **Step 4: Add Route** <a href="#step-4-add-route" id="step-4-add-route"></a>

Now at last we will create "MyTestMail" for sending our test email. so let's create bellow web route for testing send email.

**routes/web.php**

```
Route::get('send-mail', function () {

    $details = [
        'title' => 'Mail from ItSolutionStuff.com',
        'body' => 'This is for testing email using smtp'
    ];

    \Mail::to('your_receiver_email@gmail.com')->send(new \App\Mail\MyTestMail($details));

    dd("Email is Sent.");
});
```

Now you can run and check example.

It will send you email, let' see.

**Run Project:**

```
php artisan serve
```

**Open Link:**

```
http://localhost:8000/send-mail
```

```
php artisan config:cache
```

I hope it can help you...[laravel](https://morioh.com/topic/laravel)[php](https://morioh.com/topic/php)[webdev](https://morioh.com/topic/webdev)[![Laravel 7 Tutorial for Beginners - Laravel 7 Send Email Example](https://i.morioh.com/2020/03/12/d40004a00f4c.jpg)](https://morioh.com/p/5ab56958c1d9)  41.05 GEEK

**Welcome to Morioh!**

Let's write, share knowledge and earn [GeekCash](https://geekcash.org/).

[Sign up!](https://morioh.com/register)\
