Lines 217-355markdown
218 html: "<h1>Hello World</h1>",
221const result = await mail.send(emailOptions);
226The failover provider allows you to configure multiple email providers as backups. If the primary provider fails, it will automatically try the next provider in the list.
229import { createMail, MailMessage, failoverProvider, resendProvider, smtpProvider } from "@visulima/email";
231// Create individual providers
232const resend = resendProvider({ apiKey: "re_xxx" });
233const smtp = smtpProvider({
234 host: "smtp.example.com",
236 user: "user@example.com",
237 password: "password",
240// Create failover provider with multiple mailers
241const failover = failoverProvider({
243 resend, // Try Resend first
244 smtp, // Fallback to SMTP if Resend fails
246 retryAfter: 60, // Wait 60ms before trying next provider (default: 60)
249// Use failover provider
250const mail = createMail(failover);
252const message = new MailMessage().to("user@example.com").from("sender@example.com").subject("Hello").html("<h1>Hello World</h1>");
254const result = await mail.send(message);
256// The failover provider will try Resend first, and if it fails,
257// automatically try SMTP
260You can also use provider factories directly:
263import { failoverProvider, resendProvider, smtpProvider } from "@visulima/email";
265const failover = failoverProvider({
267 resendProvider({ apiKey: "re_xxx" }), // Provider factory
270 host: "smtp.example.com",
272 user: "user@example.com",
273 password: "password",
276 retryAfter: 100, // Wait 100ms between attempts
280### Round-Robin Provider
282The round-robin provider distributes your email sending workload across multiple providers. Each email is sent using the next available provider in rotation, providing load balancing across your mailers.
285import { createMail, MailMessage, roundRobinProvider, resendProvider, smtpProvider } from "@visulima/email";
287// Create individual providers
288const resend = resendProvider({ apiKey: "re_xxx" });
289const smtp = smtpProvider({
290 host: "smtp.example.com",
292 user: "user@example.com",
293 password: "password",
296// Create round-robin provider with multiple mailers
297const roundRobin = roundRobinProvider({
299 resend, // First provider
300 smtp, // Second provider
302 retryAfter: 60, // Wait 60ms before trying next provider if current is unavailable (default: 60)
305// Use round-robin provider
306const mail = createMail(roundRobin);
308// Each email will be distributed across providers in rotation
309const message1 = new MailMessage().to("user1@example.com").from("sender@example.com").subject("Email 1").html("<h1>Email 1</h1>");
310await mail.send(message1);
311// Uses resend (or random start)
313const message2 = new MailMessage().to("user2@example.com").from("sender@example.com").subject("Email 2").html("<h1>Email 2</h1>");
315// Uses smtp (next in rotation)
317const message3 = new MailMessage().to("user3@example.com").from("sender@example.com").subject("Email 3").html("<h1>Email 3</h1>");
318await mail.send(message3);
319// Uses resend (back to first)
322You can also use provider factories directly:
325import { roundRobinProvider, resendProvider, smtpProvider } from "@visulima/email";
327const roundRobin = roundRobinProvider({
329 resendProvider({ apiKey: "re_xxx" }), // Provider factory
332 host: "smtp.example.com",
334 user: "user@example.com",
335 password: "password",
338 retryAfter: 100, // Wait 100ms between attempts
342**Note:** The round-robin provider starts at a random provider and then rotates through providers for each subsequent email. If a provider is unavailable, it will automatically try the next provider in the rotation.
346AutoSend is a transactional and marketing email service with volume-based pricing. Universal runtime (Fetch API).
349import { createMail } from "@visulima/email";
350import { autoSendProvider } from "@visulima/email/providers/autosend";
352const mail = createMail(
354 apiKey: process.env.AUTOSEND_API_KEY,