title: making node.js' nodemailer work with dkim smtp
body: <p>I have difficulties to set up DKIM signing with node.js and nodemailer. 
  Here is what I got:
  A regular self hosted SMTP-email like you@yourpersonalwebsite.com on hosteurope.com
  A domain with DNS settings hosted at inwx.com
  Node.js 8.4.0</p>
  
  <p>Step 1: Generating the DKIM keys.
  I went to <a href="https://www.port25.com/dkim-wizard/" rel="nofollow noreferrer">https://www.port25.com/dkim-wizard/</a> and entered my domain name and hit generate key or whatever. Then I got stuff I should put as TXT record into the DNS settings at my domain registrar.
  To ensure that it works I ran:</p>
  
  <pre><code>dig TXT default._domainkey.example.com
  </code></pre>
  
  <p>and got </p>
  
  <pre><code>; &lt;&lt;&gt;&gt; DiG 9.8.3-P1 &lt;&lt;&gt;&gt; TXT default._domainkey.mydomain.com
  ;; global options: +cmd
  ;; Got answer:
  ;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 28927
  ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
  
  ;; QUESTION SECTION:
  ;default._domainkey.mydomain.com. IN    TXT
  
  ;; ANSWER SECTION:
  default._domainkey.mydomain.com. 3554 IN TXT    "k=rsa\; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmwCHK6Szd9ObPoD9I4JkdB9+W7a/IzGKnntAbWnHitMxQyl4TsmofRm+RZDS/Ije99opzmjLBeaCiiNXYs6nvhaVR4lrN9IFpmaJ5yuyicmE9HCDM99qlPUUzgk4l3YAGVSNK83FS7UNc5r0Ymh1bWKKB6FFwqgjVAzP6yJ7Bst0C88Wko0UHSJqeg3Y4mfeys9p6yyro4" "HnFJ+UQGYtwGD11z5+MdHI9D5eIa1TC7t0VSRYll94n4VHZI43uGO+jk3tm/LCMaYaksEiMD55rLUg78VAhh0pDsNPowbeXxcBYITqv9oMn7tNZQQKmXRU5G/WwsBpi9wrJuja7vl22wIDAQAB"
  
  ;; Query time: 0 msec
  ;; SERVER: 192.168.1.1#53(192.168.1.1)
  ;; WHEN: Tue Oct 17 13:53:10 2017
  ;; MSG SIZE  rcvd: 464
  </code></pre>
  
  <p>Look here how I set the public key in DNS settings:</p>
  
  <p><a href="https://i.stack.imgur.com/jl9hQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jl9hQ.png" alt="inwx dkim settings"></a></p>
  
  <p>Step 2: 
  Downloading the private key. I put it in a file and named it dkim.pem.</p>
  
  <p>Step 3:
  Set up a simple node.js program that sends a dkim signed email.</p>
  
  <pre><code>var fs = require('fs');
  var nodemailer = require('nodemailer');
  var EmailTemplate = require('email-templates').EmailTemplate
  var path = require('path')
  
  // config of nodemailer
  var poolConfig = {
      pool: true
      , host: 'mail.example.com'
      , port: 25
      , secure: false // use SSL
      , auth: {
              user: 'info@example.com',
              pass: 'mypassword'
      }, tls: {
              rejectUnauthorized: false
      }, dkim: {
              domainName: 'example.com'
              , keySelector: 'default'
              , privateKey: fs.readFileSync('./certificates/dkim/dkim.pem', "utf8")
              , cacheDir: '/tmp'
              , cacheTreshold: 100 * 1024
      }
  };
  
  // use config to generate a transporter
  var transporter = nodemailer.createTransport(poolConfig);
  
  // the email and its content as json
  var user = {email: "info@example.com"};
  var message = {
      template : "contactform"
      , subject : "Message from Simon"
      , pageData : {formdata: {salutation : "salutation", firstname: "firstname", lastname : "lastname", phone : "phone", email: "email", subject : "subject", text : "text" }}
  };
  
  // send the email
  if(user &amp;&amp; user.email) {
      if(message.template &amp;&amp; message.subject &amp;&amp; message.pageData ) {
          var path_email = path.join(__dirname, 'templates', message.template)
          var template_email = new EmailTemplate(path_email)
  
          message.pageData.base_url = "https://example.com";
  
          console.log(message.pageData);
  
          template_email.render(message.pageData, function (err, result) {
              if (err) {
                  return console.error(err)
              }
              // actual sending of the email
              transporter.sendMail({
                  from: "info@example.com", // sender address
                  to: user.email, // list of receivers
                  subject: message.subject, // Subject line
                  html: result.html,
                  text: result.text.replace(/&lt;\/p&gt;/g,'\n').replace(/&lt;\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'"&gt;\s]+))?)+\s*|\s*)\/?&gt;/g,'')
              }, function (err, responseStatus) {
                  if (err) {
                      return console.error(err)
                  }
                  console.log("email send to " + user.email);
              })
          })
      } else {
          console.log("Error no template, subject or pageData", null);
      }
  } else {
      console.log("Error no user or email", null);
  }
  
  // create a hello world nodejs server to wait until email has send...
  var express = require('express');
  var app = express();
  app.listen(3000, function () {
    console.log('Example app listening on port 3000. Waiting for email to be send...');
  });
  </code></pre>
  
  <ol start="4">
  <li>Open the raw data of the received email and find no dkim anything.
  I have used apple mails feature to check the email, but there was no dkim records hash or whatever in the email. mail-tester.com does not give me a positive result on dkim as well. It just sais: "Your message is not signed with DKIM"</li>
  </ol>
  
  <p>I appreciate any help that solves this issue :-) Thank you</p>