I’m trying to send emails using the Mailgun API in PHP but keep running into 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 email sending code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
date_default_timezone_set('America/New_York');
require 'vendor/autoload.php';
require 'textconverter.php';
use Mailgun\Mailgun;
$mailgunClient = new Mailgun('[API_KEY]');
$mailDomain = "[YOUR_DOMAIN]";
$htmlContent = $_POST["email_body"];
$plainText = html_to_plain_text($htmlContent);
$currentDate = date('l, F jS Y');
$response = $mailgunClient->sendMessage($mailDomain, array(
'from' => '[SENDER_EMAIL]',
'to' => '[RECIPIENT_EMAIL]',
'bcc' => '[BCC_EMAIL]',
'subject' => 'Daily notification for ' . $currentDate,
'text' => 'Hello, ' . "\n" . $plainText . "\n" . 'Best regards',
'html' => '<html><body>Hello, ' . "\n" . $htmlContent . "\n" . 'Best regards.</body></html>'
), );
echo '<h2>Message delivered successfully</h2>';
?>
The error points to the line where I close the array. What could be causing this unexpected parenthesis error? I’ve been staring at this code for hours and can’t figure out what’s wrong with the syntax.
The problem lies at the end of your sendMessage call. There’s a trailing comma after the closing parenthesis, specifically after the ‘html’ => 'Hello, ’ . “\n” . $htmlContent . “\n” . ‘Best regards.’ line, followed by ), );. This extra comma is causing the parse error. Just remove it so that it reads )); instead of ), );, and you should be fine. It’s a common mistake with PHP arrays, particularly when they span multiple lines.
Yep, that’s the trailing comma issue everyone’s talking about. But double-check your line numbers too - PHP sometimes reports errors a few lines after where they actually happen. I’ve seen this with Mailgun where the real problem was a missing semicolon further up in the code.
That trailing comma after your array is causing the syntax error. You’ve got ), ); but it should be ));
Honestly though, Mailgun’s PHP SDK is a pain. I wasted so much time debugging stuff like this when I was doing direct API integrations.
What totally changed things for me was switching to automation instead. No more wrestling with API syntax or maintaining email code - just set up workflows that handle everything automatically. No parse errors, no debugging PHP arrays, no staring at broken code for hours.
The automation grabs form submissions, processes them, and sends emails without any custom PHP. It logs everything and handles errors properly too. Way cleaner than maintaining direct API stuff.
You can fix that syntax issue, but I’d just automate the whole email process: https://latenode.com
You’ve got an extra comma before the closing parenthesis in your sendMessage function. Look at the line with your HTML content - after the closing single quote, you have ), ); but it should be ));. That trailing comma is causing your parse error.
I hit the same thing when I started using Mailgun’s PHP SDK. The syntax gets weird with multi-line arrays like this. Once you fix the comma, double-check that your API key and domain are set up right.
Also, heads up - those \n line breaks won’t render in HTML emails. You’ll want <br> tags or proper paragraph elements instead. Makes a huge difference across different email clients.