Laravel Bento Quick Start
This tutorial sets up Bento in a Laravel app using the Bento-laravel-sdk. You'll configure Bento as your mail transport, send a transactional email, and sync new users to Bento with tags and fields.
Prepare your Laravel environment. This tutorial starts with a fresh install, but the SDK works the same in existing applications.
- Install Laravel:
composer create-project laravel/laravel bento-example- Install dependencies:
composer require bentonow/bento-laravel-sdk- Set up Bento:
php artisan bento:installThe install command asks for your keys and can write them to your .env file. If you enable transactional mail, it also sets MAIL_MAILER and MAIL_FROM_ADDRESS, then offers to run php artisan bento:test to send a test email.
Next, configure your app to work with Bento. You can find your Bento API keys and Site_UUID in the Bento account dashboard. Don't share them or commit them to source code.
If you want Bento to handle your transactional email, the SDK includes a transport for your convenience.
- If you skipped the install prompts, open your
.envfile and add these lines:
BENTO_SITE_UUID=your_site_uuid
BENTO_PUBLISHABLE_KEY=your_publishable_key
BENTO_SECRET_KEY=your_secret_key- To send Laravel mail through Bento, add these lines to your
.envfile too:
MAIL_MAILER=bento
MAIL_FROM_ADDRESS="author@example.com"bento mailer for you, so you don't need to edit config/mail.php. MAIL_FROM_ADDRESS must be an author email in your Bento account.- Send a test email to confirm the mailer works:
php artisan bento:testCreate a simple mailable that uses a view and sends it as a Bento transactional email.
- Create a new mailable:
php artisan make:mail WelcomeMail- Edit
app/Mail/WelcomeMail.phpand make a few additions:
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use App\Models\User;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
// Public properties are available in the view as $user
public function __construct(public User $user)
{
}
public function build()
{
return $this->subject('Welcome!')
->view('emails.welcome');
}
}- Create a view for the email at
resources/views/emails/welcome.blade.php:
<h1>Welcome to our Bento Example, {{ $user->name }}!</h1>
<p>We're excited to have you on board. Get started by exploring our features.
Or reviewing our documentation at <a href='https://docs.bentonow.com'>docs.bentonow.com</a>
</p>- Use the mailable. For example, edit
App\Http\Controllers\Auth\RegisteredUserController:
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;
// After creating the user
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
// Send the welcome email
Mail::to($user->email)->queue(new WelcomeMail($user));When a user registers, add them as a subscriber to Bento. That way you can capture their activity and use it to trigger email flows in Bento.
- Jump back into
RegisteredUserController:
use Bentonow\BentoLaravel\BentoConnector;
use Bentonow\BentoLaravel\DataTransferObjects\CreateSubscriberData;
use Bentonow\BentoLaravel\Requests\CreateSubscriber;
// after creating the user:
$bento = new BentoConnector();
$bento->send(new CreateSubscriber(
new CreateSubscriberData(email: $user->email)
));Tags are great ways to segment users. In many email platforms they would be considered lists.
- Tag the new user based on their source (website):
use Bentonow\BentoLaravel\DataTransferObjects\ImportSubscribersData;
use Bentonow\BentoLaravel\Requests\ImportSubscribers;
use Illuminate\Support\Str;
// After we send the email lets tag the user.
$data = collect([
new ImportSubscribersData(
email: $user->email,
firstName: Str::of($user->name)
->before(' ')
->__toString(),
lastName: Str::of($user->name)
->after(' ')
->__toString(),
tags: ["list:website"],
removeTags: null,
fields: null,
),
]);
$bento->send(new ImportSubscribers($data));Tags are great for segmentation, but sometimes you need to store user data to personalize emails. Fields are perfect for this.
- Record a user's favorite color:
// assuming our sign up form included a field for favorite color
// lets update the previous tag example to include our field data.
$data = collect([
new ImportSubscribersData(
email: $user->email,
firstName: Str::of($user->name)
->before(' ')
->__toString(),
lastName: Str::of($user->name)
->after(' ')
->__toString(),
tags: ["list:website"],
removeTags: null,
fields: ["fav_color" => $request->input('favorite_color')],
),
]);
$bento->send(new ImportSubscribers($data));With Bento's API, you can quickly build a complete subscriber profile for better targeting and personalization, often with a single API call.
ImportSubscribers method safely merges new fields into a user's data without triggering any flows or events from the data you send.Next Steps
Your Laravel app is now integrated with Bento. You can send email, sync new users as subscribers, tag them, and record custom data in fields for use in emails with liquid tags.
From here, explore more Bento features and tailor them to your needs. Contact us in Discord if you need help or have suggestions!