This package makes it easy to log notifications to Hubspot Email Engagement V3 with Laravel >= 8.x
You can install the package via composer:
composer require datomatic/laravel-hubspot-email-notification-channel
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)
];
You can now use the channel in your via()
method inside the Notification class.
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)
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';
}
...
}
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());
}
}
namespace App\Models;
class User extends Authenticatable{
...
public function getHubspotContactId(\Illuminate\Notifications\Notification $notification){
return $this->hubspot_contact_id;
}
...
}
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
]
);
}
...
}
Please see CHANGELOG for more information what has changed recently.
$ composer test
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.