Sending a Basic Email
The simplest way to send an email with Unosend:
curl -X POST https://www.unosend.co/api/v1/emails \
-H "Authorization: Bearer un_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"from": "[email protected]",
"to": ["[email protected]"],
"subject": "Hello from Unosend!",
"html": "<h1>Hello World</h1><p>This is a test email.</p>"
}'
Response
Email Options
CC & BCC
Send copies to additional recipients
Attachments
Include files with your emails
Reply-To
Set a different reply address
curl -X POST https://www.unosend.co/api/v1/emails \
-H "Authorization: Bearer un_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"from": "John Doe <[email protected]>",
"to": ["[email protected]"],
"subject": "Welcome to Our Platform",
"html": "<h1>Welcome!</h1><p>Thanks for joining us.</p>",
"text": "Welcome! Thanks for joining us.",
"cc": ["[email protected]"],
"bcc": ["[email protected]"],
"reply_to": "[email protected]",
"headers": {
"X-Custom-Header": "custom-value"
},
"tags": {
"campaign": "onboarding",
"user_type": "new"
}
}'
You can specify the sender name in different ways:
Sending to Multiple Recipients
Send the same email to multiple recipients:
All recipients will see each other’s addresses. For private sends, use BCC or send individual emails.
Sending Attachments
Attach files to your emails using base64 encoding:
curl -X POST https://www.unosend.co/api/v1/emails \
-H "Authorization: Bearer un_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"from": "[email protected]",
"to": ["[email protected]"],
"subject": "Your Monthly Report",
"html": "<p>Please find your monthly report attached.</p>",
"attachments": [
{
"filename": "report.pdf",
"content": "base64_encoded_content_here",
"content_type": "application/pdf"
},
{
"filename": "data.csv",
"content": "base64_encoded_content_here",
"content_type": "text/csv"
}
]
}'
Node.js Example
import fs from 'fs';
// Read and encode file
const pdfBuffer = fs.readFileSync('report.pdf');
const base64Content = pdfBuffer.toString('base64');
const response = await fetch('https://www.unosend.co/api/v1/emails', {
method: 'POST',
headers: {
'Authorization': 'Bearer un_your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
from: '[email protected]',
to: ['[email protected]'],
subject: 'Your Monthly Report',
html: '<p>Please find your monthly report attached.</p>',
attachments: [
{
filename: 'report.pdf',
content: base64Content,
content_type: 'application/pdf'
}
]
})
});
Supported Content Types
application/pdf
image/png
image/jpeg
text/csv
text/plain
application/zip
application/json
text/html
Rich HTML Emails
Create beautiful HTML emails with styling:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="font-family: Arial, sans-serif; line-height: 1.6; color: #333;">
<div style="max-width: 600px; margin: 0 auto; padding: 20px;">
<h1 style="color: #1a1a1a;">Welcome to Unosend!</h1>
<p>Hi {{name}},</p>
<p>Thanks for signing up. Here's what you can do next:</p>
<ul>
<li>Complete your profile</li>
<li>Explore our features</li>
<li>Send your first email</li>
</ul>
<a href="https://unosend.co/dashboard"
style="display: inline-block; padding: 12px 24px;
background: #000; color: #fff; text-decoration: none;
border-radius: 6px; margin-top: 16px;">
Go to Dashboard
</a>
<p style="margin-top: 32px; font-size: 12px; color: #666;">
© 2024 Your Company. All rights reserved.
</p>
</div>
</body>
</html>
Best Practice: Always include a plain text version (text) for email clients that don’t support HTML.
Error Handling
Common error codes and how to handle them:
| Code | Description | Action |
|---|
validation_error | Invalid request parameters | Check your request body |
domain_not_verified | Sending domain not verified | Verify your domain first |
rate_limit_exceeded | Too many requests | Wait and retry |
insufficient_quota | Monthly limit reached | Upgrade your plan |
{
"error": {
"code": "domain_not_verified",
"message": "The domain 'example.com' is not verified. Please verify your domain before sending."
}
}
Best Practices
Verify your domain
Always verify your sending domain for better deliverability
Include both versions
Include both HTML and plain text versions of your email
Use inline CSS
Use inline CSS for HTML emails (external stylesheets often don’t work)
Keep attachments small
Keep attachment sizes under 10MB for best delivery
Use tags
Use tags to track email campaigns and analyze performance