Lompat ke konten Lompat ke sidebar Lompat ke footer

Membuat Full Notifikasi Kirim ke Gmail di Laravel 10 Mudah

 

1. Buat app password di https://myaccount.google.com/apppasswords?

    namanya bebas
    akan muncuk password random, save terlebih dulu
2. tambahkan di file .ENV
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=email_kalian@gmail.com
MAIL_PASSWORD=kode_app_password_tadi
MAIL_ENCRYPTION=null
MAIL_FROMADDRESS="email_kalian@gmail.com"
MAIL_FROM_NAME="${APP_NAME}"

3. buka file mail.php
pada bagian ini. tambahkan 2 baris bawah ini, totalnya seperti ini
        'smtp' => [
            'transport' => 'smtp',
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN'),

            'auth_mode' => null,
            'verify_peer' => false,


4. buat controller
php artisan make:controller UserController

<?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('email_tujuan@gmail.com')->send(new UserMail());

        return true;
    }

}

5. bagian web.php, tambahkan baris ini
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. 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> Hai {{$name}} </p>
        <p>Mohon maaf (untuk tolak) Barang anda yg di pesan dengan kode pesanan telah di setujui/tolak
</p>
<p>Jika di tolak silahkan hubungi PRT untuk konfirmasi barang

    </div>
</div>
</body>
</html>

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

8. pada bagian config/mail.php
 'mailers' => [

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


9. tes
http://localhost:8000/user/mail/send

pastikan sudah menjalankan

php artisan serve


close