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,
});
// send the message and get a callback with an error or details of the message that was sent
client.send(
{
text: 'i hope this works',
from: 'you ' ,
to: 'someone , another ' ,
cc: 'else ' ,
subject: 'testing emailjs',
},
(err, message) => {
console.log(err || message);
}
);
EXAMPLE USAGE - using async/await
// assuming top-level await for brevity
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 ' ,
to: 'someone , another ' ,
cc: 'else ' ,
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 ' ,
to: 'someone , another ' ,
cc: 'else ' ,
subject: 'testing emailjs',
attachment: [
{ data: 'i hope this works!', alternative: true },
{ path: 'path/to/file.zip', type: 'application/zip', name: 'renamed.zip' },
],
};
// send the message and get a callback with an error or details of the message that was sent
client.send(message, function (err, message) {
console.log(err || message);
});
// you can continue to send more messages with successive calls to 'client.send',
// they will be queued on the same smtp connection
// or instead of using the built-in client you can create an instance of 'smtp.SMTPConnection'
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 ' ,
to: 'someone , another ' ,
cc: 'else ' ,
subject: 'testing emailjs',
attachment: [
{ data: 'i hope this works!', alternative: true },
{ path: 'path/to/file.zip', type: 'application/zip', name: 'renamed.zip' },
],
});
// send the message and get a callback with an error or details of the message that was sent
client.send(message, (err, message) => {
console.log(err || message);
});