Copy-paste examples for the ConvertNow Email API in Node.js, Python, PHP, Ruby, and Go. No SDK required.
Set your API key as an environment variable before running any example:
export CONVERTNOW_API_KEY=cn_live_YOUR_KEY
Node.js
import fs from ‘fs’;
async function sendEmail({ to, from, cc, bcc, subject, html, text, replyTo, attachments, tags }) {
const res = await fetch(‘https://api.convertnow.co/v1/email/send’, {
method: ‘POST’,
headers: {
‘Authorization’: `Bearer ${process.env.CONVERTNOW_API_KEY}`,
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify({ to, from, cc, bcc, subject, html, text, reply_to: replyTo, attachments, tags }),
});
if (!res.ok) throw new Error((await res.json()).error);
return res.json();
}
// Basic
await sendEmail({ to: ‘[email protected]’, subject: ‘Hello’, html: ‘<p>Hello!</p>’ });
// With tags
await sendEmail({ to: ‘[email protected]’, subject: ‘Welcome’, html: ‘<p>Welcome!</p>’,
tags: [‘welcome’, ‘onboarding’] });
// With attachments
await sendEmail({
to: ‘[email protected]’, subject: ‘Report’, html: ‘<p>See attached.</p>’,
attachments: [{
filename: ‘report.pdf’,
content: fs.readFileSync(‘./report.pdf’).toString(‘base64’),
content_type: ‘application/pdf’,
}],
});
Python
import os, requests, base64
API_KEY = os.environ[‘CONVERTNOW_API_KEY’]
def send_email(to, subject, html=None, tags=None, attachments=None):
payload = {‘to’: to, ‘subject’: subject}
if html: payload[‘html’] = html
if tags: payload[‘tags’] = tags
if attachments: payload[‘attachments’] = attachments
r = requests.post(‘https://api.convertnow.co/v1/email/send’,
headers={‘Authorization’: f’Bearer {API_KEY}’}, json=payload)
r.raise_for_status()
return r.json()
# Basic
send_email(‘[email protected]’, ‘Hello’, html='<p>Hello!</p>’)
# With attachments
with open(‘report.pdf’, ‘rb’) as f:
encoded = base64.b64encode(f.read()).decode()
send_email(‘[email protected]’, ‘Report’, html='<p>See attached.</p>’,
attachments=[{‘filename’: ‘report.pdf’, ‘content’: encoded, ‘content_type’: ‘application/pdf’}])
PHP
<?php
define(‘CN_API_KEY’, getenv(‘CONVERTNOW_API_KEY’));
function cn_send(array $payload): array {
$ch = curl_init(‘https://api.convertnow.co/v1/email/send’);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [‘Authorization: Bearer ‘ . CN_API_KEY, ‘Content-Type: application/json’],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$body = curl_exec($ch); curl_close($ch);
return json_decode($body, true);
}
cn_send([‘to’ => ‘[email protected]’, ‘subject’ => ‘Hello’, ‘html’ => ‘<p>Hello!</p>’]);
$pdf = base64_encode(file_get_contents(‘/path/to/report.pdf’));
cn_send([‘to’ => ‘[email protected]’, ‘subject’ => ‘Report’, ‘html’ => ‘<p>See attached.</p>’,
‘attachments’ => [[‘filename’ => ‘report.pdf’, ‘content’ => $pdf, ‘content_type’ => ‘application/pdf’]]]);
Ruby
require ‘net/http’; require ‘json’; require ‘base64’
API_KEY = ENV[‘CONVERTNOW_API_KEY’]
def cn_send(payload)
uri = URI(‘https://api.convertnow.co/v1/email/send’)
req = Net::HTTP::Post.new(uri, ‘Authorization’ => “Bearer #{API_KEY}”, ‘Content-Type’ => ‘application/json’)
req.body = payload.to_json
Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| JSON.parse(h.request(req).body) }
end
cn_send(to: ‘[email protected]’, subject: ‘Hello’, html: ‘<p>Hello!</p>’)
pdf = Base64.strict_encode64(File.read(‘/path/to/report.pdf’, encoding: ‘binary’))
cn_send(to: ‘[email protected]’, subject: ‘Report’, html: ‘<p>See attached.</p>’,
attachments: [{ filename: ‘report.pdf’, content: pdf, content_type: ‘application/pdf’ }])
Go
package main
import (
“bytes”; “encoding/base64”; “encoding/json”
“fmt”; “net/http”; “os”
)
var apiKey = os.Getenv(“CONVERTNOW_API_KEY”)
type Attachment struct {
Filename string `json:”filename”`
Content string `json:”content”`
ContentType string `json:”content_type”`
}
type SendPayload struct {
To interface{} `json:”to”`
Subject string `json:”subject”`
HTML string `json:”html,omitempty”`
Tags []string `json:”tags,omitempty”`
Attachments []Attachment `json:”attachments,omitempty”`
}
func cnSend(p SendPayload) error {
body, _ := json.Marshal(p)
req, _ := http.NewRequest(“POST”, “https://api.convertnow.co/v1/email/send”, bytes.NewReader(body))
req.Header.Set(“Authorization”, “Bearer “+apiKey)
req.Header.Set(“Content-Type”, “application/json”)
resp, err := http.DefaultClient.Do(req)
if err != nil { return err }
defer resp.Body.Close()
var result map[string]interface{}
json.NewDecoder(resp.Body).Decode(&result)
if e, ok := result[“error”]; ok { return fmt.Errorf(“%v”, e) }
return nil
}
func main() {
cnSend(SendPayload{To: “[email protected]”, Subject: “Hello”, HTML: “<p>Hello!</p>”})
pdfBytes, _ := os.ReadFile(“/path/to/report.pdf”)
cnSend(SendPayload{
To: “[email protected]”, Subject: “Report”, HTML: “<p>See attached.</p>”,
Attachments: []Attachment{{
Filename: “report.pdf”,
Content: base64.StdEncoding.EncodeToString(pdfBytes),
ContentType: “application/pdf”,
}},
})
}