Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added DKIM signing support

  • Loading branch information...
commit 52cf687f88e74e7188c6cd87a09d830a9a413060 1 parent d2d3deb
Andris Reinman authored
View
5 .documentor.json
@@ -1,5 +0,0 @@
-{
- "project": "nodemailer",
- "engine": "jsdoc-toolkit",
- "source": "lib"
-}
View
38 README.md
@@ -25,6 +25,7 @@ in a more structured way (with TOC).
* **Preconfigured** services for using SMTP with Gmail, Hotmail etc.
* Use objects as header values for **SendGrid** SMTP API
* **XOAUTH** authentication support and token generation (3-legged OAuth) - useful with Gmail
+ * **DKIM** signing
## Check out my other mail related modules
@@ -241,6 +242,43 @@ use it to define the path to the *sendmail* command
var transport = nodemailer.createTransport("Sendmail", "/usr/bin/sendmail");
+### DKIM Signing
+
+**Nodemailer** supports DKIM signing with very simple setup. Use this with caution
+though since the generated message needs to be buffered entirely before it can be
+signed - in this case the streaming capability offered by mailcomposer is illusionary,
+there will only be one `'data'` event with the entire message. Not a big deal with
+small messages but might consume a lot of RAM when using larger attachments.
+
+Set up the DKIM signing with `useDKIM` method for a transport object:
+
+ transport.useDKIM(dkimOptions)
+
+Where `dkimOptions` includes necessary options for signing
+
+ * **domainName** - the domainname that is being used for signing
+ * **keySelector** - key selector. If you have set up a TXT record with DKIM public key at *zzz._domainkey.example.com* then `zzz` is the selector
+ * **privateKey** - DKIM private key that is used for signing as a string
+ * **headerFieldNames** - optional colon separated list of header fields to sign, by default all fields suggested by RFC4871 #5.5 are used
+
+All messages transmitted through this transport objects are from now on DKIM signed.
+
+**NB!** Currently if several header fields with the same name exists, only the last one (the one in the bottom) is signed.
+
+Example:
+
+ var transport = nodemailer.createTransport("Sendmail");
+
+ transport.useDKIM({
+ domainName: "node.ee",
+ keySelector: "dkim",
+ privateKey: fs.readFileSync("private_key.pem")
+ });
+
+ transport.sendMail(mailOptions);
+
+See examples/example_dkim.js for a complete example.
+
### Well known services for SMTP
If you want to use a well known service as the SMTP host, you do not need
View
76 examples/example_dkim.js
@@ -0,0 +1,76 @@
+var nodemailer = require('../lib/nodemailer'),
+ fs = require("fs"),
+ pathlib = require("path");
+
+// Create a SMTP transport object
+var transport = nodemailer.createTransport("sendmail");
+
+// Set up DKIM signing for outgoing messages with this transport object
+transport.useDKIM({
+ domainName: "do-not-trust.node.ee", // signing domain
+ keySelector: "dkim", // selector name (in this case there's a dkim._domainkey.do-not-trust.node.ee TXT record set up)
+ privateKey: fs.readFileSync(pathlib.join(__dirname,"test_private.pem"))
+});
+
+// Message object
+var message = {
+
+ // sender info
+ from: 'Sender Name <sender@example.com>',
+
+ // Comma separated list of recipients
+ to: '"Receiver Name" <receiver@example.com>',
+
+ // Subject of the message
+ subject: 'Nodemailer is unicode friendly ✔', //
+
+ headers: {
+ 'X-Laziness-level': 1000,
+ },
+
+ // plaintext body
+ text: 'Hello to myself!',
+
+ // HTML body
+ html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
+ '<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:nyan@node"/></p>',
+
+ // An array of attachments
+ attachments:[
+
+ // String attachment
+ {
+ fileName: 'notes.txt',
+ contents: 'Some notes about this e-mail',
+ contentType: 'text/plain' // optional, would be detected from the filename
+ },
+
+ // Binary Buffer attachment
+ {
+ fileName: 'image.png',
+ contents: new Buffer('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
+ '//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
+ 'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'base64'),
+
+ cid: 'note@node' // should be as unique as possible
+ },
+
+ // File Stream attachment
+ {
+ fileName: 'nyan cat ✔.gif',
+ filePath: __dirname+"/nyan.gif",
+ cid: 'nyan@node' // should be as unique as possible
+ }
+ ]
+};
+
+console.log('Sending Mail');
+transport.sendMail(message, function(error){
+ if(error){
+ console.log('Error occured');
+ console.log(error.message);
+ return;
+ }
+ console.log('Message sent successfully!');
+ transport.close(); // close the connection pool
+});
View
12 examples/test_private.pem
@@ -0,0 +1,12 @@
+-----BEGIN RSA PRIVATE KEY-----
+MIIBywIBAAJhANCx7ncKUfQ8wBUYmMqq6ky8rBB0NL8knBf3+uA7q/CSxpX6sQ8N
+dFNtEeEd7gu7BWEM7+PkO1P0M78eZOvVmput8BP9R44ARpgHY4V0qSCdUt4rD32n
+wfjlGbh8p5ua5wIDAQABAmAm+uUQpQPTu7kg95wqVqw2sxLsa9giT6M8MtxQH7Uo
+1TF0eAO0TQ4KOxgY1S9OT5sGPVKnag258m3qX7o5imawcuyStb68DQgAUg6xv7Af
+AqAEDfYN5HW6xK+X81jfOUECMQDr7XAS4PERATvgb1B3vRu5UEbuXcenHDYgdoyT
+3qJFViTbep4qeaflF0uF9eFveMcCMQDic10rJ8fopGD7/a45O4VJb0+lRXVdqZxJ
+QzAp+zVKWqDqPfX7L93SQLzOGhdd7OECMQDeQyD7WBkjSQNMy/GF7I1qxrscIxNN
+VqGTcbu8Lti285Hjhx/sqhHHHGwU9vB7oM8CMQDKTS3Kw/s/xrot5O+kiZwFgr+w
+cmDrj/7jJHb+ykFNb7GaEkiSYqzUjKkfpweBDYECMFJUyzuuFJAjq3BXmGJlyykQ
+TweUw+zMVdSXjO+FCPcYNi6CP1t1KoESzGKBVoqA/g==
+-----END RSA PRIVATE KEY-----
View
15 lib/transport.js
@@ -19,6 +19,8 @@ module.exports.Transport = Transport;
function Transport(type, options){
this.options = options;
+
+ this.dkimOptions = false;
switch((type || "").toString().trim().toUpperCase()){
case "SMTP":
@@ -51,10 +53,23 @@ Transport.prototype.sendMailWithTransport = function(emailMessage, callback){
return callback(new Error("Invalid transport method defined"));
}
+ if(this.dkimOptions){
+ emailMessage.useDKIM(this.dkimOptions);
+ }
+
this.transport.sendMail(emailMessage, callback);
};
/**
+ * <p>Sets up DKIM signing for this transport object</p>
+ *
+ * @param {Object} dkim DKIM options
+ */
+Transport.prototype.useDKIM = function(dkim){
+ this.dkimOptions = dkim;
+};
+
+/**
* <p>Closes the transport when needed, useful with SMTP (which uses connection
* pool) but not so much with SES or Sendmail</p>
*
View
4 package.json
@@ -1,7 +1,7 @@
{
"name": "nodemailer",
"description": "Easy to use module to send e-mails, supports unicode and SSL/TLS",
- "version": "0.3.11",
+ "version": "0.3.13",
"author" : "Andris Reinman",
"maintainers":[
{
@@ -25,7 +25,7 @@
}
],
"dependencies": {
- "mailcomposer": ">= 0.1.10",
+ "mailcomposer": ">= 0.1.11",
"simplesmtp": ">= 0.1.15"
},
"devDependencies": {
Please sign in to comment.
Something went wrong with that request. Please try again.