Creating and Using Sending Credentials


SMTP Credentials

What is SMTP?

Simple Mail Transfer Protocol (SMTP for short) is a specialized protocol that acts as the backbone of email delivery. SMTP is one the most common way of sending and receiving emails over the internet.

To use this protocol and prevent others from using it to send emails from your domains, you need to set up Credentials to authenticate yourself with our SMTP servers.

How can I create SMTP Credentials?

To create SMTP Credentials, open the AhaSend Dashboard and

  1. Visit the Credentials tab of your account.
  2. Click on the Create Credential button.
  3. Select SMTP as the Type of the credential.
  4. Choose a name for the credential: This name is only used on the dashboard and is there to help you identify this credential later on in other parts of the dashboard. If you have multiple domains in your account, it's a good idea to include the domain name in the credential name, e.g. "Example.com Production", "Example.com Staging", etc.
  5. Click on the Create Credential button to create the new credential and see the username and password.

How can I use SMTP Credentials?

Almost all programming languages have native support or established libraries for working with SMTP. We've prepared a guides for sending emails with SMTP for the languages below, but if you need help with a language not listed here, feel free to contact us at [email protected] and we'll be more than happy to help you start sending with SMTP.

API Keys

What are API Keys?

AhaSend supports sending emails using a HTTP (REST) API. While SMTP can be used for sending any number of emails, the HTTP API has better performance and throughput when it comes to sending a large number of emails. If you're sending more than 5-10 emails per second, consider using the HTTP APIs instead of SMTP to improve your delivery speeds.

API Keys are used to authenticate your HTTP requests with our server.

How can I create API Keys?

To create SMTP Credentials, open the AhaSend Dashboard and

  1. Visit the Credentials tab of your account.
  2. Click on the Create Credential button.
  3. Select API Key as the Type of the credential.
  4. Choose a name for the credential: This name is only used on the dashboard and is there to help you identify this credential later on in other parts of the dashboard. If you have multiple domains in your account, it's a good idea to include the domain name in the credential name, e.g. "Example.com Production", "Example.com Staging", etc.
  5. Click on the Create Credential button to create the new credential and see the API Key.

How can I use API Keys?

AhaSend provides HTTP REST APIs for sending emails. You can use these APIs with any HTTP client with any programming language, or start testing them with tools such as Postman or HTTPie.

Complete API documentation in OpenApi 3.0 format is available here. You can use the specs to automatically generate client SDKs for a number of languages using the OpenAPI Generator

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://send.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!