Home/Integrations/Better Auth
Modern TypeScript Authentication•Full-Stack Auth Framework

How to Configure Mailofly Email Provider in Better Auth

Integrate Mailofly with Better Auth for instant email verification, password reset links, two-factor authentication codes, and passwordless magic links.

npm install better-auth @mailofly/node
Setup Tutorial

Get Up and Running in 3 Easy Steps

01

Install Better Auth and Mailofly

Run `npm install better-auth @mailofly/node` in your Next.js, Nuxt, or Node project.

02

Configure MAILOFLY_API_KEY

Add your Mailofly secret key to `.env` and verify your sender domain in the Mailofly dashboard.

03

Add email hooks to betterAuth()

Implement the `sendVerificationEmail` and `sendResetPassword` functions calling `mailofly.emails.send()`.

Production-Ready Code Example

Copy and paste into your project. Fully typed and tested.

lib/auth.ts
import { betterAuth } from "better-auth";
import { Mailofly } from "@mailofly/node";

const mailofly = new Mailofly({
  apiKey: process.env.MAILOFLY_API_KEY!,
});

export const auth = betterAuth({
  emailAndPassword: {
    enabled: true,
    async sendResetPassword({ user, url }) {
      await mailofly.emails.send({
        from: "Security Team <auth@yourdomain.com>",
        to: [user.email],
        subject: "Reset your password",
        html: `
          <p>Hi ${user.name || "there"},</p>
          <p>We received a request to reset your password. Click the link below:</p>
          <p><a href="${url}" style="display:inline-block;padding:10px 18px;background:#000;color:#fff;border-radius:6px;text-decoration:none;">Reset Password</a></p>
          <p>If you didn't request this, you can safely ignore this email.</p>
        `,
      });
    },
  },
  emailVerification: {
    sendOnSignUp: true,
    async sendVerificationEmail({ user, url }) {
      await mailofly.emails.send({
        from: "Security Team <auth@yourdomain.com>",
        to: [user.email],
        subject: "Verify your email address",
        html: `
          <h2>Welcome, ${user.name || "User"}!</h2>
          <p>Please confirm your email address by clicking the link below:</p>
          <p><a href="${url}">Verify Email Address</a></p>
        `,
      });
    },
  },
});
Why Developers Love It

Engineered for Better Auth

Sub-Second Auth Code Delivery

Users expect instant OTPs and magic links. Mailofly delivers transactional emails to inboxes in under 1.2 seconds.

30-Day Audit Logs for Debugging

Inspect exact delivery status, timestamps, and bounce reasons for all verification emails directly in the Mailofly dashboard.

Type-Safe Authentication Flow

Both Better Auth and the Mailofly SDK provide end-to-end TypeScript types for zero runtime errors.

Got Questions?

Frequently Asked Questions about Better Auth

SMTP connections require handshakes and open TCP sockets that frequently stall or time out in serverless deployment environments like Vercel. Mailofly uses lightning-fast HTTP REST API calls, eliminating cold-start latency.

Start Sending from Better Auth in Minutes

3,000 free emails every month. Copy your API key, run the code snippet, and watch your delivery logs in real time.