Simple php function to send an email with Mandrill

Simple php function to send an email with Mandrill

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Simple php function to send an email with Mandrill

Integrating Mailchimp's Mandrill service into a PHP application requires navigating between the raw HTTP requests defined in their API documentation and the convenience of existing client libraries. While the official mandrill-api-php wrapper available on Bitbucket offers a structured approach, it often introduces unnecessary complexity for straightforward use cases. You might find that setting up autoloading, managing class namespaces, or handling specific versioning issues makes the implementation feel heavier than it needs to be.

For many developers, the goal is simply to execute the send method with the necessary parameters without wrestling with dependency managers or legacy library quirks. The Mandrill API documentation clearly defines the payload structure required for the send endpoint, but translating that into a reusable PHP function can vary wildly depending on how you choose to interact with it.

The most direct path is often using cURL to construct the JSON payload manually. This approach bypasses the overhead of external wrappers and gives you precise control over the request lifecycle. You define your API key, set the Content-Type header to application/json, and send a POST request to https://mandrillapp.com/api/1.0/messages/send.json.

Here is a practical implementation that encapsulates this logic into a simple function:

function sendMandrillEmail(string $apiKey, string $to, array $subject, array $body): array {
    $url = 'https://mandrillapp.com/api/1.0/messages/send.json';
    
    // Prepare the payload according to Mandrill's API specification
    $payload = [
        'message' => [
            'to' => [
                0 => [
                    'email' => $to,
                    'type' => 'to',
                ],
            ],
            'subject' => $subject['content'] ?? '',
            'text_part' => $body['text'] ?? '',
            'html_part' => $body['html'] ?? '',
        ],
    ];

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: API ' . $apiKey,
    ]);

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return json_decode($response, true);
}

This function accepts an API key, a recipient email address, and associative arrays for subject and body content. It returns the raw JSON response from Mandrill, allowing you to inspect success or failure flags programmatically. One critical detail here is the Authorization header; Mandrill expects your API key prefixed with API , which is easy to miss but essential for authentication.

When building this function, you must also consider error handling. The API returns a JSON object containing an errors array if something fails. In a production environment, checking the HTTP status code and parsing the returned data is mandatory to prevent silent failures. For instance, a 401 status indicates an invalid API key, while a 500 suggests internal server errors on Mandrill's side.

Using raw cURL also avoids potential pitfalls associated with the official Bitbucket wrapper. That library relies on specific autoloading configurations and may require updating vendor files if you are using Composer. If your project does not already have that dependency, introducing it solely for a simple send function adds significant boilerplate code to your composer.json.

If you are already working within the Laravel ecosystem, this pattern aligns well with how you might structure service classes or facades. You can inject this logic into a dedicated mail service class, keeping your controllers clean and focused on business logic rather than HTTP transport details.

Ultimately, the choice between using a wrapper library and raw cURL depends on your project's scale and existing dependencies. For lightweight scripts or specific modules where minimal overhead is preferred, a custom function like the one above provides a clean, dependency-free solution that adheres strictly to the Mandrill API specifications.

Our network