const result = await client.sendEmail({ to: 'recipient@example.com', from: 'sender@yourdomain.com', subject: 'Hello from Mailblock!', text: 'This is my first email using the Mailblock SDK.'});if (result.success) { console.log('Email sent successfully!'); console.log('Email ID:', result.data.id);} else { console.error('Failed to send email:', result.error);}
const result = await client.email() .to('recipient@example.com') .from('sender@yourdomain.com') .subject('Hello from Mailblock!') .text('This email was sent using method chaining.') .send();
You can send rich HTML emails instead of plain text:
Copy
const result = await client.sendEmail({ to: 'recipient@example.com', from: 'sender@yourdomain.com', subject: 'Welcome to Our Service', html: ` <h1>Welcome!</h1> <p>Thanks for signing up. We're excited to have you on board.</p> <a href="https://yoursite.com/dashboard">Get Started</a> `});
Schedule emails to be sent at a future date and time:
Copy
// Schedule email for 1 hour from nowconst scheduledTime = new Date(Date.now() + 60 * 60 * 1000);const result = await client.sendEmail({ to: 'recipient@example.com', from: 'sender@yourdomain.com', subject: 'Scheduled Email', text: 'This email was scheduled to be sent later.', scheduledAt: scheduledTime});if (result.success) { console.log('Email scheduled successfully!');}
You can also use ISO date strings:
Copy
const result = await client.sendEmail({ to: 'recipient@example.com', from: 'sender@yourdomain.com', subject: 'Scheduled Email', text: 'This email was scheduled using an ISO string.', scheduledAt: '2024-12-25T09:00:00.000Z' // Christmas morning});
Enable debug mode during development to see detailed logs:
Copy
const client = new Mailblock('your-api-key', { debug: true});// Now you'll see detailed logs for all API callsconst result = await client.sendEmail({ to: 'test@example.com', from: 'sender@yourdomain.com', subject: 'Debug Test', text: 'Testing with debug mode enabled'});
The SDK includes full TypeScript support out of the box:
Copy
import Mailblock, { EmailOptions, EmailResponse } from 'mailblock';const client: Mailblock = new Mailblock('your-api-key');const emailOptions: EmailOptions = { to: 'recipient@example.com', from: 'sender@yourdomain.com', subject: 'TypeScript Email', text: 'This email was sent with full type safety!'};const result: EmailResponse = await client.sendEmail(emailOptions);