Docs/Email

Email Service

Send emails with Gmail in just a few lines of code.

Installation

npm install irismail

Quick Start

Import IrisMail from irismail/server and create an instance with your Gmail credentials.

Gmail App Password Required

You need to generate a Gmail App Password for authentication. Your regular Gmail password won't work.
app/api/send-email/route.tsts
import { IrisMail } from 'irismail/server';

const mail = new IrisMail({
  auth: {
    user: process.env.GMAIL_USER!,
    pass: process.env.GMAIL_APP_PASSWORD!,
  },
});

const result = await mail.sendMail({
  from: process.env.GMAIL_USER!,
  to: 'user@example.com',
  subject: 'Welcome to our app!',
  html: '<h1>Hello!</h1><p>Thanks for signing up.</p>',
});

console.log(result);
// { success: true, messageId: '<unique-id@smtp.gmail.com>' }

API Reference

new IrisMail(config)

Creates a new IrisMail instance configured for Gmail SMTP.

PropTypeDefaultDescription
auth.user*stringYour Gmail email address
auth.pass*stringGmail App Password (not your regular password)

mail.sendMail(options)

Sends an email and returns a promise with the result.

PropTypeDefaultDescription
from*stringSender email address
to*stringRecipient email address
subject*stringEmail subject line
html*stringHTML email body (plain text is auto-generated)

Returns:

interface SendMailResult {
  success: boolean;
  messageId: string;
}

Next.js API Route

Full example of a Next.js API route that sends emails.

app/api/send-email/route.tsts
import { IrisMail } from 'irismail/server';
import { NextRequest, NextResponse } from 'next/server';

const mail = new IrisMail({
  auth: {
    user: process.env.GMAIL_USER!,
    pass: process.env.GMAIL_APP_PASSWORD!,
  },
});

export async function POST(req: NextRequest) {
  const { to, subject, html } = await req.json();

  try {
    const result = await mail.sendMail({
      from: process.env.GMAIL_USER!,
      to,
      subject,
      html,
    });

    return NextResponse.json(result);
  } catch (error) {
    return NextResponse.json(
      { error: 'Failed to send email' },
      { status: 500 }
    );
  }
}

Environment Variables

Add these to your .env.local file:

.env.localbash
GMAIL_USER=your-email@gmail.com
GMAIL_APP_PASSWORD=your-16-character-app-password

Need OTP Components?

Check out our OTP input components or star us on GitHub.