Emails

Send transactional and marketing emails with automatic PII protection.

Send an Email

Send a single email to one or more recipients.

POST /v1/emails

Request Body

ParameterTypeDescription
fromstringSender email address (required)
tostring | string[]Recipient email address(es) (required)
subjectstringEmail subject line (required)
htmlstringHTML body content
textstringPlain text body content
ccstring | string[]CC recipients
bccstring | string[]BCC recipients
replyTostringReply-to address
templateIdstringTemplate ID to use
variablesobjectVariables for template rendering
scheduledAtstringISO 8601 datetime for scheduled delivery
tagsstring[]Tags for categorization
metadataobjectCustom key-value metadata

Example

send-email.ts
const email = await client.emails.send({
  from: 'hello@yourdomain.com',
  to: 'user@example.com',
  subject: 'Welcome to our platform!',
  html: `
    <h1>Welcome, {{name}}!</h1>
    <p>Thanks for signing up.</p>
  `,
  variables: {
    name: 'John',
  },
  tags: ['welcome', 'onboarding'],
});

Send with Template

Use a pre-defined template instead of providing HTML inline.

send-with-template.ts
const email = await client.emails.send({
  from: 'hello@yourdomain.com',
  to: 'user@example.com',
  templateId: 'template_xxxxx',
  variables: {
    firstName: 'John',
    orderNumber: '12345',
    items: [
      { name: 'Widget', price: 29.99 },
      { name: 'Gadget', price: 49.99 },
    ],
  },
});

Schedule Email

Schedule an email to be sent at a specific time.

schedule-email.ts
const email = await client.emails.send({
  from: 'hello@yourdomain.com',
  to: 'user@example.com',
  subject: 'Your weekly digest',
  html: '<h1>Weekly Digest</h1>',
  scheduledAt: '2024-12-25T09:00:00Z',
});

// Cancel a scheduled email
await client.emails.cancel(email.id);

List Emails

Retrieve a list of emails with optional filters.

GET /v1/emails
list-emails.ts
// List all emails
const { data, hasMore, nextCursor } = await client.emails.list();

// Filter by status
const delivered = await client.emails.list({
  status: 'delivered',
  limit: 50,
});

// Paginate through results
let cursor;
do {
  const page = await client.emails.list({ cursor, limit: 100 });
  console.log(page.data);
  cursor = page.nextCursor;
} while (cursor);

Get Email

Retrieve a single email by ID.

GET /v1/emails/:id
get-email.ts
const email = await client.emails.get('email_xxxxx');

console.log(email.status);     // 'delivered'
console.log(email.sentAt);     // '2024-01-01T12:00:00Z'
console.log(email.piiDetected); // false

Email Status

Emails progress through the following statuses:

StatusDescription
pendingEmail is being processed
queuedEmail is queued for sending
sendingEmail is being sent
sentEmail was sent successfully
deliveredEmail was delivered to recipient
bouncedEmail bounced (hard or soft)
failedEmail failed to send
cancelledScheduled email was cancelled

PII Detection

Emails are automatically scanned for PII before sending. The behavior depends on your account settings:

PII Blocking Mode

When enabled, emails containing PII will be blocked and return a 422 error with details about the detected PII types.

pii-detection.ts
import { PiiDetectedError } from '@veilmail/sdk';

try {
  await client.emails.send({
    from: 'hello@yourdomain.com',
    to: 'user@example.com',
    subject: 'Your order',
    html: 'Your SSN is 123-45-6789', // Contains PII!
  });
} catch (error) {
  if (error instanceof PiiDetectedError) {
    console.log('PII detected:', error.piiTypes);
    // ['US_SOCIAL_SECURITY_NUMBER']
  }
}