Integrating Mailgun service with CodeIgniter framework

I’m having trouble integrating Mailgun email service into my CodeIgniter project for sending account verification emails. Everything works fine on my local development environment (XAMPP on Windows), but when I upload to my production server, I get errors.

The error I’m seeing is:

Fatal error: syntax error, unexpected ‘[’ in /home/public_html/project/mailgun/vendor/guzzlehttp/psr7/src/functions.php on line 78

I thought maybe the issue was with cross-platform compatibility, so I reinstalled all the Mailgun dependencies directly on the Linux server using composer, but the problem persists.

My local setup runs PHP 5.6.25 while the production server runs PHP 5.3.3 on CentOS. I followed the official Mailgun installation guide for setting up dependencies.

Here’s my controller code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require '/home/public_html/myproject/mailgun/vendor/autoload.php';
use Mailgun\Mailgun;

class Signup extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Member_model');
        $this->load->helper('cookie');
    }
    
    public function process($token = '')
    {
        if (($this->session->userdata('memberLogin') == TRUE))  
        { 
            redirect(site_url('members/home'));  
        }

        if (isset($_POST['signup'])){
            $formData['postdata'] = $_POST;

            if(!empty($_POST['name']) && !empty($_POST['surname']) && 
            !empty($_POST['email']) && !empty($_POST['pwd']) && 
            !empty($_POST['company']) && !empty($_POST['location'])){ 

                $emailExists = $this->Member_model->verifyEmail();

                if($emailExists==true){
                    $this->session->set_flashdata('error', '<p>This email address is already in use!</p>');
                }else{
                    $handle = str_replace(' ', '',$_POST['name'].$_POST['surname']);
                    $cleanHandle = preg_replace('/[^A-Za-z0-9\-]/', '', $handle);

                    $handleExists = $this->Member_model->verifyHandle($cleanHandle);

                    if($handleExists==true){
                        $finalHandle = $this->generateHandle($cleanHandle);
                    }else{
                        $finalHandle = $cleanHandle;
                    }

                    $formData['activationCode'] = rand(10000,999999); 
                    $formData['memberName'] = $_POST['name'];

                    $this->Member_model->createMember($finalHandle,$formData['activationCode']);
                    mkdir("/home/public_html/members/".$finalHandle);

                    $emailSubject = "Account Activation Required";
                    $emailContent = $this->load->view('emails/activation.php',$formData,TRUE);

                    # Initialize mailgun client
                    $mailgunClient = new Mailgun('my-secret-api-key');
                    $mailgunDomain = "my-domain.com";

                    # Send activation email
                    $response = $mailgunClient->sendMessage($mailgunDomain, array(
                    'from'    => 'Support <[email protected]>',
                    'to'      => 'Member <[email protected]>',
                    'subject' => 'Welcome to our platform',
                    'text'    => 'Account activation email content'
                    ));

                    $this->session->set_flashdata('success','Registration successful! Check your email.');
                    redirect(site_url('signup/success'));    
                }
            }else{
                $this->session->set_flashdata('error', '<p>Please fill all required fields!</p>');
            }
        }   
        
        $formData['pageTitle']='Member Registration';
        $formData['token']=$token;
        $this->load->view('signup/form',$formData);
    }
    
    function generateHandle($baseString) {
        $finalHandle = $baseString;
        for ($counter = 1; $counter < 50; $counter++) {
             $handleCheck = $this->Member_model->verifyHandle($baseString.$counter);
             if(empty($handleCheck)){
                $finalHandle = $baseString.$counter;
                break;
             }
        }
        return $finalHandle;
    }
}
?>

Any suggestions on what might be causing this issue would be really helpful. Thanks!

it’s a php version issue. ur on 5.3.3 but guzzle uses short array syntax [] which wasn’t added until later. mailgun needs php 5.5+ min. either upgrade ur server’s php version or downgrade to an older mailgun sdk that works with 5.3.

Had this exact issue a few months ago with an old project. You’re right - it’s the PHP version mismatch. PHP 5.3 doesn’t support short array syntax that newer libraries use everywhere. If you can’t upgrade PHP right away, try building a simple CURL solution to hit Mailgun’s REST API directly. Skip the SDK completely. It’s more work but you get full control and avoid all the dependency headaches. I did this as a temp fix while waiting for server upgrades and it worked great. Mailgun’s API docs have solid examples for raw HTTP requests - easy to turn into a CodeIgniter library.

That syntax error is happening because you’re running PHP 5.3.3 on your production server. Guzzle HTTP (which Mailgun uses) needs the short array syntax that wasn’t added until later PHP versions. Your best bet is upgrading to at least PHP 5.5, but shoot for 5.6+ since that’s what you’re using locally. Can’t upgrade? Try downgrading to Mailgun PHP library version 1.7.2 - it’ll work with older PHP but you’ll miss out on some newer features.