Sending a few hundred emails per day is easy. Sending millions while maintaining high deliverability? That's where SMTP API best practices become crucial.
1. Implement Proper Authentication
Always authenticate your SMTP connections:
- SPF: Authorize which servers can send email for your domain
- DKIM: Cryptographically sign your emails
- DMARC: Instruct receivers how to handle authentication failures
Quick SPF Setup
v=spf1 include:_spf.google.com include:sendgrid.net ~all2. Respect Rate Limits
Every SMTP provider has rate limits. Void Relay returns clear error codes when you hit rate limits, so you can slow down appropriately.
3. Handle Errors Gracefully
The Void Relay SDK provides detailed error information with status codes:
import { RelayClient } from '@voidvalue/relay';
const relay = new RelayClient({
apiKey: process.env.RELAY_API_KEY!,
smtpKey: process.env.RELAY_SMTP_KEY!,
});
// Handle different error types appropriately
try {
await relay.send({
to: '[email protected]',
subject: 'Hello',
content: '<p>Welcome!</p>',
isHtml: true
});
} catch (error) {
// Check statusCode to determine the error type
if (error.statusCode === 429) {
console.error('Rate limit exceeded - slow down');
} else if (error.statusCode === 401) {
console.error('Invalid API or SMTP key');
} else if (error.statusCode === 400) {
console.error('Invalid email format');
} else {
console.error('Send failed:', error.message);
}
} 4. Implement Proper Queueing
For high-volume sending, use a message queue like BullMQ:
import { Queue, Worker } from 'bullmq';
import { RelayClient } from '@voidvalue/relay';
const relay = new RelayClient({
apiKey: process.env.RELAY_API_KEY!,
smtpKey: process.env.RELAY_SMTP_KEY!,
});
const emailQueue = new Queue('emails', {
connection: redisConnection,
defaultJobOptions: {
attempts: 3,
backoff: { type: 'exponential', delay: 1000 }
}
});
const worker = new Worker('emails', async (job) => {
const { to, subject, content, isHtml } = job.data;
return await relay.send({ to, subject, content, isHtml });
}, {
connection: redisConnection,
concurrency: 5,
limiter: { max: 10, duration: 1000 }
}); 5. Monitor and Alert
Track your email performance using Void Relay's logging:
// Query logs for monitoring
const logs = await relay.listLogs({
page: 1,
limit: 100,
status: 'failed',
});
const delivered = await relay.listLogs({ status: 'delivered' });
const failed = await relay.listLogs({ status: 'failed' });
const deliveryRate = delivered.logs.length /
(delivered.logs.length + failed.logs.length);
if (deliveryRate < 0.95) {
sendAlert('Delivery rate dropped below 95%');
} 6. Validate Email Addresses
The Void Relay SDK includes built-in validation:
// Basic email validation before sending
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email) && email.length <= 254;
}
if (!isValidEmail(email)) {
console.warn('Invalid email:', email);
return;
}
// The SDK also validates emails and returns appropriate errors
try {
await relay.send({ to: 'invalid-email', subject: 'Test', content: 'Test' });
} catch (error) {
// Error: Invalid email address format
} Ready to Scale Your Email?
Void Relay handles rate limiting, retries, and queueing automatically.