DEV Community

Ayowande Oluwatosin
Ayowande Oluwatosin

Posted on

πŸ›  Fixing `Undefined property: TwilioSmsMessage::$contentVariables` in Laravel (Twilio Notifications)

While integrating Twilio SMS notifications in a Laravel app using:

"laravel-notification-channels/twilio": "^4.1"
Enter fullscreen mode Exit fullscreen mode

I ran into this error:

Undefined property: NotificationChannels\Twilio\TwilioSmsMessage::$contentVariables
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 contentVariables to 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
Enter fullscreen mode Exit fullscreen mode

✏️ 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

  1. Ensuring all Twilio-related packages are on compatible versions
  2. Avoiding contentVariables when sending SMS
  3. Using Twilio REST API directly if you need Content Templates (WhatsApp)
  4. Forking the package and maintaining your fix properly
  5. 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)