我一直在這里尋找答案,但到目前為止沒有任何效果。基本上我有一個txt檔案,我想使用Nodemailer發送它,就是這樣。但我不斷收到此錯誤:錯誤:ENOENT:沒有這樣的檔案或目錄,打開“data.txt”該檔案存在并且直接與負責發送它的 .js 檔案相同,因此路徑正確。我檢查了我的 .json 檔案,我需要的所有包都存在。這一切都在本地作業,所以我很難理解出了什么問題。
代碼如下:
let nodemailer = require('nodemailer');
let transporter = nodemailer.createTransport({
service: 'outlook',
auth:{
user: 'myEmail',
pass: process.env.MAIL_PASS
}
});
data.map(coin => {
if(coin.symbol !== 'BNB'){
top80.push(coin.symbol.toUpperCase());
cleanedOrders[coin.symbol.toUpperCase()] = [];
}
})
let mailoptions = {
from: 'senderEmail',
to: 'toEmail',
subject: 'Report',
text: 'Find this months report attached.',
attachments: [
{
filename: 'report.txt',
path: 'data.txt'
}
]
}
function getCoinData(numberOfCoins) {
//should be < -1
if (numberOfCoins > -1) {
//console.log('All coin orders captured');
//email results
transporter.sendMail(mailoptions, (err, info) => {
if(err){
console.log(err);
res.json(`error compiling report: ${err}`);
} else {
res.json(`Report sent.`);
}
});
}
}
uj5u.com熱心網友回復:
由于您提供了相對路徑,因此nodemailer將建立relative到process.cwd. 這process.cwd()是程式的作業目錄或想想您的main.js檔案的位置,您啟動程式的檔案夾!。
假設您有以下檔案夾結構:
main.js
-email
--email.js
--data.txt
如果您使用引數啟動程式,即使檔案被呼叫,main.js該process.cwd()引數也將始終是所在的檔案夾。main.jsemail.js
選項1
- 移動
data.txt到根檔案夾(與所在檔案夾相同main.js),它會找到它。
選項 2
- 提供一個絕對路徑
nodemailer,最好使用全域__dirname
var { join } = requiure('path')
let mailoptions = {
from: 'senderEmail',
to: 'toEmail',
subject: 'Report',
text: 'Find this months report attached.',
attachments: [
{
filename: 'report.txt',
// __dirname is equal to the directory the file is located in!.
path: join(__dirname, 'data.txt')
}
]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/370566.html
