我正在閱讀關於該Nodemailer是using SMTP or sendmail or Amazon SES,我也發現更多smtp servers所以我假設,除了我的nodemailer應用程序,我必須使其中一個SMTP服務器運行,並使我的應用程序連接到它,但有人告訴我,Nodemailer可以本身就是一個自治的(或者也許是一個standlaone)的SMTP服務器「Nodemailer自動發送郵件(不需要通過服務器/ SMTP服務)」。Nodemailer可以作爲獨立的SMTP服務器嗎?
所以我再次讀取其的文檔,我發現Nodemailer傳輸對象使用nodemailer-SMTP傳輸模塊作爲默認
var transporter = nodemailer.createTransport(smtpTransport(options)) (1)
(通過使用smtpTransport作爲默認)或
var transporter = nodemailer.createTransport(options) (2)
(我認爲這意味着(1)相當於(2))
所以我在想,也許它是nodemailer-smtp-transport使Nodemaile R設定爲一個獨立的SMTP服務器,然後我想這代碼定義Nodemailer轉運,並使其發送電子郵件和sendmail:(我們稱之爲snippet1)
var transporter = nodemailer.createTransport(smtpTransport({
host: 'localhost',
port: 465,
auth: {
user: 'username',
pass: 'pass'
}
}));
// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'Ahmed Feki ✔ <[email protected]>', // sender address
to: '[email protected], [email protected]', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world from nodemailer test ✔</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log('#### sendMail error : ' + error);
}
console.log('#### Message sent: ' + info.response);
});
,當我正在運行的應用程序我得到這個錯誤:
sendMail error : Error: connect ECONNREFUSED 127.0.0.1:25
所以我決定使用此代碼使用SMTP服務器在同一應用程序:
var SMTPServer = require('smtp-server').SMTPServer;
var server = new SMTPServer({
onConnect: function(session, callback){
if(session.remoteAddress === '127.0.0.1'){
return callback(new Error('No connections from localhost allowed'));
}
return callback(); // Accept the connection
},
onError:function(err){
console.log('Error onError %s', err.message);
}
});
/*
onError wasn't mentioned on the documentation i just wrote it myself
as to replace the error on listener :
server.on('error', function(err){
console.log('Error %s', err.message);
});
and i don't know if it would work or not.. i actually kept both of them and just modified the log for i can know from where it would come.
*/
server.listen(465, onConnectCb);
和onConnectCb定義中(在上面的最後一行威騰)我寫的所有snippet1的代碼和結果哪裏像現在好多了,我得到了以下日誌:
[2015-11-02 15:33:11] INFO: Connection from ::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 220 Fekis-PC ESMTP
[2015-11-02 15:33:11] DEBUG: C: EHLO [127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 250-OK: Nice to meet you [::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] C: STARTTLS
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] S: 220 Ready to start TLS
[2015-11-02 15:33:11] INFO: [mhJFwLqR43aX] Connection upgraded to TLS
#### sendMail error : Error: self signed certificate
[2015-11-02 15:33:11] INFO: Connection closed to [::ffff:127.0.0.1]
這是好太多即使沒有發送的電子郵件中仍存在上有一個錯誤
'self signed certificate'
,我想這是因爲我還沒有設置密鑰和證書作爲SMTPSERVER對象的選項(如我還是不要不知道如何得到它們)。
var server = new SMTPServer({
/*secure: true,
key: fs.readFileSync('private.key'),
cert: fs.readFileSync('server.crt')*/
});
所以我問這個問題來得到一個確認,Nodemailer不能是一個獨立的SMTP服務器。