Laravel Setup

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.

1Setting up Your Laravel Environment

Prepare your Laravel environment. This tutorial starts with a fresh install, but the SDK works the same in existing applications.

  1. Install Laravel:
Shell
composer create-project laravel/laravel bento-example
  1. Install dependencies:
Shell
composer require bentonow/bento-laravel-sdk
  1. Set up Bento:
Shell
php artisan bento:install

The 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.

2Configuring Your Environment

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.

  1. If you skipped the install prompts, open your .env file and add these lines:
Shell
BENTO_SITE_UUID=your_site_uuid
BENTO_PUBLISHABLE_KEY=your_publishable_key
BENTO_SECRET_KEY=your_secret_key
  1. To send Laravel mail through Bento, add these lines to your .env file too:
Shell
MAIL_MAILER=bento
MAIL_FROM_ADDRESS="author@example.com"
The SDK registers the 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.
  1. Send a test email to confirm the mailer works:
Shell
php artisan bento:test
The Bento mail transport works just like any other SMTP mail configuration you might be used to. We recommend using queues and something like supervisor for the best user experience.
3Creating and Using a Mailable

Create a simple mailable that uses a view and sends it as a Bento transactional email.

There are many advantages to implementing this use case through a Bento flow, but this example keeps it simple.
  1. Create a new mailable:
Shell
php artisan make:mail WelcomeMail
  1. Edit app/Mail/WelcomeMail.php and make a few additions:
PHP
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');
  }
}
  1. Create a view for the email at resources/views/emails/welcome.blade.php:
HTML
<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>
  1. Use the mailable. For example, edit App\Http\Controllers\Auth\RegisteredUserController:
PHP
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));
This example queues the email so users don't have to wait for it to send when they create an account. It does require a queue worker to be running.
4Adding the User to Bento

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.

  1. Jump back into RegisteredUserController:
PHP
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)
));
For mature applications with many pre-existing users, take a look at our Laravel Importer to help you get started quickly.
5Adding a Bento Tag to a User

Tags are great ways to segment users. In many email platforms they would be considered lists.

  1. Tag the new user based on their source (website):
PHP
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));
6Using Bento fields

Tags are great for segmentation, but sometimes you need to store user data to personalize emails. Fields are perfect for this.

  1. Record a user's favorite color:
PHP
// 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.

The 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!