最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - making node.js' nodemailer work with dkim smtp - Stack Overflow

programmeradmin0浏览0评论

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 [email protected] on hosteurope A domain with DNS settings hosted at inwx Node.js 8.4.0

Step 1: Generating the DKIM keys. I went to / 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:

dig TXT default._domainkey.example

and got

; <<>> DiG 9.8.3-P1 <<>> TXT default._domainkey.mydomain
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28927
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;default._domainkey.mydomain. IN    TXT

;; ANSWER SECTION:
default._domainkey.mydomain. 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

Look here how I set the public key in DNS settings:

Step 2: Downloading the private key. I put it in a file and named it dkim.pem.

Step 3: Set up a simple node.js program that sends a dkim signed email.

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'
    , port: 25
    , secure: false // use SSL
    , auth: {
            user: '[email protected]',
            pass: 'mypassword'
    }, tls: {
            rejectUnauthorized: false
    }, dkim: {
            domainName: 'example'
            , 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: "[email protected]"};
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 && user.email) {
    if(message.template && message.subject && message.pageData ) {
        var path_email = path.join(__dirname, 'templates', message.template)
        var template_email = new EmailTemplate(path_email)

        message.pageData.base_url = "";

        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: "[email protected]", // sender address
                to: user.email, // list of receivers
                subject: message.subject, // Subject line
                html: result.html,
                text: result.text.replace(/<\/p>/g,'\n').replace(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)\/?>/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...');
});
  1. 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 does not give me a positive result on dkim as well. It just sais: "Your message is not signed with DKIM"

I appreciate any help that solves this issue :-) Thank you

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 [email protected] on hosteurope. A domain with DNS settings hosted at inwx. Node.js 8.4.0

Step 1: Generating the DKIM keys. I went to https://www.port25./dkim-wizard/ 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:

dig TXT default._domainkey.example.

and got

; <<>> DiG 9.8.3-P1 <<>> TXT default._domainkey.mydomain.
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 28927
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;default._domainkey.mydomain.. IN    TXT

;; ANSWER SECTION:
default._domainkey.mydomain.. 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

Look here how I set the public key in DNS settings:

Step 2: Downloading the private key. I put it in a file and named it dkim.pem.

Step 3: Set up a simple node.js program that sends a dkim signed email.

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.'
    , port: 25
    , secure: false // use SSL
    , auth: {
            user: '[email protected]',
            pass: 'mypassword'
    }, tls: {
            rejectUnauthorized: false
    }, dkim: {
            domainName: 'example.'
            , 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: "[email protected]"};
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 && user.email) {
    if(message.template && message.subject && message.pageData ) {
        var path_email = path.join(__dirname, 'templates', message.template)
        var template_email = new EmailTemplate(path_email)

        message.pageData.base_url = "https://example.";

        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: "[email protected]", // sender address
                to: user.email, // list of receivers
                subject: message.subject, // Subject line
                html: result.html,
                text: result.text.replace(/<\/p>/g,'\n').replace(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[\^'">\s]+))?)+\s*|\s*)\/?>/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...');
});
  1. 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. does not give me a positive result on dkim as well. It just sais: "Your message is not signed with DKIM"

I appreciate any help that solves this issue :-) Thank you

Share Improve this question edited Oct 18, 2017 at 13:18 rcpfuchs asked Oct 17, 2017 at 12:05 rcpfuchsrcpfuchs 8321 gold badge14 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I finally got it. Basically it could not be a more stupid issue: I did not use the latest nodemailer version. I have used 2.4, but the dkim argument requires at least nodemailer 3 - I used nodemailer 4.2.

发布评论

评论列表(0)

  1. 暂无评论