emailjs
send text/html emails and attachments (files, streams and strings) from node.js to any smtp server
Last updated a month ago by GitHub Actions .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install emailjs 
SYNC missed versions from official npm registry.

emailjs ๐Ÿ“งโœจ

Send emails with ease!

This library lets you send rich HTML emails, attachments (from files, streams, or strings), and plain text messages to any SMTP server.

checks

What's to expect from emailjs? ๐Ÿš€

  • SSL and TLS Support: Secure connections to your SMTP servers.
  • Authentication Galore: Supports popular SMTP authentication methods like PLAIN, LOGIN, CRAM-MD5, and XOAUTH2.
  • Asynchronous Sending: Emails are queued and sent in the background.
  • Rich Content: Send HTML emails and include multiple attachments.
  • Flexible Attachments: Attachments can be files, data streams, or plain strings.
  • UTF-8 Ready: Full support for UTF-8 in headers and body.
  • Built-in Type Declarations: first-class TypeScript support.
  • Greylisting Awareness: Automatically handles greylisting to improve deliverability.

Get Started! ๐Ÿ› ๏ธ

Installing

It's super simple!

npm install emailjs

Requirements

  • Access to an SMTP Server.
  • If your email service (like Gmail) uses two-step verification, you'll need an application-specific password.

Quick Examples ๐Ÿง‘โ€๐Ÿ’ป

Here's how easy it is to send emails:

Text-Only Emails

import { SMTPClient } from 'emailjs'

const client = new SMTPClient({
	user: 'your-username',
	password: 'your-password',
	host: 'smtp.your-email.com',
	ssl: true, // Use SSL for secure connection
})

async function sendMyEmail() {
	try {
		const message = await client.sendAsync({
			text: 'Hello from emailjs! This is a test message.',
			from: 'You <your-email@example.com>',
			to: 'Someone <someone@example.com>',
			subject: 'Exciting News from emailjs! ๐ŸŽ‰',
		})
		console.log('Email sent successfully:', message)
	} catch (err) {
		console.error('Failed to send email:', err)
	} finally {
		client.smtp.close() // Don't forget to close the connection!
	}
}

sendMyEmail()

HTML Emails & Attachments

import { SMTPClient, Message } from 'emailjs'

const client = new SMTPClient({
	user: 'your-username',
	password: 'your-password',
	host: 'smtp.your-email.com',
	tls: true,
})

async function sendRichEmail() {
	const htmlContent = `
        <h1>Greetings!</h1>
        <p>This is an <b>HTML email</b> with a lovely picture and an attachment.</p>
        <img src="cid:my-image" alt="Embedded Image" width="150" height="100">
        <p>Check out the attached file!</p>
    `

	const message = new Message({
		from: 'You <your-email@example.com>',
		to: 'Someone <someone@example.com>',
		subject: 'Your Awesome HTML Email! ๐Ÿ–ผ๏ธ๐Ÿ“„',
		attachment: [
			{
				data: htmlContent,
				alternative: true, // This part is the HTML body
				contentType: 'text/html',
			},
			{
				path: 'path/to/your/document.pdf', // Attach a file from disk
				type: 'application/pdf',
				name: 'document.pdf',
			},
			{
				path: 'path/to/your/image.jpg', // Embed an image for the HTML
				type: 'image/jpeg',
				name: 'cool_image.jpg',
				// Reference in HTML with cid:my-image
				headers: { 'Content-ID': '<my-image>' },
			},
		],
	})

	try {
		await client.sendAsync(message)
		console.log('Rich email sent successfully!')
	} catch (err) {
		console.error('Failed to send rich email:', err)
	} finally {
		client.smtp.close()
	}
}

sendRichEmail()

API Reference ๐Ÿ“–

The emailjs library is fully typed, here is a brief overview of most likely to be used methods

new SMTPClient(options)

Create a new client instance to connect to your SMTP server.

