Using the Mailshake API to Get Recipients (with PHP examples)

by Endgrate Team 2024-07-27 5 min read

Mailshake homepage

Introduction to Mailshake API

Mailshake is a powerful email outreach platform designed to help businesses streamline their communication efforts. It offers a suite of tools for creating and managing email campaigns, tracking engagement, and generating leads. With its user-friendly interface and robust features, Mailshake is a popular choice for businesses looking to enhance their email marketing strategies.

Integrating with the Mailshake API allows developers to automate and optimize various aspects of email campaigns. For example, you can use the API to retrieve a list of recipients for a specific campaign, enabling you to analyze engagement metrics or update recipient information programmatically. This capability is particularly useful for businesses that need to manage large volumes of email interactions efficiently.

Setting Up Your Mailshake API Test Account

Before you can start using the Mailshake API to manage recipients, you'll need to set up a test account. This will allow you to safely experiment with API calls without affecting your live data.

Create a Mailshake Account

If you don't already have a Mailshake account, you can sign up for a free trial on the Mailshake website. This will give you access to the platform's features and allow you to generate an API key for testing purposes.

  • Visit the Mailshake website and click on "Sign Up".
  • Follow the on-screen instructions to create your account.
  • Once your account is set up, log in to access the dashboard.

Generate Your Mailshake API Key

To authenticate your API requests, you'll need an API key. Follow these steps to generate one:

  • Navigate to the "Extensions" section in the Mailshake dashboard.
  • Select "API" from the menu.
  • Click on "Create API Key" and follow the prompts to generate your key.
  • Copy the API key and store it securely, as you'll need it for making API requests.

Configure API Key Authentication

Mailshake uses API key-based authentication for accessing its API. You can include your API key in requests in one of the following ways:

  • As a query string parameter: ?apiKey=your_api_key
  • In the request body as JSON: {"apiKey": "your_api_key"}
  • Via an HTTP Authorization header: Authorization: Basic [base-64 encoded version of your api key]

Ensure you replace your_api_key with the actual key you generated.

Testing Your API Connection

To verify that your API key is working correctly, you can test the connection using the following PHP code:


$apiKey = 'your_api_key';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.mailshake.com/2017-04-01/me');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Basic ' . base64_encode($apiKey . ':')
]);

$response = curl_exec($ch);
curl_close($ch);

echo $response;

This code snippet makes a request to the Mailshake API to retrieve information about the authenticated user. Replace your_api_key with your actual API key. If successful, the response will contain user details.

Mailshake authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve Mailshake Recipients Using PHP

To interact with the Mailshake API and retrieve recipients for a specific campaign, you'll need to make HTTP requests using PHP. This section will guide you through the process, including setting up your environment and executing the necessary API calls.

Setting Up Your PHP Environment for Mailshake API Integration

Before making API calls, ensure your PHP environment is properly configured. You'll need:

  • PHP 7.4 or later
  • cURL extension enabled

Verify your PHP version and cURL extension by running the following commands:


php -v
php -m | grep curl

Installing Required PHP Dependencies for Mailshake API

Ensure that the cURL extension is enabled in your php.ini file. This extension is crucial for making HTTP requests to the Mailshake API.

Example Code to Retrieve Mailshake Recipients

Below is a PHP code snippet to retrieve recipients from a specific Mailshake campaign:


$apiKey = 'your_api_key';
$campaignID = 1; // Replace with your actual campaign ID

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.mailshake.com/2017-04-01/recipients/list');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['campaignID' => $campaignID]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Basic ' . base64_encode($apiKey . ':'),
    'Content-Type: application/x-www-form-urlencoded'
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

if (isset($data['results'])) {
    foreach ($data['results'] as $recipient) {
        echo 'Email: ' . $recipient['emailAddress'] . '
'; } } else { echo 'Error retrieving recipients.'; }

Replace your_api_key with your actual API key and $campaignID with the ID of the campaign you wish to query. This script sends a POST request to the Mailshake API to list recipients, then parses and displays their email addresses.

Handling API Response and Errors

After executing the API call, it's important to handle potential errors. The Mailshake API may return various error codes, such as:

  • invalid_api_key: The provided API key is missing or invalid.
  • not_found: The specified campaign or recipient could not be found.
  • limit_reached: The request exceeds the allowed quota.

For more information on error handling, refer to the Mailshake API documentation.

Verifying Successful API Requests in Mailshake Dashboard

To confirm that your API requests are successful, check the Mailshake dashboard. The recipients retrieved should match those listed in your campaign. If discrepancies occur, review your API call parameters and error messages for troubleshooting.

Conclusion and Best Practices for Using Mailshake API with PHP

Integrating the Mailshake API into your PHP applications can significantly enhance your email marketing capabilities by automating recipient management and campaign interactions. By following the steps outlined in this guide, you can efficiently retrieve and manage recipients for your Mailshake campaigns.

Best Practices for Secure and Efficient Mailshake API Integration

  • Secure API Key Storage: Always store your API keys securely, using environment variables or secure vaults, to prevent unauthorized access.
  • Handle Rate Limits: Be mindful of Mailshake's rate limits. Implement logic to handle the limit_reached error by retrying requests after the specified wait time.
  • Data Standardization: Ensure that recipient data is standardized before making API calls to avoid errors related to invalid data formats.

Leveraging Endgrate for Streamlined Integration Management

While integrating with the Mailshake API offers powerful capabilities, managing multiple integrations can become complex. Endgrate simplifies this process by providing a unified API endpoint for various platforms, including Mailshake. By using Endgrate, you can:

  • Save time and resources by outsourcing integration management.
  • Build once for each use case, reducing the need for multiple integration efforts.
  • Offer an intuitive integration experience for your customers.

Explore how Endgrate can enhance your integration strategy by visiting Endgrate.

By adhering to these best practices and leveraging tools like Endgrate, you can ensure a robust and efficient integration with the Mailshake API, ultimately enhancing your email marketing efforts.

Read More

Ready to get started?

Book a demo now

Book Demo