Skip to content

Commit

Permalink
feat: add support of replyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Sep 12, 2019
1 parent 6d21a71 commit 3f27618
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
21 changes: 20 additions & 1 deletion lib/message.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,25 @@ const MessageSchema = new Schema(
},
},

/**
* @name replyTo
* @description actual sender of the message i.e e-mail sender etc
* @type {Object}
* @since 0.1.0
* @version 1.0.0
* @instance
*/
replyTo: {
type: String,
trim: true,
index: true,
searchable: true,
fake: {
generator: 'internet',
type: 'email',
},
},

/**
* @name subject
* @description subject of the message
Expand Down Expand Up @@ -724,7 +743,7 @@ MessageSchema.methods.preValidate = function preValidate(next) {
* @instance
*/
MessageSchema.methods.isHtml = function _isHtml() {
return (this.mime === MIME_HTML) || isHtml(this.body);
return this.mime === MIME_HTML || isHtml(this.body);
};

/**
Expand Down
11 changes: 7 additions & 4 deletions lib/transports/smtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ exports.toObject = function toObject() {
* @version 0.1.0
* @private
*/
exports.init = function(options) {
exports.init = function (options) {
//initialize transport if not exist
if (!exports.transport) {
//merge options
Expand All @@ -138,7 +138,7 @@ exports.init = function(options) {
* @version 0.1.0
* @private
*/
exports._send = function(message, done) {
exports._send = function (message, done) {
//ensure transport is initialized
exports.init();

Expand All @@ -157,6 +157,9 @@ exports._send = function(message, done) {
//convert message to nodemailer email payload
let email = message.toObject();

// allow reply to
email.replyTo = (email.replyTo || email.sender);

// set mail html body
if (message.isHtml()) {
email.html = email.body;
Expand Down Expand Up @@ -184,10 +187,10 @@ exports._send = function(message, done) {
* @version 0.1.0
* @public
*/
exports.send = function(message, done) {
exports.send = function (message, done) {
//ensure email sender
message.sender = message.sender || getString('SMTP_FROM');

//perform actual smtp sending
exports._send(message, done);
};
};
15 changes: 15 additions & 0 deletions test/unit/message.schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ describe('Message Schema', () => {
expect(bcc.options.fake).to.exist;
});

it('should have replyTo field', function() {
const replyTo = Message.path('replyTo');

expect(replyTo).to.exist;
expect(replyTo).to.be.an.instanceof(SchemaTypes.String);
expect(replyTo.instance).to.be.equal('String');
expect(replyTo).to.be.an('object');
expect(replyTo.options.type).to.be.a('function');
expect(replyTo.options.type.name).to.be.equal('String');
expect(replyTo.options.trim).to.be.true;
expect(replyTo.options.index).to.be.true;
expect(replyTo.options.searchable).to.be.true;
expect(replyTo.options.fake).to.exist;
});

it('should have subject field', function() {
const subject = Message.path('subject');

Expand Down

0 comments on commit 3f27618

Please sign in to comment.