Sending emails using the AhaSend HTTP API


At AhaSend, we provide a reliable SMTP service for all your email sending needs. However, for customers seeking enhanced performance, especially when sending large volumes of emails, or for those operating in restricted network environments where SMTP ports (like 25, 587, and 2525) are blocked, our HTTP API offers a powerful and flexible alternative.

By integrating directly with our API, you can bypass traditional SMTP limitations and send emails programmatically from any application or workflow. This gives you the speed required for high-volume transactional and marketing emails and ensures deliverability even when standard ports are unavailable. This article will walk you through how to start sending emails with our API using several popular programming languages and even straight from your command line.

Using the API from the command line

You can use the API to send emails from the command-line using the curl command:

curl https://api.ahasend.com/v1/email/send
    -X POST
    -H 'Content-Type: application/json'
    -H 'X-Api-Key: YOUR-API-KEY'
    -d '{
        "from": {
            "name": "My awesome startup"
            "email": "[email protected]"
        },
        "recipients": [
            {
                "name": "Someone",
                "email": "[email protected]"
            }
        ],
        "content": {
            "subject": "Sample email",
            "text_body": "Plain text body",
            "html_body": "<p>This is the <b>HTML</b> body</p>",
        }
    }'

Using the API with Node.js

To use the API with Node.js, you can use any http client. We'll be using Axios for this example.

To install Axios run the following command in your project directory:

npm i axios
const axios = require('axios');

const email = {
    from: {
        name: 'My awesome startup',
        email: '[email protected]',
    },
    recipients: [
        {
            name: 'Someone',
            email: '[email protected]',
        }
    ],
    content: {
        subject: 'Sample email',
        text_body: 'Plain text body',
        html_body: '<p>This is the <b>HTML</b> body</p>',
    },
};
axios.post('https://api.ahasend.com/v1/email/send', email, {
    headers: {
        'Content-Type': 'application/json',
        'X-Api-Key': 'YOUR-API-KEY',
    }
}).then((response) => {
    console.log(response);
});

Using the API with Ruby

You can use any http client to send the requests from your Ruby application. We'll be using HTTParty for this example.

To install HTTParty run the following command in your project directory:

gem install httparty
require 'httparty'

class AhaSend
    include HTTParty

    base_uri 'https://api.ahasend.com'
    headers 'X-Api-Key' => ENV['AHASEND_API_KEY'], 'Content-Type' => 'application/json'

    def initialize(from_name:, from_email:)
        @from_name = from_name
        @from_email = from_email
    end

    def send_email(subject:, html_body:, recipient_email:, recipient_name: nil, text_body: nil)
        email = {
            from: { name: @from_name, email: @from_email },
            recipients: [{ name: recipient_name, email: recipient_email }],
            content: { subject:, html_body:, text_body: },
        }

        self.class.post('/v1/email/send', body: email)
    end
end

ahasend = AhaSend.new(from_name: 'My awesome startup', from_email: '[email protected]')
ahasend.send_email(
    subject: 'Sample email',
    html_body: '<p>This is the <b>HTML</b> body</p>',
    text_body: 'Plain text body',
    recipient_name: 'Someone',
    recipient_email: '[email protected]'
)

Using the API with PHP

You can use any http client to send the requests from your PHP application. We'll be using PHP's file_get_contents() function for this example.

$email = [
    'from' => [
        'name' => 'My awesome startup',
        'email' => '[email protected]',
    ],
    'recipients' => [
        [
            'name' => 'Someone',
            'email' => '[email protected]',
        ]
    ],
    'content' => [
        'subject' => 'Sample email',
        'text_body' => 'Plain text body',
        'html_body' => '<p>This is the <b>HTML</b> body</p>',
        'attachments' => [
            [
                'data' => base64_encode(file_get_contents('./file.pdf')),
                'base64' => true,
                'content_type' => 'application/pdf',
                'file_name' => 'report.pdf',
            ],
        ],
    ],
];
$options = array(
    'http' => array(
        'header'  => "Content-type: application/json\r\nX-Api-Key: YOUR-API-KEY",
        'method'  => 'POST',
        'content' => json_encode($email)
    )
);
$context  = stream_context_create($options);
$resp = file_get_contents('https://api.ahasend.com/v1/email/send', FALSE, $context);
var_export($resp, TRUE);

Using the API with Python

You can use any http client to send the requests from your Python application. We'll be using the requests library for this example.

To install requests run the following command:

python -m pip install requests
import requests
email = {
    'from': {
        'name': 'My awesome startup',
        'email': '[email protected]',
    },
    'recipients': [
        {
            'name': 'Someone',
            'email': '[email protected]',
        }
    ],
    'content': {
        'subject': 'Sample email',
        'text_body': 'Plain text body',
        'html_body': '<p>This is the <b>HTML</b> body</p>',
    },
}
headers = {
    'X-Api-Key': 'YOUR_API_KEY',
    'Content-Type': 'application/json'
}
r = requests.post('https://api.ahasend.com/v1/email/send', json=email, headers=headers)
print(r.json())
Category
AhaSend
Send up to 1,000 emails per month on us, no credit card required!