0

Laravel 8 Send Mail using Mailtrap

 1 month ago
source link: https://www.laravelcode.com/post/laravel-8-send-mail-using-mailtrap
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Laravel 8 Send Mail using Mailtrap

  1355 views

  5 months ago

Laravel

Today, I would relish to show you laravel 8 send mail utilizing mailtrap. We will utilize laravel 8 send email mailtrap. In this article, we will implement a laravel 8 mail mailtrap. This post will give you a simple example of laravel 8 configure mailtrap. So, let's follow a few steps to engender example of laravel 8 mailtrap integration.

Laravel 8 provides mail class to send an email. you can utilize several drivers for sending email in laravel 8. you can utilize mailtrap, SMTP, Mailgun, Postmark, Amazon SES, and Sendmail. you have to configure on the env file what driver you opt to utilize.

In this tutorial, i will give you step-by-step injunctive authorization to send email in laravel utilizing mailtrap. you can engender blade file design and additionally with dynamic information for mail layout. so let's optically discern step by step guide and send email to your requisite.

Step 1: Add Configuration

First you need to create account on mailtrap if you don't have. So click bellow link to create account:

Mailtrap Site

After creating account you will get mail configuration as mail host, mail port, mail username, mail passwor. you can see bellow screen shot:

add details from there bellow:

.env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=528a733..
MAIL_PASSWORD=73c29..
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

Step 2: Create Mail

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\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
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 Laravelcode')
                    ->view('emails.myTestMail');
    }
}

Step 3: Create Blade View

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>Laravelcode</title>
</head>
<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
   
    <p>Thank you</p>
</body>
</html>

Step 4: Add Route

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 Laravelcode',
        '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
http://localhost:8000/send-mail

I hope it can help you.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK