emailjs
send emails, html and attachments (files, streams and strings) from node.js to any smtp server
INSTALLING
npm install emailjs
FEATURES
- works with SSL and TLS smtp servers
- supports smtp authentication ('PLAIN', 'LOGIN', 'CRAM-MD5', 'XOAUTH2')
- emails are queued and the queue is sent asynchronously
- supports sending html emails and emails with multiple attachments (MIME)
- attachments can be added as strings, streams or file paths
- supports utf-8 headers and body
- built-in type declarations
- automatically handles greylisting
REQUIRES
- auth access to an SMTP Server
- if your service (ex: gmail) uses two-step authentication, use an application specific password
EXAMPLE USAGE - text only emails
import { SMTPClient } from 'emailjs';
const client = new SMTPClient({
user: 'user',
password: 'password',
host: 'smtp.your-email.com',
ssl: true,
});
client.send(
{
text: 'i hope this works',
from: 'you <username@your-email.com>',
to: 'someone <someone@your-email.com>, another <another@your-email.com>',
cc: 'else <else@your-email.com>',
subject: 'testing emailjs',
},
(err, message) => {
console.log(err || message);
}
);
EXAMPLE USAGE - using async/await
import { SMTPClient } from 'emailjs';
const client = new SMTPClient({
user: 'user',
password: 'password',
host: 'smtp.your-email.com',
ssl: true,
});
try {
const message = await client.sendAsync({
text: 'i hope this works',
from: 'you <username@your-email.com>',
to: 'someone <someone@your-email.com>, another <another@your-email.com>',
cc: 'else <else@your-email.com>',
subject: 'testing emailjs',
});
console.log(message);
} catch (err) {
console.error(err);
}
EXAMPLE USAGE - html emails and attachments
import { SMTPClient } from 'emailjs';
const client = new SMTPClient({
user: 'user',
password: 'password',
host: 'smtp.your-email.com',
ssl: true,
});
const message = {
text: 'i hope this works',
from: 'you <username@your-email.com>',
to: 'someone <someone@your-email.com>, another <another@your-email.com>',
cc: 'else <else@your-email.com>',
subject: 'testing emailjs',
attachment: [
{ data: '<html>i <i>hope</i> this works!</html>', alternative: true },
{ path: 'path/to/file.zip', type: 'application/zip', name: 'renamed.zip' },
],
};
client.send(message, function (err, message) {
console.log(err || message);
});
EXAMPLE USAGE - sending through outlook
import { SMTPClient, Message } from 'emailjs';
const client = new SMTPClient({
user: 'user',
password: 'password',
host: 'smtp-mail.outlook.com',
tls: {
ciphers: 'SSLv3',
},
});
const message = new Message({
text: 'i hope this works',
from: 'you <username@outlook.com>',
to: 'someone <someone@your-email.com>, another <another@your-email.com>',
cc: 'else <else@your-email.com>',
subject: 'testing emailjs',
attachment: [
{ data: '<html>i <i>hope</i> this works!</html>', alternative: true },
{ path: 'path/to/file.zip', type: 'application/zip', name: 'renamed.zip' },
],
});
client.send(message, (err, message) => {
console.log(err || message);
});
EXAMPLE USAGE - attaching and embedding an image
import { SMTPClient, Message } from 'emailjs';
const client = new SMTPClient({
user: 'user',
password: 'password',
host: 'smtp-mail.outlook.com',
tls: {
ciphers: 'SSLv3',
},
});
const message = new Message({
text: 'i hope this works',
from: 'you <username@outlook.com>',
to: 'someone <someone@your-email.com>, another <another@your-email.com>',
cc: 'else <else@your-email.com>',
subject: 'testing emailjs',
attachment: [
{
data:
'<html>i <i>hope</i> this works! here is an image: <img src="cid:my-image" width="100" height ="50"> </html>',
},
{ path: 'path/to/file.zip', type: 'application/zip', name: 'renamed.zip' },
{
path: 'path/to/image.jpg',
type: 'image/jpg',
headers: { 'Content-ID': '<my-image>' },
},
],
});
client.send(message, (err, message) => {
console.log(err || message);
});
API
new SMTPClient(options)
const options = {
user,
password,
host,
port,
ssl,
tls,
timeout,
domain,
authentication,
logger,
};
SMTPClient#send(message, callback)
const headers = {
from,
to,
cc,
bcc,
text,
subject,
attachment,
};
Message#attach(options)
Can be called multiple times, each adding a new attachment.
const options = {
path,
data,
stream,
type,
name,
charset,
method,
alternative,
inline,
encoded,
headers,
related,
};
Message#checkValidity()
Synchronously validate that a Message is properly formed.
const message = new Message(options);
const { isValid, validationError } = message.checkValidity();
if (isValid) {
} else {
console.error(validationError);
}
new SMTPConnection(options={})
const options = {
user,
password,
host,
port,
ssl,
tls,
timeout,
domain,
authentication,
logger,
};
To target a Message Transfer Agent (MTA), omit all options.
SMTPConnection#authentication
associative array of currently supported SMTP authentication mechanisms
Authors
eleith
zackschuster
Testing
npm install -d
npm test
Contributions
issues and pull requests are welcome