P H O T O N

CONSOLE

Loading

Node.js & TypeScript Guide

Node.js Nodemailer SMTP Relay Configuration
Connection Pooling • Async/Await • Next.js Setup

Integrate Nodemailer with PhotonRelay in Node.js, Express, and Next.js. Implement connection pooling, custom headers, and sub-second OTP dispatches.

5 min read • Node 18, 20 & 22 • Full TypeScript Support
Direct Answer: Configuring Nodemailer

To configure Nodemailer with an SMTP relay, call nodemailer.createTransport({ host: 'mail.photonrelay.com', port: 587, secure: false, auth: { user: process.env.SMTP_USER, pass: process.env.SMTP_PASS } }) and dispatch messages with transporter.sendMail(options). For high-concurrency microservices, enable pool: true with maxConnections: 5 to reuse TCP sockets and slash latency.

1. Installing Nodemailer & Creating the Transporter

npm install nodemailer
# Optional for TypeScript projects:
npm install -D @types/nodemailer

Create a reusable mail client module (mailer.js or mailer.ts):

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  host: "mail.photonrelay.com",
  port: 587,
  secure: false, // true for port 465, false for 587 STARTTLS
  auth: {
    user: process.env.PHOTON_SMTP_KEY,
    pass: process.env.PHOTON_SMTP_SECRET
  },
  pool: true, // reuse socket connections
  maxConnections: 5,
  maxMessages: 100
});

module.exports = transporter;

2. Enabling Connection Pooling (pool: true)

Without connection pooling, Nodemailer establishes a fresh TCP socket, performs a 3-way handshake, and negotiates a new TLS session for every single email sent. Under heavy concurrency, this exhausts operating system file descriptors.

Setting pool: true maintains persistent open connections to PhotonRelay Anycast nodes, slashing latency down to under 350 milliseconds per dispatch.

3. Sending HTML Email with Dynamic Data

async function sendVerificationEmail(recipientEmail, otpCode) {
  const info = await transporter.sendMail({
    from: '"Security Team" <auth@yourdomain.com>',
    to: recipientEmail,
    subject: "Your Authentication Code",
    text: `Your OTP code is: ${otpCode}`,
    html: `<h2>Your Verification Code</h2><p style="font-size:24px;font-weight:bold;color:#4F46E5;">${otpCode}</p>`,
    headers: {
      'X-Entity-Ref-ID': 'otp_9821'
    }
  });
  console.log("Message delivered: %s", info.messageId);
}

4. Integration in Next.js Server Actions

// app/actions/send-email.ts
'use server';

import transporter from '@/lib/mailer';

export async function handleContactForm(formData: FormData) {
  const email = formData.get('email') as string;
  const message = formData.get('message') as string;

  try {
    await transporter.sendMail({
      from: '"Inquiries" <contact@yourdomain.com>',
      to: 'team@yourdomain.com',
      subject: `New Contact Submission from ${email}`,
      text: message
    });
    return { success: true };
  } catch (error) {
    return { success: false, error: 'Dispatch failed' };
  }
}

Send Sub-Second Emails in Node.js with PhotonRelay

Plug into globally distributed Anycast edge nodes. Enjoy 5,000 free monthly emails and pay-per-use scaling at flat $0.50 per 1,000 emails.

6. Frequently Asked Questions

In Nodemailer, 'secure: false' indicates STARTTLS negotiation on port 587. The socket connects unencrypted initially and upgrades to encrypted TLS 1.3 before credentials are sent. 'secure: true' is reserved exclusively for implicit SSL sockets on port 465.

Enable 'pool: true' with 'maxConnections: 5' in your createTransport configuration. This maintains a pool of persistent TCP connections, reusing open TLS sockets across batches instead of performing a new 3-way handshake for every individual email.

Yes. Declare your email dispatch function with 'use server', initialize the transporter outside the request scope for socket reuse, and invoke sendMail inside your server action.