0
我很難用node.js和nodemailer設置DKIM簽名。 這裏是我的了: 有規律的自我託管SMTP電子郵件一樣[email protected]上hosteurope.com 與8.4.0使node.js的nodemailer與dkim一起工作smtp
步驟1在inwx.com Node.js的託管DNS設置的域名:生成DKIM密鑰。 我去了https://www.port25.com/dkim-wizard/並輸入我的域名並點擊生成密鑰或其他。然後,我得到了我應該將TXT記錄放入域名註冊商的DNS設置中的內容。 爲了確保它的工作原理,我跑:
dig TXT default._domainkey.example.com
,並得到
; <<>> DiG 9.8.3-P1 <<>> TXT default._domainkey.mydomain.com
;; 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.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
看吧我如何設置DNS設置公鑰:
第2步: 下載私鑰。我把它放在一個文件中,並命名爲dkim.pem。
第3步: 設置一個簡單的node.js程序發送一個dkim簽名的電子郵件。
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: '[email protected]',
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: "[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.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: "[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...');
});
- 打開接收到的電子郵件的原始數據,並沒有發現任何DKIM。 我已經使用蘋果郵件功能來檢查電子郵件,但沒有dkim記錄散列或電子郵件中的任何內容。 mail-tester.com也不會給我在dkim上的正面結果。它只是最高審計機關:「你的消息不與DKIM簽名的」
我意識到,解決了這個問題:-)謝謝