const options = {
	user: 'your-username', // ๐Ÿ”‘ Username for logging into SMTP
	password: 'your-password', // ๐Ÿคซ Password for logging into SMTP
	host: 'smtp.your-email.com', // ๐ŸŒ SMTP server host (defaults to 'localhost')
	port: 587, // ๐Ÿ”Œ SMTP port (defaults: 25 unencrypted, 465 SSL, 587 TLS)
	ssl: true, // ๐Ÿ”’ Boolean or object for immediate SSL connection
	tls: true, // ๐Ÿ” Boolean or object (see typescript types) to initiate STARTTLS
	timeout: 5000, // โณ Max milliseconds to wait for SMTP responses
	domain: 'your-domain.com', // ๐Ÿ  Domain to greet SMTP with (defaults to os.hostname)
	authentication: ['PLAIN', 'LOGIN'], // ๐Ÿค Preferred authentication methods
	logger: console, // ๐Ÿ“ Override the built-in logger (e.g., custom logging)
}

SMTPClient#send(message, callback)

Sends an email message. You can pass a Message instance or a headers object.

client.send(messageObject, (err, details) => {
	if (err) console.error(err)
	else console.log('Message sent:', details)
})

SMTPClient#sendAsync(message)

a promise-based way to send emails! โœจ

try {
	const details = await client.sendAsync(messageObject)
	console.log('Message sent:', details)
} catch (err) {
	console.error('Failed to send:', err)
}

new Message(headers)

Constructs an RFC2822-compliant message object.

const headers = {
	from: 'sender@example.com', // ๐Ÿ’Œ Sender (required!)
	to: 'recipient@example.com', // ๐Ÿ“ฌ Recipients (at least one of to, cc, or bcc)
	cc: 'carbon-copy@example.com', // ๐Ÿ‘ฅ CC recipients
	bcc: 'blind-copy@example.com', // ๐Ÿ•ต๏ธโ€โ™€๏ธ BCC recipients
	subject: 'Your Subject Here', // ๐Ÿ“ Email subject
	text: 'Plain text body.', // ๐Ÿ—’๏ธ Plain text content
	attachment: [{ data: 'Hello!' }], // ๐Ÿ“Ž One or more attachments
}

Message#attach(options)

Adds an attachment to the message. Can be called multiple times.

message.attach({
	path: 'path/to/file.zip', // ๐Ÿ“ Path to a file on disk
	data: 'Binary content as string or buffer', // ๐Ÿ“„ Raw data
	stream: fs.createReadStream('file.jpg'), // ๐ŸŒŠ A readable stream
	type: 'application/zip', // MIME type
	name: 'custom-name.zip', // Filename perceived by recipient
	alternative: true, // attach inline as an alternative (e.g., HTML body)
	inline: true, // If true, attached inline (e.g., for <img src="cid:...">)
	headers: { 'X-Custom-Header': 'value' }, // Custom attachment headers
})

Message#checkValidity()

Synchronously validates that a Message is properly formed before sending.

const { isValid, validationError } = message.checkValidity()
if (!isValid) {
	console.error('Message is invalid:', validationError)
}

Authors โœ๏ธ

  • eleith
  • zackschuster

Testing ๐Ÿงช

# Run all tests
npm test

# Run tests with code coverage report
npm run test:coverage

Development ๐Ÿง‘โ€๐Ÿ’ป๐ŸŒฑ

for a local smtp testing experience, use our Mailpit compose service

1. Start Mailpit with Docker Compose

Ensure you have Docker and Docker Compose installed.

# From the project root, start Mailpit
docker compose up

Mailpit will be accessible via:

  • Web UI: http://localhost:8025
  • SMTP Server: localhost:1025

2. Run Example Sending Scripts

You can use the provided scripts to send different types of emails to your local Mailpit instance.

First, make sure the emailjs library is built:

npm run build

Then, run any of the example scripts:

# Send a plain text email
node scripts/send-text.js

# Send an HTML email
node scripts/send-html.js

