Lompat ke konten Lompat ke sidebar Lompat ke footer

Membuat Notifikasi Kirim Email di Laravel 10 MailTrap Sandbox



1. buat akun https://mailtrap.io/home

2. pilih "test server" (jika ingin tes terlebih dulu)

3. pilih "Integrations" ubah ke "Laravel 7.x and 8.x" lalu copy codenya


contoh kode env saya

MAIL_MAILER=smtp MAIL_HOST=sandbox.smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=1db63ab9654c0a MAIL_PASSWORD=27*****6e476c MAIL_ENCRYPTION=tls


masukkan kode ke file env

4. buat baru kontroller

php artisan make:controller UserController

buat isinya seperti ini

<?php

namespace App\Http\Controllers;
use App\Mail\UserMail;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Mail;

use Illuminate\Http\Request;

class UserController extends Controller
{
/**
* Get the authenticated User.
*
* @return \Illuminate\Http\JsonResponse
*/

public function index()
{
Mail::to('lahiru.ariyasinghe69@gmail.com')->send(new UserMail());

return true;
}

}




5. buat di web.php, masukkan kode seperti ini

use App\Http\Controllers\UserController;

Route::get('/user/mail/send', [UserController::class, 'index']);

6. buat folder app/Mail. buat file UserMail.php

<?php

namespace App\Mail;

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

class UserMail extends Mailable
{
    use Queueable, SerializesModels;

    protected $isSuccess, $logo;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('lahiru@gmail.com', 'GenoTech')
        ->subject('Your Registration is Successful!!!')
        ->view('emails.user_email',['name'=>"Kasun", 'logo' => $this->logo]);
    }
}


7. anda bisa masukkan logo img (optional) ke dalam public folder


8. buat user_email.blade.php di dalam folder resources/view/emails

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <style>
        body{
            height: 750px;
            margin: 2px;
            padding: 2px;
            font-family: Helvetica, Arial, sans-serif;
        }

        .button-container{
            margin:40px 0;
        }

        #box{
            width: 850px;
            margin: 0 auto;
            height:100%;
        }

        #header{
            height: 200px;
            width: 100%;
            position: relative;
            display: block;
            border-bottom: 1px solid #504597;
        }

        .button{
            background-color: #d60e0e;
            border: none;
            color: white !important;
            padding: 10px 25px;
            text-align: center;
            text-decoration: none;
            margin:auto;
            font-size: 22px;
            cursor: pointer;
            border-radius: 10px;
        }

        #image {
            width: 150px;
            height: auto;
            margin-top: 16px;
        }

        #rightbar {
            width: 100%;
            height: 560px;
            padding: 0px;
        }

        .text-div{
            font-size: 18px;
            margin-bottom: 3px;
        }

        #footer {
            clear: both;
            height: 40px;
            text-align: center;
            background-color: #2d0f80;
            margin: 0px;
            padding: 0px;
            color: white;
        }

        p, pre
        {
            font-size: 18px;
            line-height: 1.4;
        }

        .heading{
            color: #504597;
            font-size: 24px;
        }

    </style>
</head>
<body>
<div id="box">
    <div id="header">
       <img id="image" src="{{ $logo }}">
    </div>
    <div class="spacing"></div>
    <div id ="rightbar">
        <h1 class="heading"></h1>
        <p >Hi {{$name}} ,</p>
        <p>Thank you for registered your business with Genotechies.</p>
        <p>Please view the inbox for more details or contact Geno Tech admin for further instructions.</p>
   
        <div class="text-div">Thanks,</div>
        <div class="text-div">Geno Team.</div>
    </div>
</div>
</body>
</html>



jika ingin tes tanpa logo ubh bagian img baris jadi seperti ini

 <!-- <img id="image" src="{{ $logo }}"> -->

9. pada bagian config/mail.php

tambahkan

 'mailers' => [

        'stream' => [  
            'ssl' => [
                'allow_self_signed' => true,
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ],


10. tes dengan ketik

http://localhost:8000/user/mail/send

 

pastikan sudah menjalankan

php artisan serve