Mailgun PHP syntax error with closing parenthesis

I’m trying to send emails using the Mailgun PHP library but I keep getting a parse error. The error message shows:

PHP Parse error: syntax error, unexpected ‘)’ in /path/to/my/script.php on line 42

Here’s my code:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

require 'vendor/autoload.php';
require 'htmltotext.php';
use Mailgun\Mailgun;

$mailgunClient = new Mailgun('[API_KEY]');
$myDomain = "[MY_DOMAIN]";
$htmlContent = $_POST["content"];
$plainText = html_to_text_converter($htmlContent);
$currentDate = date('D, M jS ');

$response = $mailgunClient->sendMessage($myDomain, array(
    'from'    => '[SENDER_EMAIL]',
    'to'      => '[RECIPIENT_EMAIL]',
    'bcc'     => '[BCC_EMAIL]',
    'subject' => 'Daily notification for ' . $currentDate,
    'text'    => 'Hello, ' . "\n" . $plainText . "\n" . 'Additional content',
    'html'    => '<html><body>Hello, ' . "\n" . $htmlContent . "\n" . 'Additional content.</body></html>'
), );

echo '<h2>Message delivered successfully</h2>';
?>

The error points to the line where the array ends. What could be causing this issue?

yeah, that’s def the trailing comma issue everyone mentioned. also, check your error handling - mailgun throws exceptions that ur code isn’t catching.

That trailing comma after your array is causing the syntax error. Just remove the comma before the closing parentheses and you’re set.

Honestly though, PHP mail libraries and their weird syntax are a pain. I’ve burned way too many hours debugging stuff like this with Mailgun, SendGrid, and other services.

Now I just route everything through Latenode. You can build email workflows without any PHP code - just connect your Mailgun account, create the template, and trigger it however you need.

No syntax errors, no broken code when libraries update, and you can add retry logic or backup providers without touching PHP. Much cleaner than handling all this code manually.

Check it out: https://latenode.com

That parse error is from the trailing comma after your array’s closing bracket. You’ve got ), ); in your sendMessage call when it should be ));. PHP doesn’t like commas after the last argument in function calls. I run into this all the time when copying code or when formatting tools add extra commas. Just remove that comma and your Mailgun script will work fine. Super common syntax error when working with APIs that use arrays.

That trailing comma after your closing bracket is the problem. Remove it and PHP will parse fine.

But debugging Mailgun PHP syntax errors gets old quick. I’ve wasted too much time fixing broken mail scripts every time API versions change or someone drops in a typo.

I switched to Latenode for all my email workflows. Connect your Mailgun account and build the whole flow visually. No PHP syntax errors, no version headaches, and you get error handling plus retries without writing code.

Need to modify templates or add conditions later? Just update the workflow instead of messing with PHP files. Way less painful than maintaining mail scripts.

Yeah, that trailing comma in your sendMessage function is breaking things. But there’s more - your Mailgun setup might bite you too depending on your SDK version. Newer versions need both the API key and base URL: new Mailgun('your-api-key', new HttpAdapter(), 'https://api.mailgun.net') or whatever matches your version. Also, you’re feeding POST data straight into html_to_text_converter without any validation. I’ve seen scripts fail silently when that function gets weird input formats, so add some error checking there.

You’ve got a trailing comma after the closing parenthesis on line 42. Your sendMessage call shows ), ); but it should be )); - PHP doesn’t let you put a comma after the final argument. I ran into this exact problem when upgrading from an older PHP version where my IDE missed these trailing comma errors. Just remove that comma and your script will run fine.

same issue here - you’ve got an extra comma right after the closing bracket in your sendMessage array. Delete it and the parse error should disappear. good luck!