resend http api
This commit is contained in:
@@ -35,9 +35,6 @@ function serializeConfig(smtp: SmtpConfig) {
|
||||
}
|
||||
|
||||
function buildTransportUrl(config: SmtpConfig): string {
|
||||
if (config.provider === 'resend') {
|
||||
return `smtps://resend:${config.apiKey}@smtp.resend.com:465`;
|
||||
}
|
||||
if (config.provider === 'mailhog') {
|
||||
return `smtp://${config.host ?? 'localhost'}:${config.port ?? 1025}`;
|
||||
}
|
||||
@@ -72,8 +69,20 @@ smtpRouter.put('/', async (ctx) => {
|
||||
|
||||
smtpRouter.post('/test-connection', async (ctx) => {
|
||||
try {
|
||||
const { transport } = await getTransport();
|
||||
await transport.verify();
|
||||
const result = await getTransport();
|
||||
|
||||
if (result.type === 'resend') {
|
||||
const res = await fetch('https://api.resend.com/domains', {
|
||||
headers: { 'Authorization': `Bearer ${result.apiKey}` },
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
throw new Error(err.message ?? `Resend API error: ${res.status}`);
|
||||
}
|
||||
return ctx.json({ success: true });
|
||||
}
|
||||
|
||||
await result.transport.verify();
|
||||
return ctx.json({ success: true });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown error';
|
||||
@@ -93,17 +102,29 @@ smtpRouter.post('/test', async (ctx) => {
|
||||
if (body.password?.includes('****')) body.password = saved.password;
|
||||
}
|
||||
|
||||
const from = `${body.fromName} <${body.fromEmail}>`;
|
||||
const testHtml = '<h2>Officer Test Email</h2><p>If you received this, your email configuration is working correctly.</p>';
|
||||
|
||||
try {
|
||||
if (body.provider === 'resend') {
|
||||
const res = await fetch('https://api.resend.com/emails', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${body.apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ from, to: body.to, subject: 'Officer Test Email', html: testHtml }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const err = await res.json();
|
||||
throw new Error(err.message ?? `Resend API error: ${res.status}`);
|
||||
}
|
||||
return ctx.json({ success: true });
|
||||
}
|
||||
|
||||
const url = buildTransportUrl(body);
|
||||
const transport = createTransport(url);
|
||||
const from = `${body.fromName} <${body.fromEmail}>`;
|
||||
|
||||
await transport.sendMail({
|
||||
from,
|
||||
to: body.to,
|
||||
subject: 'Officer Test Email',
|
||||
html: '<h2>Officer Test Email</h2><p>If you received this, your email configuration is working correctly.</p>',
|
||||
});
|
||||
await transport.sendMail({ from, to: body.to, subject: 'Officer Test Email', html: testHtml });
|
||||
return ctx.json({ success: true });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'Unknown error';
|
||||
|
||||
Reference in New Issue
Block a user