I’m trying to send emails using the Mailgun PHP library installed through Composer in my CodeIgniter project. However, I keep getting this error message:
Fatal error: EmailSender cannot use Mailgun\Mailgun - it is not a trait in C:\xampp\htdocs\myproject\application\controllers\EmailSender.php on line 4
A PHP Error was encountered
Severity: Error
Message: EmailSender cannot use Mailgun\Mailgun - it is not a trait
Filename: controllers/EmailSender.php
Line Number: 4
I followed the official documentation and several tutorials but still can’t figure out what’s wrong. Here’s my controller setup:
EmailSender Controller Code
<?php
class EmailSender extends CI_Controller
{
use Mailgun\Mailgun;
public function __construct()
{
parent::__construct();
// require FCPATH . './vendor/autoload.php';
}
public function sendEmail()
{
try {
$mailgunClient = new Mailgun('2yyyy');
$emailDomain = "yyy.mailgun.org";
$response = $mailgunClient->sendMessage($emailDomain, array(
'from' => 'Test User <test@YOUR_DOMAIN.COM>',
'to' => 'User <USER@YOUR_DOMAIN.COM>',
'subject' => 'Test Email',
'text' => 'This is a test message using Mailgun!'
));
return $response;
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}
What am I doing wrong here? Any help would be appreciated.
You’re mixing up PHP’s use keyword - it has different meanings for traits vs namespaces. Drop the use Mailgun\Mailgun; line entirely since you’re already creating the class with new Mailgun(). But there’s another problem. Your autoloader might not be loading properly - uncomment that require statement in your constructor. I’ve seen CodeIgniter load the autoloader too late, which breaks namespace resolution. Also, newer Mailgun SDK versions changed their setup requirements. Double-check you’re using the right API key format and see if your version needs extra config steps. The SDK docs will show you exactly what constructor parameters you need.
Your use statement is the problem. You’re treating Mailgun\Mailgun like a trait, but it’s actually a class. The use keyword there is for traits, not importing classes.
Just delete the use Mailgun\Mailgun; line entirely. You don’t need it since you’re already doing new Mailgun('2yyyy').
Also uncomment your autoloader:
<?php
class EmailSender extends CI_Controller
{
public function __construct()
{
parent::__construct();
require FCPATH . './vendor/autoload.php';
}
public function sendEmail()
{
// your existing code
}
}
Honestly though, email delivery and API configs get messy fast with CodeIgniter. Been there.
I skip all this complexity now and use Latenode for email workflows. You can set up Mailgun integration visually, test without code, and trigger it from CodeIgniter with a simple HTTP request.
No composer conflicts, no trait errors, and you can change email logic without touching your codebase. Much cleaner.
the error’s happening because you’re mixing trait syntax with class instantiation. drop that use Mailgun\Mailgun; line completely - that’s only for traits. your new Mailgun('2yyyy') is the right way to create the object. also double-check that your autoloader’s uncommented and working, otherwise Mailgun won’t be found.
You’re mixing up PHP’s use statement syntax. Delete that use Mailgun\Mailgun; line completely - that’s trait syntax, not for importing classes. With Composer packages in CodeIgniter, you don’t usually need use statements unless you want shorter class names. Your new Mailgun('2yyyy') approach is right. Just make sure your vendor autoloader loads properly. I ran into the same thing when the autoloader wasn’t loading early enough in CodeIgniter’s lifecycle. Move the require statement to your main index.php or config files instead of the constructor. Also check that your Mailgun SDK version matches whatever docs you’re using - the API changed between versions.