datomatic/laravel-hubspot-email-notification-channel

Laravel Channel to save email and notifications on Hubspot Email

v1.4.1 2024-12-06 14:19 UTC

This package is auto-updated.

Last update: 2024-12-06 18:01:06 UTC


README

Latest Version on Packagist Software License Quality Score Total Downloads

This package makes it easy to log notifications to Hubspot Email Engagement V3 with Laravel >= 8.x

Contents

Installation

You can install the package via composer:

composer require datomatic/laravel-hubspot-email-notification-channel

Setting up the HubspotEmail service

Generate an API Key or a Private App from Hubspot. Important! From November 30th 2022 Hubspot will require you to use only private apps. If you have both API Key and Private App configured, to switch using only Private App just remove HUBSPOT_API_KEY from your .env file.

Configure your Hubspot API on .env

HUBSPOT_API_KEY=XXXXXXXX 
# or
HUBSPOT_ACCESS_TOKEN=XXXXXXXX
HUBSPOT_OWNER_ID=XXX //an Hubspot owner id to save as email creator

To publish the config file to config/newsletter.php run:

php artisan vendor:publish --provider="Datomatic\LaravelHubspotEmailNotificationChannel\HubspotEmailServiceProvider"

This will publish a file hubspot.php in your config directory with the following contents:

// config/hubspot.php

return [
    'api_key' => env('HUBSPOT_API_KEY'),
    'access_token' => env('HUBSPOT_API_KEY'),
    'hubspot_owner_id' => env('HUBSPOT_OWNER_ID',null)
];

Usage

You can now use the channel in your via() method inside the Notification class.

Email notification

Your Notification class must have toMail method. The package accepts: MailMessage lines notifications, MailMessage view notifications and Markdown mail notifications.

Data stored on Hubspot:

  • Hubspot Contact Id => The Notifiable Model must have getHubspotContactId(\Illuminate\Notifications\Notification $notification) function
  • Send at timestamp
  • subject
  • mail text (the html of the email or the toHubspotTextMail method of notification)

Example

Notification example

use Datomatic\LaravelHubspotEmailNotificationChannel\HubspotEmailChannel;
use Illuminate\Notifications\Notification;

class OrderConfirmation extends Notification
{
    ...
    public function via($notifiable)
    {
        return ['mail', HubspotEmailChannel::class]];
    }

    public function toMail($notifiable)
    {
        $message = (new MailMessage)
            ->subject(__('order.order_confirm', ['code' => $this->order->code]));

        return $message->view(
            'emails.order', [
                'title' => __('order.order_confirm', ['code' => $this->order->code]),
                'order' => $this->order
            ]
        );
    }

    //Optional text method
    public function toHubspotTextMail($notifiable):string
    {
        return 'text of message to put on hubspot';
    }
    ...
}

Send text version of html email

An example of use of toHubspotTextMail method is to send the text version of the email.

use Soundasleep\Html2Text;
class OrderConfirmation extends Notification
{
    ...

    public function toHubspotTextMail(mixed $notifiable): string
    {
        return Html2Text::convert($this->toMail($notifiable)->render());
    }
}

Model example

namespace App\Models;

class User extends Authenticatable{
    ...
    public function getHubspotContactId(\Illuminate\Notifications\Notification $notification){
        return $this->hubspot_contact_id;
    }
    ...
}

Dynamic Contact Owner

use Datomatic\LaravelHubspotEmailNotificationChannel\HubspotEmailChannel;
use Illuminate\Notifications\Notification;

class PersonalMessage extends Notification
{
    ...

    public function via($notifiable)
    {
        return ['mail', HubspotEmailChannel::class]];
    }

    public function toMail($notifiable)
    {
        $message = (new MailMessage)
            ->subject(__('messages.personal_subject'))
            ->from($this->employee->email, $this->employee->name)
            ->metadata('hubspot_owner_id', $this->employee->hubspot_owner_id);

        return $message->view(
            'messages.personal', [
                'title' => __('messages.personal_welcome', ['recipient' => $notifiable->name]),
                'employee' => $this->employee
            ]
        );
    }

    ...
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email info@albertoperipolli.com instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.