We maintain a custom package build specifically for Meteor.js. Documentation from NPM version of mail-time
is valid and applicable to usage within Meteor.js in the same way as it's used in Node.js. The only difference in using Atmosphere/Packosphere version is in the import
/require
statement.
Mail-Time package can be installed and used within Meteor.js via NPM or Atmosphere
Install Atmosphere ostrio:mailer
package:
meteor add ostrio:mailer
Import meteor/ostrio:mailer
package
import { MailTime, MongoQueue, RedisQueue } from 'meteor/ostrio:mailer';
mail-time
package usage examples in Meteor.js
For compatibility and flexibility MailTime has no dependency on nodemailer
it should be installed and imported manually. Create one or more "SMTP transports" before initializing new MailTime instance
// transports.js
import nodemailer from 'nodemailer';
// Use DIRECT transport
// To enable sending email from localhost
// install "nodemailer-direct-transport" NPM package:
import directTransport from 'nodemailer-direct-transport';
const transports = [];
const directTransportOpts = {
pool: false,
direct: true,
name: 'mail.example.com',
from: 'no-reply@example.com',
};
transports.push(nodemailer.createTransport(directTransport(directTransportOpts)));
// IMPORTANT: Add `.options` to a newly created transport,
// this is necessary to make sure options are available to MailTime package:
transports[0].options = directTransportOpts;
export { transports };
MailTime uses separate storage for Queue management and Scheduler. In the example below MongoDB is used for both
import { MailTime, MongoQueue } from 'meteor/ostrio:mailer';
import { MongoInternals } from 'meteor/mongo';
import { transports } from './transports.js';
const mailQueue = new MailTime({
queue: new MongoQueue({
db: MongoInternals.defaultRemoteCollectionDriver().mongo.db,
}),
josk: {
adapter: {
type: 'mongo',
db: MongoInternals.defaultRemoteCollectionDriver().mongo.db,
}
},
transports,
from(transport) {
// To pass spam-filters `from` field should be correctly set
// for each transport, check `transport` object for more options
return `"Awesome App" <${transport.options.from}>`;
},
onError(error, email, details) {
console.log(`Email "${email.mailOptions.subject}" wasn't sent to ${email.mailOptions.to}`, error, details);
},
onSent(email, details) {
console.log(`Email "${email.mailOptions.subject}" successfully sent to ${email.mailOptions.to}`, details);
},
});
MailTime uses separate storage for Queue management and Scheduler. In the example below MongoDB is used for queue and Redis is used for scheduler
import { MailTime, MongoQueue } from 'meteor/ostrio:mailer';
import { MongoInternals } from 'meteor/mongo';
import { createClient } from 'redis';
import { transports } from './transports.js';
const mailQueue = new MailTime({
queue: new MongoQueue({
db: MongoInternals.defaultRemoteCollectionDriver().mongo.db,
}),
josk: {
adapter: {
type: 'redis',
client: await createClient({ url: 'redis://url' }).connect(),
}
},
transports,
from(transport) {
return `"Awesome App" <${transport.options.from}>`;
}
});
MailTime uses separate storage for Queue management and Scheduler. In the example below Redis is used for queue and MongoDB is used for scheduler
import { MailTime, RedisQueue } from 'meteor/ostrio:mailer';
import { MongoInternals } from 'meteor/mongo';
import { createClient } from 'redis';
import { transports } from './transports.js';
const mailQueue = new MailTime({
queue: new RedisQueue({
client: await createClient({ url: 'redis://url' }).connect(),
}),
josk: {
adapter: {
type: 'mongo',
db: MongoInternals.defaultRemoteCollectionDriver().mongo.db,
}
},
transports,
from(transport) {
return `"Awesome App" <${transport.options.from}>`;
}
});
MailTime uses separate storage for Queue management and Scheduler. In the example below Redis is used for both
import { MailTime, RedisQueue } from 'meteor/ostrio:mailer';
import { MongoInternals } from 'meteor/mongo';
import { createClient } from 'redis';
import { transports } from './transports.js';
const redisClient = await createClient({ url: 'redis://url' }).connect();
const mailQueue = new MailTime({
queue: new RedisQueue({
client: redisClient,
}),
josk: {
adapter: {
type: 'redis',
client: redisClient,
}
},
transports,
from(transport) {
return `"Awesome App" <${transport.options.from}>`;
}
});
Run automated tests
- Clone this package
- In Terminal (Console) go to directory where package is cloned
- Get URL for Redis database (local or remote)
- Then run:
# Default
REDIS_URL="redis://127.0.0.1:6379" meteor test-packages ./ --driver-package=meteortesting:mocha
# Test with specific domain
EMAIL_DOMAIN="example.com" REDIS_URL="redis://127.0.0.1:6379" meteor test-packages ./ --driver-package=meteortesting:mocha
# In case of the errors, — enable DEBUG for detailed output
DEBUG="true" REDIS_URL="redis://127.0.0.1:6379" meteor test-packages ./ --driver-package=meteortesting:mocha
# With custom port
REDIS_URL="redis://127.0.0.1:6379" meteor test-packages ./ --driver-package=meteortesting:mocha --port 8888
# With local MongoDB
MONGO_URL="mongodb://127.0.0.1:27017/meteor-mail-time-test-001" REDIS_URL="redis://127.0.0.1:6379" meteor test-packages ./ --driver-package=meteortesting:mocha
# Be patient, tests are taking around 2 mins