Basic Usage

Python

```python
import requests

def send_email(api_key, from_name, from_email, to, subject, body):
    response = requests.post(
        'https://app.xn--lemn-sqa.com/api/transactional/send',
        headers={
            'Content-Type': 'application/json',
            'X-Auth-APIKey': api_key
        },
        json={
            "fromname": from_name,
            "fromemail": from_email,
            "to": to,
            "subject": subject,
            "body": body
        }
    )
    return response.json()

# Usage
result = send_email(
    'your_api_key_here',
    'Sender Name',
    'sender@example.com',
    'recipient@example.com',
    'Test Subject',
    '<html><body>This is a test email.</body></html>'
)
print(result)

JavaScript (Node.js)

const axios = require('axios');

async function sendEmail(apiKey, fromName, fromEmail, to, subject, body) {
    try {
        const response = await axios.post('https://app.xn--lemn-sqa.com/api/transactional/send', {
            fromname: fromName,
            fromemail: fromEmail,
            to: to,
            subject: subject,
            body: body
        }, {
            headers: {
                'Content-Type': 'application/json',
                'X-Auth-APIKey': apiKey
            }
        });
        return response.data;
    } catch (error) {
        console.error('Error sending email:', error);
        throw error;
    }
}

// Usage
sendEmail(
    'your_api_key_here',
    'Sender Name',
    'sender@example.com',
    'recipient@example.com',
    'Test Subject',
    '<html><body>This is a test email.</body></html>'
)
.then(result => console.log(result))
.catch(error => console.error(error));

PHP

<?php

function sendEmail($apiKey, $fromName, $fromEmail, $to, $subject, $body) {
    $url = 'https://app.xn--lemn-sqa.com/api/transactional/send';
    $data = array(
        'fromname' => $fromName,
        'fromemail' => $fromEmail,
        'to' => $to,
        'subject' => $subject,
        'body' => $body
    );

    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json\r\nX-Auth-APIKey: $apiKey",
            'method'  => 'POST',
            'content' => json_encode($data)
        )
    );

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result;
}

// Usage
$result = sendEmail(
    'your_api_key_here',
    'Sender Name',
    'sender@example.com',
    'recipient@example.com',
    'Test Subject',
    '<html><body>This is a test email.</body></html>'
);
echo $result;

Ruby

require 'net/http'
require 'uri'
require 'json'

def send_email(api_key, from_name, from_email, to, subject, body)
  uri = URI.parse('https://app.xn--lemn-sqa.com/api/transactional/send')
  request = Net::HTTP::Post.new(uri)
  request['Content-Type'] = 'application/json'
  request['X-Auth-APIKey'] = api_key
  request.body = {
    fromname: from_name,
    fromemail: from_email,
    to: to,
    subject: subject,
    body: body
  }.to_json

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  JSON.parse(response.body)
end

# Usage
result = send_email(
  'your_api_key_here',
  'Sender Name',
  'sender@example.com',
  'recipient@example.com',
  'Test Subject',
  '<html><body>This is a test email.</body></html>'
)
puts result

Java

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class LemonEmailSender {
    public static String sendEmail(String apiKey, String fromName, String fromEmail, String to, String subject, String body) throws Exception {
        URL url = new URL("https://app.xn--lemn-sqa.com/api/transactional/send");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("X-Auth-APIKey", apiKey);
        con.setDoOutput(true);

        String jsonInputString = String.format(
            "{\"fromname\":\"%s\",\"fromemail\":\"%s\",\"to\":\"%s\",\"subject\":\"%s\",\"body\":\"%s\"}",
            fromName, fromEmail, to, subject, body
        );

        try(OutputStream os = con.getOutputStream()) {
            byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
            StringBuilder response = new StringBuilder();
            String responseLine = null;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            return response.toString();
        }
    }

    public static void main(String[] args) {
        try {
            String result = sendEmail(
                "your_api_key_here",
                "Sender Name",
                "sender@example.com",
                "recipient@example.com",
                "Test Subject",
                "<html><body>This is a test email.</body></html>"
            );
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class LemonEmailSender
{
    private static readonly HttpClient client = new HttpClient();

    public static async Task<string> SendEmailAsync(string apiKey, string fromName, string fromEmail, string to, string subject, string body)
    {
        var payload = new
        {
            fromname = fromName,
            fromemail = fromEmail,
            to = to,
            subject = subject,
            body = body
        };

        var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json");

        client.DefaultRequestHeaders.Add("X-Auth-APIKey", apiKey);

        var response = await client.PostAsync("https://app.xn--lemn-sqa.com/api/transactional/send", content);

        return await response.Content.ReadAsStringAsync();
    }

    static async Task Main(string[] args)
    {
        try
        {
            string result = await SendEmailAsync(
                "your_api_key_here",
                "Sender Name",
                "sender@example.com",
                "recipient@example.com",
                "Test Subject",
                "<html><body>This is a test email.</body></html>"
            );
            Console.WriteLine(result);
        }
        catch (Exception e)
        {
            Console.WriteLine($"An error occurred: {e.Message}");
        }
    }
}

Was this article helpful?
© 2024 Lemón SES