Integrating Brevo with Laravel for a PhotoBooth Application
Hai Guys in this blog, i want to share how to integrate Brevo with Laravel for a QyowoBooth app that i make. This guide is intended for those who are already familiar with Laravel and want to send emails after users upload photos and videos. We will store data such as unique IDs, photo paths, video paths, and email addresses .
Demo Application
You can view the PhotoBooth demo on https://qyowobooth.samuelstevenph.com/.
Okay!! Lets Startt
Step 1: Create a New Laravel Project
To get started, make sure you have the latest version of Laravel installed. Create a new project with the following command:
composer create-project --prefer-dist laravel/laravel photobooth-be
Database Configuration
Once the project is created, configure the database in the .env file as follows:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=photobooth-be
DB_USERNAME=root
DB_PASSWORD=
Run the migration to ensure the database connection is working:
php artisan migrate
Step 2: Create Model and Migration
Next, create a model and migration to store image and video data:
php artisan make:model PhotoBoothImage --migration
Migration Schema
Open the migration file located in database/migrations and configure it as follows:
Schema::create('photoBoothImages', function (Blueprint $table) {
$table->id();
$table->string('custom_id')->unique();
$table->string('file_path_photo')->nullable();
$table->string('file_path_video')->nullable();
$table->string('email');
$table->timestamps();
});
Run the migration:
php artisan migrate
Step 3: Install API Support in Laravel
Since we are using Laravel 12, we need to install the API package:
php artisan install:api
Step 4: Create a Controller
Create a new controller using the following command:
php artisan make:controller PhotoBoothImageController --api
Controller Implementation
Open PhotoBoothImageController.php and add the logic to store images and videos:
public function store(Request $request)
{
$validated = $request->validate([
'email' => 'required|email',
'image' => 'required|image|mimes:png,jpg,jpeg|max:2048',
'video' => 'required|file|max:51200',
]);
$customId = 'SL-' . now()->format('Ymd') . '-' . Str::random(8);
$imagePath = $request->file('image')->store('images', 'public');
$videoPath = $request->file('video')->store('videos', 'public');
$image = new PhotoBoothImage();
$image->custom_id = $customId;
$image->file_path_photo = 'storage/' . $imagePath;
$image->file_path_video = 'storage/' . $videoPath;
$image->email = $request->email;
$image->save();
return response()->json(['message' => 'Upload successful!', 'id' => $customId], 200);
}
Step 5: Configure Brevo
Create an account on Brevo and generate an API key.
- Click on your profile at the top right corner.
- Choose SMTP & API.
- Go to API Keys and generate a new key.
Store the API Key in the .env file:
MAIL_MAILER=smtp
MAIL_HOST=smtp-relay.sendinblue.com
MAIL_PORT=587
MAIL_USERNAME=admin@samuelstevenph.com
MAIL_PASSWORD=master-password
BREVO_API_KEY=xkeysib-xxxx
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=admin@samuelstevenph.com
MAIL_FROM_NAME="Qyowo Booth"
Configure Mail Settings in config/mail.php:
Ensure that your mailer configuration is correct:
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp-relay.sendinblue.com'),
'port' => env('MAIL_PORT', 587),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
],
],
Step 6: Create Brevo Service
Create a new file in app/Services/BrevoService.php with the following content:
namespace App\Services;
use Illuminate\Support\Facades\Log;
use SendinBlue\Client\Configuration;
use SendinBlue\Client\Api\TransactionalEmailsApi;
use SendinBlue\Client\Model\SendSmtpEmail;
class BrevoService
{
public function sendEmail($email, $subject, $content)
{
$config = Configuration::getDefaultConfiguration()->setApiKey("api-key", env('BREVO_API_KEY'));
$apiInstance = new TransactionalEmailsApi(null, $config);
$sendSmtpEmail = new SendSmtpEmail([
'subject' => $subject,
'sender' => ['name' => 'Qyowo Booth', 'email' => "admin@samuelstevenph.com"],
'to' => [['email' => $email, 'name' => 'Recipient']],
'htmlContent' => $content,
]);
try {
$result = $apiInstance->sendTransacEmail($sendSmtpEmail);
Log::info('Email sent successfully', ['result' => $result]);
return json_decode(json_encode($result), true);
} catch (\Exception $e) {
Log::error('Failed to send email', ['error' => $e->getMessage()]);
return ['error' => 'Failed to send email: ' . $e->getMessage()];
}
}
}
Step 7: Update Controller for Email Sending
Modify the store method in PhotoBoothImageController to include email sending:
$brevoService = new BrevoService();
$content = "
<div style='font-family: Arial, sans-serif;'>
<h1>🎉 Upload Successful! 🎉</h1>
<p>Your photo and video have been uploaded successfully with ID:</p>
<p><strong>{$customId}</strong></p>
<p>Click the link below to view your upload:</p>
<a href='https://qyowobooth.samuelstevenph.com/{$customId}'>View Your Photo and Video</a>
</div>
";
$response = $brevoService->sendEmail($request->email, 'Upload Successful', $content);
return response()->json(['message' => 'Upload successful!', 'email_response' => $response, 'id' => $customId], 200);
Step 8: Create API Endpoint
Add the following route to routes/api.php:
use App\Http\Controllers\PhotoBoothImageController;
Route::post('/upload', [PhotoBoothImageController::class, 'store']);
Route::get('/images/{id}', [PhotoBoothImageController::class, 'show']);
Conclusion
By following these steps, you have successfully integrated Brevo with Laravel to automatically send emails after users upload their photos and videos. Now your application can notify users instantly via email, enhancing the overall experience!
