Image

laravel

Recaptcha

Setup recaptcha.

2 min read
Last update: November 28, 2021

This guide explain how to setup recaptcha with API, not with Blade front.

For NuxtJS backend installation, check NuxtJS reCAPTCHA

Dependencies

composer require biscolab/laravel-recaptcha
.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=noreply@mail.com
MAIL_FROM_NAME="${APP_NAME}"
MAIL_TO_ADDRESS=contact@mail.com
MAIL_TO_NAME="${APP_NAME} contact"

RECAPTCHA_SITE_KEY=
RECAPTCHA_SECRET_KEY=
config/mail.php
<?php

return [
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'noreply@mail.com'),
        'name'    => env('MAIL_FROM_NAME', 'Amazing project'),
    ],
    'to' => [
        'address' => env('MAIL_TO_ADDRESS', 'contact@mail.com'),
        'name'    => env('MAIL_TO_NAME', 'Amazing project'),
    ],
];

Controller

app/Http/Controllers/Api/SubmissionController.php
<?php

namespace App\Http\Controllers\Api;

use Mail;
use App\Models\Submission;
use App\Mail\SubmissionMail;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class SubmissionController extends Controller
{
    public function send(Request $request)
    {
        $validate = $this->validate($request, [
            'name'                  => 'required|string',
            'email'                 => 'required|email',
            'message'               => 'required|string|min:250',
            'g-recaptcha-response'  => 'required|recaptcha',
        ]);
        
        $from_address = config('mail.from.address');

        // Send mail
        Mail::send(new SubmissionMail(submission: $submission));

        return response()->json([
            'success' => 'Your mail was sended!',
            'contact' => [
                'from' => $from_address,
            ],
        ], 200);
    }
}

Route

routes/api.php
Route::post('submission', [SubmissionController::class, 'send'])->name('api.submission.send');

Mailable

Create Mailable with blade template

app/Mail/SubmissionMail.php
<?php

namespace App\Mail;

use App\Models\Submission;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SubmissionMail extends Mailable
{
    use Queueable;
    use SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(
        public Submission $submission,
    ) {
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        /** @var Mailable $mail */
        $mail = $this;

        $subject = "Subject";
        $from_address = config('mail.from.address');
        $from_name = config('mail.from.name');
        $to_address = config('mail.to.address');
        $to_name = config('mail.to.name');

        return $mail->to($to_address, $to_name)
            ->from($from_address, $from_name)
            ->subject($subject)
            ->markdown('emails.submission')
            ->with([
                'name'       => $this->submission->name,
                'email'      => $this->submission->email,
                'message'    => $this->submission->message,
            ]);
    }
}
resources/views/emails/submission.blade.php
@component('mail::message')
  # A new contact

  **Name** : {{ $name }}<br />
  **Email**&nbsp;: <a href="mailto:{{ $email }}">{{ $email }}</a>

  **Message**&nbsp;:<br />
  {{ $message }}

  *Team*
@endcomponent