# Send an email with attachments
node scripts/send-attachment.js

After running a script, open your Mailpit Web UI (http://localhost:8025) to see the emails stream in! ๐Ÿ“ฉ

Current Tags

  • 5.0.4                                ...           latest (a month ago)

79 Versions

  • 5.0.4                                ...           a month ago
  • 5.0.3                                ...           3 months ago
  • 5.0.2                                ...           4 months ago
  • 5.0.1                                ...           6 months ago
  • 5.0.0                                ...           a year ago
  • 4.0.3                                ...           3 years ago
  • 4.0.2                                ...           4 years ago
  • 4.0.1                                ...           4 years ago
  • 4.0.0                                ...           5 years ago
  • 3.8.1                                ...           5 years ago
  • 3.8.0                                ...           5 years ago
  • 3.7.0                                ...           5 years ago
  • 3.6.0                                ...           5 years ago
  • 3.5.0                                ...           5 years ago
  • 3.4.0                                ...           6 years ago
  • 3.3.0                                ...           6 years ago
  • 3.2.1                                ...           6 years ago
  • 3.2.0                                ...           6 years ago
  • 3.1.0                                ...           6 years ago
  • 3.0.0                                ...           6 years ago
  • 2.2.0                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           9 years ago
  • 1.0.12                                ...           9 years ago
  • 1.0.11                                ...           9 years ago
  • 1.0.10                                ...           9 years ago
  • 1.0.9                                ...           10 years ago
  • 1.0.8                                ...           10 years ago
  • 1.0.7                                ...           10 years ago
  • 1.0.6                                ...           10 years ago
  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           11 years ago
  • 1.0.3                                ...           11 years ago
  • 1.0.2                                ...           11 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.3.16                                ...           12 years ago
  • 0.3.15                                ...           12 years ago
  • 0.3.13                                ...           12 years ago
  • 0.3.12                                ...           12 years ago
  • 0.3.11                                ...           12 years ago
  • 0.3.10                                ...           12 years ago
  • 0.3.9                                ...           12 years ago
  • 0.3.8                                ...           13 years ago
  • 0.3.6                                ...           13 years ago
  • 0.3.5                                ...           13 years ago
  • 0.3.4                                ...           14 years ago
  • 0.3.3                                ...           14 years ago
  • 0.3.2                                ...           14 years ago
  • 0.3.1                                ...           14 years ago
  • 0.3.0                                ...           14 years ago
  • 0.2.9                                ...           14 years ago
  • 0.2.8                                ...           15 years ago
  • 0.2.7                                ...           15 years ago
  • 0.2.5                                ...           15 years ago
  • 0.2.4                                ...           15 years ago
  • 0.2.2                                ...           15 years ago
  • 0.2.1                                ...           15 years ago
  • 0.2.0                                ...           15 years ago
  • 0.1.19                                ...           15 years ago
  • 0.1.18                                ...           15 years ago
  • 0.1.17                                ...           15 years ago
  • 0.1.16                                ...           15 years ago
  • 0.1.15                                ...           15 years ago
  • 0.1.14                                ...           15 years ago
  • 0.1.13                                ...           15 years ago
  • 0.1.12                                ...           15 years ago
  • 0.1.11                                ...           16 years ago
  • 0.1.10                                ...           16 years ago
  • 0.1.9                                ...           16 years ago
  • 0.1.8                                ...           16 years ago
  • 0.1.7                                ...           16 years ago
  • 0.1.6                                ...           16 years ago
  • 0.1.5                                ...           16 years ago
  • 0.1.4                                ...           16 years ago
  • 0.1.3                                ...           16 years ago
  • 0.1.2                                ...           16 years ago
  • 0.1.1                                ...           16 years ago
  • 0.1.0                                ...           16 years ago
Maintainers (1)
Downloads
Total 200
Today 4
This Week 10
This Month 104
Last Day 4
Last Week 31
Last Month 79
Dependencies (0)
None
Dev Dependencies (8)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE