While integrating Twilio SMS notifications in a Laravel app using:
"laravel-notification-channels/twilio": "^4.1"
I ran into this error:
Undefined property: NotificationChannels\Twilio\TwilioSmsMessage::$contentVariables
Even though my notification code was perfectly valid and only used ->content(), Laravel kept throwing this exception.
This post explains why this happens, how I fixed it, and what to be careful about.
π The Problem
The error occurs when some internal logic (or another package) tries to access:
$contentVariables
on TwilioSmsMessage.
However, in Twilio Notification Channel v4.1, the base class TwilioMessage does not define this property, causing PHP to throw an undefined property exception.
This is especially common if:
- You upgraded/downgraded packages
- You previously experimented with Twilio Content Templates
- A dependency expects
contentVariablesto exist
β The Workaround That Fixed It
I fixed the issue by extending the base TwilioMessage class inside the package to explicitly define contentVariables.
π File edited:
vendor/laravel-notification-channels/twilio/src/TwilioMessage.php
βοΈ Updated TwilioMessage class
<?php
namespace NotificationChannels\Twilio;
abstract class TwilioMessage
{
/**
* The phone number the message should be sent from.
*/
public ?string $from = null;
public ?string $statusCallback = null;
public ?string $statusCallbackMethod = null;
/**
* Create a new message instance.
*/
public function __construct(
public string $content = '',
public string $contentVariables = ''
) {
}
/**
* Create a message object.
*/
public static function create(string $content = ''): self
{
return new static($content);
}
/**
* Set the message content.
*/
public function content(string $content): self
{
$this->content = $content;
return $this;
}
/**
* Set content variables (prevents undefined property errors).
*/
public function contentVariables(string $content): self
{
$this->content = $content;
return $this;
}
/**
* Set the phone number the message should be sent from.
*/
public function from(string $from): self
{
$this->from = $from;
return $this;
}
public function getFrom(): ?string
{
return $this->from;
}
public function statusCallback(string $statusCallback): self
{
$this->statusCallback = $statusCallback;
return $this;
}
public function statusCallbackMethod(string $statusCallbackMethod): self
{
$this->statusCallbackMethod = $statusCallbackMethod;
return $this;
}
}
After this change, the error disappeared completely.
β οΈ Important Warning
Editing vendor files is NOT recommended for production.
Why?
- Composer updates will overwrite the change
- Other environments wonβt have the fix
- It hides the real compatibility issue
β Better Long-Term Alternatives
If you hit this issue, consider:
- Ensuring all Twilio-related packages are on compatible versions
- Avoiding
contentVariableswhen sending SMS - Using Twilio REST API directly if you need Content Templates (WhatsApp)
- Forking the package and maintaining your fix properly
- Opening a GitHub issue / PR on the package repo
π Final Thoughts
If youβre working with:
- Laravel Notifications
- Twilio SMS
laravel-notification-channels/twilio
β¦and hit unexplained property errors, check the base message classes first.
Hope this saves someone hours of debugging π
Happy coding π
Top comments (0)