Skip to content

Instantly share code, notes, and snippets.

@alizaenazet
Last active October 15, 2024 05:17
Show Gist options
  • Save alizaenazet/ad54e675cce30bf39e6952ef0ba4d4cb to your computer and use it in GitHub Desktop.
Save alizaenazet/ad54e675cce30bf39e6952ef0ba4d4cb to your computer and use it in GitHub Desktop.

Laravel smtp CC24

Prapare the env file

MAIL_MAILER=smtp
MAIL_HOST=mail.doyanbelajar.my.id
MAIL_PORT=587 # ganti ke 465 jika gagal
[email protected]
MAIL_PASSWORD=MYPASSWORD
MAIL_ENCRYPTION=ssl
MAIL_FROM_ADDRESS="[email protected]" 
MAIL_FROM_NAME="Azet laravel"

Make mail file

php artisan make:mail Registration

this command will make a Registration.php new file. find path : app/Mail/Registration.php


Change the functions below in Registration.php

// on top of file
use Illuminate\Mail\Mailables\Address; // Overide import
use Illuminate\Mail\Mailables\Envelope; // Overide import

 public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address(env("MAIL_FROM_ADDRESS"), env("MAIL_USERNAME")),
            subject: 'Regis sukses',
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            view: 'emails.registration-successful', // make sure you have this blade file
        );
    }

Make sure u already make registration-successful.blade.php in views dir.


Make the email api :

make a new route in web.php

// on top of file
use Illuminate\Support\Facades\Route;
use App\Mail\Registration;
use Illuminate\Support\Facades\Mail; 

Route::get('/tes/email', function () {
    Mail::to('[email protected]')->send(new Registration());

    return response()->json(['message' => 'Test email sent successfully']);
});

USE POST METHOD WITH PARAMS


Modift the route web.php :

// Define a POST route for sending an email

Route::post('/kirim-email', function () {
    // Retrieve the 'email' and 'username' from the request
    $email = request('email');
    $username = request('username');

    // Check if 'email' or 'username' is missing in the request
    if (!$email || !$username) {
        // Return a JSON response with an error message and a 400 status code
        return response()->json(['message' => 'Email and username are required'], status: 400);
    }
    
    // Send an email using the Registration Mailable class
    Mail::to($email)->send(new Registration($email, $username));

    // Return a JSON response indicating the email was sent successfully
    return response()->json(['message' => 'email sent successfully']);
})
// Disable the CSRF token verification for this route
->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]);

Modify the Email file Registration.php :

 // make new contractor
 
     public $email;
     public $username;
    public function __construct($email, $username)
    {
        $this->email = $email;
        $this->username = $username;
    }
    
    
    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        // Return a new Content instance with the view and data to be used in the email
        return new Content(
            // Specify the Blade template to be used for the email content
            view: 'emails.registration-successful', // make sure you have this blade file
            // Pass the necessary data to the Blade template
            with: [
                'email' => $this->email, // The email address of the user
                'username' => $this->username, // The username of the user
            ],
        );
    }

Modify blade view file :

<!DOCTYPE html>
<html>
<head>
    <title>Registration Successful</title>
</head>
<body>
    <h1>Registration Successful</h1>
    <p>Thank you, {{ $username }}, for registering with us!</p>
    <p>Your email: {{ $email }}</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment