Skip to main content

Recipes

Send an email with attachments

IQEmailOptions.attachments accepts file paths or QEmailAttachment objects for inline content:

import { EmailEngineStore, type QEmailAttachment } from '@quik/communication';

await EmailEngineStore.get('smtp').send({
to: 'user@example.com',
subject: 'Report',
text: 'See attached.',
attachments: [
'/tmp/report.pdf',
{
filename: 'data.csv',
content: Buffer.from('a,b\n1,2'),
contentType: 'text/csv'
} satisfies QEmailAttachment
]
});

Send an SMS

import { SMSEngineStore } from '@quik/communication';

await SMSEngineStore.get('twilio').send({
to: '+40700111222',
text: 'Your code is 123456'
});

Send a WhatsApp template message

import { WhatsAppEngineStore, WhatsAppMessages } from '@quik/communication';

const msg = WhatsAppMessages.template('40700111222', {
name: 'otp_code',
language: { code: 'en' },
components: [
{
type: 'body',
parameters: [ { type: 'text', text: '123456' } ]
}
]
});

await WhatsAppEngineStore.get('meta').send(msg);

WhatsAppMessages also builds text, image, video, audio, document, sticker, location, and reaction payloads.

Inject the configured default engine

import { GetEmailEngine, type QEmailEngine } from '@quik/communication';

class NotificationService {
@GetEmailEngine()
private emailEngine: QEmailEngine;
}

GetEmailEngine()/GetSMSEngine()/GetWhatsAppEngine() are class field decorators that resolve the engine registered under whatever communication.<channel>.engine is currently set to, instead of a hardcoded engine name. Pass a name (e.g. GetEmailEngine('smtp')) to override the configured default.

Register a custom email engine

import {
EmailEngineStore,
QEmailEngine,
QEmailEngineType,
type IQEmailOptions
} from '@quik/communication';

class ConsoleEmailEngine extends QEmailEngine {
protected get transportType() {
return QEmailEngineType.TEST;
}

protected async _send(message: IQEmailOptions) {
console.log('Email subject:', message.subject);
}
}

EmailEngineStore.add('console', ConsoleEmailEngine);
await EmailEngineStore.get('console').send({
to: 'user@example.com',
subject: 'Welcome',
text: 'Hi there.'
});

Configure retries and timeouts

Retries only kick in for CommunicationProviderTimeoutError or unexpected errors — a QTranslatableError thrown by _send (for example a validation error) is not retried and is rethrown immediately:

// communication.email.retry.attempts = 3
// communication.email.retry.delayMilliseconds = 200
// communication.email.retry.backoffFactor = 2
// communication.email.timeoutMilliseconds = 5000