Sendgrid-nodejs: λ©”μΌμ˜ 첨뢀 νŒŒμΌμ—λŠ” 0λ°”μ΄νŠΈκ°€ μžˆμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2019λ…„ 01μ›” 18일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: sendgrid/sendgrid-nodejs

μ•ˆλ…•ν•˜μ„Έμš”,

@sendgrid/mail - "^6.3.1" 및 λ…Έλ“œ - 10.14.1을 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄ λ¬Έμ œλŠ” Excel 첨뢀 파일의 λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ 단일 ν–‰ 데이터λ₯Ό 보낼 λ•Œ 첨뢀 νŒŒμΌμ΄μžˆλŠ” 메일을받을 수 μžˆμ§€λ§Œ Excel의 λ°μ΄ν„°λ² μ΄μŠ€ λ°μ΄ν„°μ—μ„œ μ—¬λŸ¬ ν–‰ 데이터λ₯Ό 첨뢀 파일둜 보낼 λ•Œ 0 λ°”μ΄νŠΈμ˜ 첨뢀 νŒŒμΌμ„ μˆ˜μ‹ ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. νŒŒμΌμ— 이미지도 ν¬ν•¨μ‹œμΌ°μŠ΅λ‹ˆλ‹€. Exceljs λͺ¨λ“ˆμ„ 톡해 μƒμ„±ν•œ λ™μΌν•œ νŒŒμΌμ„ μ²¨λΆ€ν•˜λ €κ³  ν•©λ‹ˆλ‹€. IIλŠ” 많이 μ‹œλ„ν–ˆμ§€λ§Œ μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€ .. λΈŒλΌμš°μ €μ—μ„œ 이 μ½”λ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ λ™μΌν•œ Excel νŒŒμΌμ„ 생성할 수 μžˆλŠ” κ³³μ—μ„œ
"
res.setHeader("μ½˜ν…μΈ  μœ ν˜•", "μ‘μš© ν”„λ‘œκ·Έλž¨/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
res.setHeader("λ‚΄μš© 처리", "첨뢀 파일 이름='" + excelFile.excelOffFile + "'");
workbookFinal.xlsx.write(res).then(() => {
res.end(); });μ—μŠ€
"
메일을 보내기 μœ„ν•œ λ‚΄ μ½”λ“œλŠ” -
sgMail.send({
λ°›λŠ” μ‚¬λžŒ: config.get("mail.defaultAdd"),
보낸 μ‚¬λžŒ: fromMailμΆ”κ°€,
제λͺ©: "μ—‘μ…€ 첨뢀 μ£Όλ¬Έ μ†Œν”„νŠΈμ›¨μ–΄λ₯Ό 톡해 보내기",
HTML: <h1>Test Mail</h1> ,
첨뢀 파일: [
{
λ‚΄μš©: fs.readFileSync(excelFile.excelOffFilePath, {인코딩:"base64"}),
파일 이름: excelFile.excelOffFile,
μœ ν˜•: "μ‘μš© ν”„λ‘œκ·Έλž¨/vnd.ms-excel"
},
{
λ‚΄μš©: fs.readFileSync(excelFile.excelFactFilePath, { 인코딩: "base64" }),
파일 이름: ExcelFile.excelFactFile,
μœ ν˜•: "μ‘μš© ν”„λ‘œκ·Έλž¨/vnd.ms-excel"
},
{
λ‚΄μš©: fs.readFileSync(excelFile.excelBrFilePath, { 인코딩: "base64" }),
파일 이름: ExcelFile.excelBrFile,
μœ ν˜•: "μ‘μš© ν”„λ‘œκ·Έλž¨/vnd.ms-excel"
},
{
λ‚΄μš©: fs.readFileSync(excelFile.excelSuFilePath, { 인코딩: "base64" }),
파일 이름: excelFile.excelSuFile,
μœ ν˜•: "μ‘μš© ν”„λ‘œκ·Έλž¨/vnd.ms-excel"
}
]
})

메일 μŠ€ν¬λ¦°μƒ· -
image

같은 문제λ₯Ό ν•΄κ²°ν•˜λŠ” 방법을 λ„μ™€μ£Όμ„Έμš”.

unknown or a waiting for feedback non-library issue

λͺ¨λ“  3 λŒ“κΈ€

μ•ˆλ…•ν•˜μ„Έμš” @prasoojalan μž…λ‹ˆλ‹€ .

파일 κ²½λ‘œλ‚˜ 파일 인코딩에 λ¬Έμ œκ°€ μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 파일이 μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜κΈ° μœ„ν•΄ μ½”λ“œμ—μ„œ excelFile.* 값을 λ‹€μ‹œ ν™•μΈν•©λ‹ˆλ‹€. 그런 λ‹€μŒ 파일 μžμ²΄κ°€ base64둜 μ˜¬λ°”λ₯΄κ²Œ μΈμ½”λ”©λ˜μ—ˆλŠ”μ§€ ν™•μΈν•©λ‹ˆλ‹€. μ–΄μ©Œλ©΄ 이 도ꡬ κ°€ ν›„μžλ₯Ό λ„μšΈ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ§ˆμŒμ„ λ‹΄μ•„,

μ—˜λ¨Έ

μ•ˆλ…•ν•˜μ„Έμš” @ μ—˜λ¨Έ

파일 생성 및 전솑을 μœ„ν•œ λ‚΄ 전체 μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
ν•˜λ‚˜μ˜ 첨뢀 루트 경둜λ₯Ό μ œκ³΅ν•˜λ €κ³  μ‹œλ„ν–ˆμ§€λ§Œ μž‘λ™ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. μƒμ„±λœ λ™μΌν•œ μ—‘μ…€ 파일이 λΈŒλΌμš°μ €μ— μ„±κ³΅μ μœΌλ‘œ λ‹€μš΄λ‘œλ“œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.... μ–΄λ–€ μ½”λ“œκ°€ λˆ„λ½λ˜μ—ˆκ±°λ‚˜ κ°œλ…μ„ μ œλŒ€λ‘œ 이해할 수 μ—†μŠ΅λ‹ˆλ‹€... 도와주싀 수 μžˆλ‚˜μš”?

"C"λ₯Ό 맀개 λ³€μˆ˜λ‘œ μ‚¬μš©ν•˜λŠ” createExcelFile ν•¨μˆ˜μ—μ„œ λ‹€λ₯Έ κ²½μš°μ—λŠ” μ—†λŠ” 톡합 λ¬Έμ„œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

exports.getExcelFile = (req, res, next) => {
   const excelFile = {};

   Order.findById(req.params.id).then(order => {
      excelFile.excelOffFile = "Off_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelOffFilePath = path.join("data", "excel", excelFile.excelOffFile);
      excelFile.excelFactFile = "Fact_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelFactFilePath = path.join("data", "excel", excelFile.excelFactFile);
      excelFile.excelBrFile = "Br_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelBrFilePath = path.join("data", "excel", excelFile.excelBrFile);
      excelFile.excelSuFile = "Su_" + order.factory + "_" + order._id + ".xls";
      excelFile.excelSuFilePath = path.join("data", "excel", excelFile.excelSuFile);

      let workbookFinal = createExcelFile(order, "C", excelFile.excelOffFilePath);

      createExcelFile(order, "F", excelFile.excelFactFilePath);
      createExcelFile(order, "B", excelFile.excelBrFilePath);
      createExcelFile(order, "S", excelFile.excelSuFilePath);
sgMail.send({
         to: config.get("mail.defaultAdd"),
         from: defaultMailId,
         subject: "Order with excel attachment send through software ",
         html: `<h1>
         The mail has been sent as trial through program for testing excel file attachement  .
         </h1>
         <h3>
            Office file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelOffFile })}</br>
            Fact file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelFactFile })}</br>
            Br file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelBrFile })}</br>
            Su file - ${url.format({ protocol: req.protocol, host: req.get("host"), pathname: "orders/genExcelFile/" + excelFile.excelSuFile })}</br>
         </h3>
         `,
         attachments: [
            {
               content: fs.readFileSync(path.join(rootDir.rootDir, excelFile.excelOffFilePath), { encoding: "base64" }),
               filename: excelFile.excelOffFile,
            },
            {
               content: fs.readFileSync(excelFile.excelFactFilePath, { encoding: "base64" }),
               filename: excelFile.excelFactFile,
            },
            {
               content: fs.readFileSync(excelFile.excelBrFilePath, { encoding: "base64" }),
               filename: excelFile.excelBrFile,
            },
            {
               content: fs.readFileSync(excelFile.excelSuFilePath, { encoding: "base64" }),
               filename: excelFile.excelSuFile,
            }
         ]
      }).then(result => {
         console.log("mail sent successfully...");
      }).catch(err => console.log(err));

      res.setHeader("Content-Type", "application/vnd.ms-excel");
      res.setHeader("Content-Disposition", "attachment;filename='" + excelFile.excelOffFile + "'");
      workbookFinal.xlsx.write(res).then(() => {
         res.end();
      });

}).catch(err => console.log(err));
};

μ•ˆλ…•ν•˜μ„Έμš” @Elmer

λ‹Ήμ‹ μ˜ μ œμ•ˆμ— κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€. μ €λ₯Ό 많이 λ‹Ήν™©ν•˜κ²Œ ν–ˆλ˜ μ½”λ“œμ˜ 흐름을 μ΄ν•΄ν•˜λŠ” 데 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
λ‚˜λŠ” λ‚΄ μ‹€μˆ˜λ₯Ό νŒŒμ•…ν•˜κ³  ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. λ‚΄ μ½”λ“œλŠ” 메일을 보낼 λ•Œ 기둝이 μ—†λŠ” Excel νŒŒμΌμ„ μƒμ„±ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ ν•¨μˆ˜μ—μ„œ 메일 μ½”λ“œλ₯Ό μ œκ±°ν•˜κ³  λ³„λ„μ˜ ν•¨μˆ˜λ₯Ό λ§Œλ“  λ‹€μŒ getExcelFile ν•¨μˆ˜ λ‹€μŒμ— ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν–ˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ 파일이 μƒμ„±λ˜κ³  파일이 μ˜¬λ°”λ₯΄κ²Œ μ²¨λΆ€λ˜μ—ˆμŒμ„ 확인할 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

μΆ”κ°€ μ§ˆλ¬Έμ„ μ œκΈ°ν•˜κΈ° 전에 제 μ½”λ“œμ— 집쀑할 수 μžˆλ„λ‘ λ„μ™€μ£Όμ…”μ„œ λ‹€μ‹œ ν•œ 번 μ§„μ‹¬μœΌλ‘œ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.

λ§ˆμŒμ„ λ‹΄μ•„,

ν”„λΌμˆœ 쿠마λ₯΄ μž˜λž€

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰

κ΄€λ ¨ 문제

amlcodes picture amlcodes  Β·  4μ½”λ©˜νŠΈ

danielflippance picture danielflippance  Β·  4μ½”λ©˜νŠΈ

murphman300 picture murphman300  Β·  4μ½”λ©˜νŠΈ

thinkingserious picture thinkingserious  Β·  4μ½”λ©˜νŠΈ

TobiahRex picture TobiahRex  Β·  3μ½”λ©˜νŠΈ