@wydev/amail
v1.2.7
Published
Official Node.js SDK for Amail — self-hosted email proxy gateway with Resend-compatible API
Maintainers
Readme
@wydev/amail
Amail Node SDK。
安装
npm install @wydev/amail使用
import { Amail } from '@wydev/amail';
const amail = new Amail('am_your_api_key', {
baseUrl: 'https://amail.your-domain.com',
providerId: 'smtp_config_id',
});
const { data, error } = await amail.emails.send({
from: '[email protected]',
to: '[email protected]',
subject: '你好',
html: '<h1>测试邮件</h1>',
});
if (error) {
console.error(error);
} else {
console.log('已发送:', data.id);
}API
new Amail(key?, options?)
创建客户端。
key:API Key,可从AMAIL_API_KEY读取。options.baseUrl:服务地址,默认http://localhost:3000。options.userAgent:自定义 User-Agent。options.providerId:默认 SMTP Provider ID,可从AMAIL_PROVIDER_ID读取。
amail.emails
emails.send(payload, options?)
发送单封邮件。等同 emails.create()。
providerId 可在初始化时设置,也可单次发送传入。
const { data, error } = await amail.emails.send({
providerId: 'smtp_config_id',
from: '示例 <[email protected]>',
to: ['[email protected]', '[email protected]'],
subject: '你好',
html: '<p>测试邮件</p>',
text: '测试邮件',
cc: '[email protected]',
bcc: '[email protected]',
replyTo: '[email protected]',
headers: { 'X-Trace': 'demo' },
tags: [{ name: 'type', value: 'test' }],
scheduledAt: '2024-12-01T12:00:00Z',
}, {
idempotencyKey: 'unique-key-123',
});emails.get(id)
查询邮件。
const { data, error } = await amail.emails.get('email_id');emails.list(options?)
分页列表。
const { data, error } = await amail.emails.list({ limit: 10 });
const next = await amail.emails.list({ limit: 10, after: data.data[data.data.length - 1].id });emails.cancel(id)
取消排队或定时邮件。
const { data, error } = await amail.emails.cancel('email_id');amail.batch
batch.send(payload, options?)
批量发送。等同 batch.create()。
const { data, error } = await amail.batch.send([
{ providerId: 'smtp_config_id', from: '[email protected]', to: '[email protected]', subject: '你好 1', html: '<p>测试邮件 1</p>' },
{ providerId: 'smtp_config_id', from: '[email protected]', to: '[email protected]', subject: '你好 2', html: '<p>测试邮件 2</p>' },
]);错误
API 错误返回 { data, error }。
const { data, error } = await amail.emails.send({ ... });
if (error) {
console.log(error.statusCode);
console.log(error.name);
console.log(error.message);
}从 Resend 迁移
import { Resend } from 'resend';
const resend = new Resend('re_xxx');
import { Amail } from '@wydev/amail';
const amail = new Amail('am_xxx', { baseUrl: 'https://amail.your-domain.com' });Amail 需要指定 providerId。
许可证
MIT
