Skip to content

Node.js SDK - Sending Emails

The mailiam Node.js SDK makes sending transactional emails simple and reliable.

Send a basic email with HTML content:

import { mailiam } from '@mailiam/node';
const mailiam = new mailiam({
apiKey: process.env.MAILIAM_API_KEY
});
const result = await mailiam.emails.send({
from: 'hello@mycompany.com',
to: 'customer@example.com',
subject: 'Welcome to Our Service',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>'
});
console.log('Message ID:', result.messageId);

Provide both HTML and plain text versions:

await mailiam.emails.send({
from: 'hello@mycompany.com',
to: 'customer@example.com',
subject: 'Welcome!',
html: '<h1>Welcome!</h1><p>Thanks for signing up.</p>',
text: 'Welcome! Thanks for signing up.'
});

Specify a different reply-to address:

await mailiam.emails.send({
from: 'notifications@mycompany.com',
to: 'customer@example.com',
replyTo: 'support@mycompany.com',
subject: 'Your Order Confirmation',
html: '<p>Your order has been confirmed.</p>'
});

Add CC and BCC recipients:

await mailiam.emails.send({
from: 'hello@mycompany.com',
to: 'customer@example.com',
cc: 'team@mycompany.com',
bcc: ['archive@mycompany.com', 'analytics@mycompany.com'],
subject: 'Important Update',
html: '<p>This is an important update.</p>'
});

Send to multiple recipients:

await mailiam.emails.send({
from: 'hello@mycompany.com',
to: ['user1@example.com', 'user2@example.com', 'user3@example.com'],
subject: 'Team Announcement',
html: '<p>Important team announcement...</p>'
});

Use named email addresses for a more personal touch:

await mailiam.emails.send({
from: { name: 'John from MyCompany', email: 'john@mycompany.com' },
to: { name: 'Jane Doe', email: 'jane@example.com' },
replyTo: { name: 'Support Team', email: 'support@mycompany.com' },
subject: 'Personal Message',
html: '<p>Hi Jane, ...</p>'
});

Add custom email headers:

await mailiam.emails.send({
from: 'hello@mycompany.com',
to: 'customer@example.com',
subject: 'Custom Headers Example',
html: '<p>Email with custom headers</p>',
headers: {
'X-Custom-ID': '12345',
'X-Campaign': 'welcome-series'
}
});
async function sendWelcomeEmail(userEmail: string, userName: string) {
return await mailiam.emails.send({
from: { name: 'MyApp', email: 'hello@myapp.com' },
to: userEmail,
subject: `Welcome ${userName}!`,
html: `
<div style="font-family: Arial, sans-serif;">
<h1>Welcome ${userName}!</h1>
<p>We're excited to have you on board.</p>
<a href="https://myapp.com/get-started"
style="display: inline-block; padding: 12px 24px;
background: #007bff; color: white;
text-decoration: none; border-radius: 4px;">
Get Started
</a>
</div>
`,
text: `Welcome ${userName}! We're excited to have you on board. Get started: https://myapp.com/get-started`
});
}
async function sendPasswordReset(userEmail: string, resetToken: string) {
const resetUrl = `https://myapp.com/reset-password?token=${resetToken}`;
return await mailiam.emails.send({
from: { name: 'MyApp Security', email: 'security@myapp.com' },
to: userEmail,
subject: 'Reset Your Password',
html: `
<h2>Password Reset Request</h2>
<p>Click the link below to reset your password:</p>
<a href="${resetUrl}">Reset Password</a>
<p>This link expires in 1 hour.</p>
<p>If you didn't request this, please ignore this email.</p>
`,
text: `Reset your password: ${resetUrl}\n\nThis link expires in 1 hour.\nIf you didn't request this, please ignore this email.`
});
}
async function sendOrderConfirmation(order: Order) {
return await mailiam.emails.send({
from: { name: 'MyStore', email: 'orders@mystore.com' },
to: order.customerEmail,
subject: `Order Confirmation #${order.id}`,
html: `
<h1>Thanks for your order!</h1>
<h2>Order #${order.id}</h2>
<p>Total: $${order.total}</p>
<p>We'll send you a shipping notification when your order ships.</p>
`,
text: `Thanks for your order!\n\nOrder #${order.id}\nTotal: $${order.total}\n\nWe'll send you a shipping notification when your order ships.`
});
}
async function sendNotification(userEmail: string, message: string) {
return await mailiam.emails.send({
from: { name: 'MyApp Notifications', email: 'notifications@myapp.com' },
to: userEmail,
subject: 'New Notification',
html: `
<div style="padding: 20px; background: #f5f5f5;">
<h3>You have a new notification</h3>
<p>${message}</p>
<a href="https://myapp.com/notifications">View All Notifications</a>
</div>
`,
text: `You have a new notification\n\n${message}\n\nView all: https://myapp.com/notifications`
});
}

The send() method returns a response object:

const result = await mailiam.emails.send({ ... });
console.log(result.messageId); // "msg_abc123..."
console.log(result.success); // true
console.log(result.message); // Optional success message

See the Error Handling guide for comprehensive error handling patterns.