balderdashy / sails-hook-email

Sails email hook
67 stars 34 forks source link

Recommended way to use nodemailer-dkim plug-in #24

Open kpturner opened 8 years ago

kpturner commented 8 years ago

Hi

I use sails-hook-email successfully but I am getting some SPAM issues (especially with Hotmail) and want to make use of the nodemailer-dkim plugin. However, I am struggling to find the best way to do this.

I need to be able to tell the transporter to use the plug-in signer like so

transporter.use('stream', require('nodemailer-dkim').signer({
  domainName: 'kreata.ee',
  keySelector: 'test',
  privateKey: fs.readFileSync('private.pem')
}));

but I don't get access transport object that sails-hook-email creates internally

dapriett commented 8 years ago

@kpturner - You can just create your own transporter object and pass it directly in using the transporter configuration option.

// config/email.js

var nodemailer = require('nodemailer');

// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'gmail.user@gmail.com',
        pass: 'userpass'
    }
});

transporter.use('stream', require('nodemailer-dkim').signer({
  domainName: 'kreata.ee',
  keySelector: 'test',
  privateKey: fs.readFileSync('private.pem')
}));

module.exports.email = {
    transporter: transporter
};
kpturner commented 8 years ago

Perfect - thanks

kpturner commented 8 years ago

Actually I spoke too soon. That isn't quite what I need because, unless I am wrong, that involves me knowing that I need to use DKIM when Sails lifts. In my case, I want sails-hook-email to initialise as it normally would. Later on, at runtime as the email is sent, I need to make a programmatic decision whether or not to use DKIM based on some DB config. So I need to be able to influence the transporter after it has been initialised and I need to be able to switch DKIM on/off for each email.

kpturner commented 8 years ago

For my simplistic brain I simply added a method to sails-hook-email:

**
 * Transporter.
 * @return {Object}
 */

transporter: function () {
  return transport;
} 

then when I came to send the email:

sails.hooks.email.transporter().use('stream', require('nodemailer-dkim').signer({
  domainName: 'kreata.ee',
  keySelector: 'test',
  privateKey: fs.readFileSync('private.pem')
}));

Is there any chance of including the extra method in the plug-in - or is there a better solution?

outOFFspace commented 8 years ago

apply this commit, pls.