CodeIgniter: Trouble using Mailgun library for email sending

I’m having issues with sending emails in CodeIgniter using the Mailgun library installed via Composer. When I try to use it, I get this error:

Fatal error: Sendmail cannot use Mailgun\Mailgun - it is not a trait in C:\xampp\htdocs\ngetest\application\controllers\Sendmail.php on line 4

I’ve followed the Mailgun docs and watched some tutorials, but I’m still stuck. Here’s my Sendmail controller:

<?php
class EmailSender extends CI_Controller
{
    use MailService\MailProvider;

    public function __construct()
    {   
        parent::__construct();
    }

    public function sendMessage()
    {
        try {
            $mailClient = new MailService('api_key_here');
            $mailDomain = 'your_domain.mail.com';
            
            $result = $mailClient->dispatch($mailDomain, [
                'from'    => 'Test User <test@your_domain.com>',
                'to'      => 'Recipient <[email protected]>',
                'subject' => 'Test Email',
                'text'    => 'This is a test email sent via MailService!'
            ]);
            
            return $result;
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

Can someone help me figure out what I’m doing wrong? I’m new to using email libraries in CodeIgniter.

hey finn i had similar issues. seems like u re using a trait that doesnt exist. check your Mailgun library import and ensure you instantiate the correct class name- Mailgun\Mailgun, not MailService\MailProvider. hope it helps!

Having worked with CodeIgniter and various email libraries, I can offer some insights on your issue. It appears the problem isn’t with Mailgun itself but with integrating it into CodeIgniter by trying to use traits, which CodeIgniter does not actually support. Instead of using a trait, you should load the Mailgun client within your controller and call its methods directly. Make sure that you have installed Mailgun properly via Composer and that you are including the autoloader. For example, your sendEmail method could look like this:

public function sendEmail()
{
    $mg = Mailgun::create('your-api-key');
    $result = $mg->messages()->send('your-domain.com', [
        'from'    => '[email protected]',
        'to'      => '[email protected]',
        'subject' => 'Test Email',
        'text'    => 'This is a test!'
    ]);

    if ($result) {
        echo 'Email sent successfully';
    } else {
        echo 'Failed to send email';
    }
}

This approach should resolve the error you’re encountering.

I’ve encountered this issue before. The problem lies in how you’re attempting to use the Mailgun library. CodeIgniter doesn’t natively support traits, which is what the error message is hinting at. Instead, you should instantiate the Mailgun client directly in your controller method. Try modifying your code like this:

public function sendMessage()
{
    $mg = Mailgun::create('your-api-key');
    $domain = 'your-domain.com';

    try {
        $mg->messages()->send($domain, [
            'from'    => 'Sender <[email protected]>',
            'to'      => 'Recipient <[email protected]>',
            'subject' => 'Test Email',
            'text'    => 'This is a test email sent via Mailgun!'
        ]);
        echo 'Email sent successfully';
    } catch (Exception $e) {
        echo 'Error: ' . $e->getMessage();
    }
}

Make sure you’ve properly installed the Mailgun SDK via Composer and included the autoloader in your CodeIgniter application. This approach should resolve your issue.