Integrating Mailgun service with CodeIgniter framework

I need help with implementing Mailgun email service in my CodeIgniter application for sending account verification emails. Everything works fine on my local development environment using XAMPP, but when I upload to my production server, I’m getting 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 the vendor files being different between operating systems, so I reinstalled all the Mailgun dependencies directly on the server using composer, but the problem persists.

My local setup runs PHP 5.6.25 on Windows with XAMPP, while the production server uses PHP 5.3.3 on Linux CentOS. Could this PHP version difference be causing the issue?

Here’s my controller code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require '/home/public_html/myapp/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 index($parameter = '')
    {
        if (($this->session->userdata('memberLogin') == TRUE))  
        { 
            redirect(site_url('members/home'));  
        }

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

            if(!empty($_POST['fname']) && !empty($_POST['lname']) && 
            !empty($_POST['email']) && !empty($_POST['pwd']) && 
            !empty($_POST['company']) && !empty($_POST['location']) && 
            !empty($_POST['street'])){ 

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

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

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

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

                    $formData['token'] = rand(10000,999999);
                    $formData['name'] = $_POST['fname'];

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

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

                    # Initialize Mailgun client
                    $mailgunClient = new Mailgun('my_api_key_here');
                    $mailgunDomain = "my_domain_here";

                    # Send email via Mailgun API
                    $response = $mailgunClient->sendMessage($mailgunDomain, array(
                        'from'    => 'Support <[email protected]>',
                        'to'      => 'Member <[email protected]>',
                        'subject' => 'Welcome to our platform',
                        'text'    => 'Please verify your account'
                    ));

                    $this->session->set_flashdata('success','Registration successful!');
                    redirect(site_url('signup/success'));
                }
            }else{
                $this->session->set_flashdata('error', '<p>Please fill all required fields!</p>');
            }
        }
        
        $formData['title']='';
        $formData['parameter']=$parameter;
        $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 ideas what might be causing this syntax error? I’m really stuck on this one.

I’ve faced this issue before on older servers as well. The syntax error arises because PHP 5.3 does not recognize short array syntax, which was introduced in PHP 5.4. Many libraries, including Guzzle and Mailgun’s SDK, require at least PHP 5.4 to function correctly. If upgrading PHP on your production server isn’t an option, consider downgrading to Mailgun’s SDK version 1.7.2, as it’s the last version compatible with PHP 5.3. Alternatively, you could use CodeIgniter’s built-in email functionality by configuring SMTP to work with Mailgun’s servers, avoiding potential dependency conflicts while still benefiting from Mailgun’s email services.

Hit this exact issue two years back on a legacy project. Your PHP 5.3.3 version is the culprit - it’s too old for the short array syntax that newer dependencies need. Upgrading to PHP 5.6+ would fix it, but I get that’s not always doable with shared hosting or old servers. Quick fixes: grab an older Mailgun SDK version that works with PHP 5.3, or switch to a different email service that still supports older PHP. I ended up having to push my client to upgrade their hosting because keeping up with ancient PHP versions gets brutal when libraries start dropping support.

yep, that’s a php version issue. php 5.3 can’t use short array syntax like . guzzle needs at least 5.4. you should upgrade your server’s php version or downgrade to an older version of Mailgun that works with 5.3.