Well, there has been a slight struggle on how to send CSV file as an attachment using Node.js. If you’re wondering how to do that, look no further, we are going to do just that in this post! 😊
好吧,在如何使用Node.js作为附件发送CSV文件方面存在一些困难。 如果您想知道该如何做,请不要再看了,我们将在本文中做到这一点! 😊
We will dwell into using Nodemailer , a popular emailing module that literally makes sending makes “easy as cake”. It is a project that was started back in 2010 and now is a “de facto” solution for emailing that Node.js developers turn to.
我们将介绍使用Nodemailer ,它是一种流行的电子邮件发送模块,实际上使发送变得“轻而易举”。 这是一个始于2010年的项目,现在是Node.js开发人员可以通过电子邮件发送的“事实”解决方案。
Well, there has been a slight struggle on how to send CSV file as an attachment using Node.js. If you’re wondering how to do that, look no further, we are going to do just that in this post! 😊
好吧,在如何使用Node.js作为附件发送CSV文件方面存在一些困难。 如果您想知道该如何做,请不要再看了,我们将在本文中做到这一点! 😊
We will dwell into using Nodemailer , a popular emailing module that literally makes sending makes “easy as cake”. It is a project that was started back in 2010 and now is a “de facto” solution for emailing that Node.js developers turn to.
我们将介绍使用Nodemailer ,它是一种流行的电子邮件发送模块,实际上使发送变得“轻而易举”。 这是一个始于2010年的项目,现在是Node.js开发人员可以通过电子邮件发送的“事实”解决方案。
Let’s get started by creating a new npm repository using your preferred terminal.
让我们开始使用您的首选终端创建一个新的npm存储库。
npm init --yNote: That --y flag is for people that don’t have much time 😛.
注意:-- --y标志适用于时间不多的人😛。
We will install the following dependencies in our repository:
我们将在存储库中安装以下依赖项:
Nodemailer Nodemailer json2csv json2csvYet again, let’s use your preferred terminal and the npm package manager to install the dependencies:
再一次,让我们使用您喜欢的终端和npm软件包管理器来安装依赖项:
npm i --save nodemailer json2csvIf you’re successful, your package.json should list the dependencies with the “latest” version number against each of them.
如果成功,您的package.json应该列出依赖关系,并在每个依赖关系上列出“最新”版本号。
json2csv module is used to convert JSON objects to CSV format in an easy and neat manner.
json2csv模块用于以一种简单整洁的方式将JSON对象转换为CSV格式。
Now let’s include the Nodemailer dependency into our code and initialize a transporter. Note that I’m using the ES6 syntax for the code.
现在让我们将Nodemailer依赖项包含到我们的代码中,并初始化一个传输器。 请注意,我在代码中使用了ES6语法。
const nodemailer = require("nodemailer");const transporter = nodemailer.createTransport({ host: "smtp.ethereal.email", port: 587, auth: { user: "<email>", pass: "<password>", },});In the above example, we have Ethereal email service. Ethereal is a fake SMTP service, mostly aimed at Nodemailer users (but not limited to). It’s a completely free anti-transactional email service where messages never get delivered.
在上面的示例中,我们有Ethereal电子邮件服务。 Ethereal是一种伪造的SMTP服务,主要针对Nodemailer用户(但不仅限于此)。 这是一个完全免费的反事务电子邮件服务,永远不会传递消息。
Now, let’s initialize some random JSON data.
现在,让我们初始化一些随机的JSON数据。
const nodemailer = require("nodemailer");const transporter = nodemailer.createTransport({ host: "smtp.ethereal.email", port: 587, auth: { user: "<email>", pass: "<password>", },});//some dataconst data = [{relation:"father",name:"Anakin Skywalker"},{relation:"son",name:"Luke Skywalker"}];Let’s convert the data into CSV using the light but awesome csvtojson library
让我们使用轻巧的csvtojson库将数据转换为CSV
const nodemailer = require("nodemailer");const transporter = nodemailer.createTransport({ host: "smtp.ethereal.email", port: 587, auth: { user: "<email>", pass: "<password>", },});//some dataconst data = [{relation:"father",name:"Anakin Skywalker"},{relation:"son",name:"Luke Skywalker"},...]; //conver the data to CSV with the column namesconst csv = parse(data, ["relation","name"]);Now, without further ado, let’s send this data as a CSV attachment leveraging the Nodemailer transporter mail options. Note that the data is sent using the “attachments” key on the mail configuration.
现在,不用多说,让我们利用Nodemailer传输程序邮件选项将这些数据作为CSV附件发送。 请注意,数据是使用邮件配置上的“附件”键发送的。
transporter.sendMail( { from: "darth.vader@sithmail.com", to: "luke.skywalker@jedimail.com", subject: "You need to know the truth", text: "Ola! Please check the attachment for a surprise 😊", html: "<b>Ola! Please check the attachment for a surprise! 😊</b>", // here is the magic attachments: [ { filename: "file.csv", content: csv, }, ], }, (err, info) => { if (err) { console.log("Error occurred. " + err.message); return process.exit(1); } console.log("Message sent: %s", info.messageId); // Preview only available when sending through an Ethereal account console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); });Let’s complete our example
让我们完成示例
const nodemailer = require("nodemailer");const transporter = nodemailer.createTransport({ host: "smtp.ethereal.email", port: 587, auth: { user: "<email>", pass: "<password>", },});//some dataconst data = [ { relation: "father", name: "Anakin Skywalker" }, { relation: "son", name: "Luke Skywalker" },];//conver the data to CSV with the column namesconst csv = parse(data, ["relation", "name"]);transporter.sendMail( { from: "darth.vader@sithmail.com", to: "luke.skywalker@jedimail.com", subject: "You need to know the truth", text: "Ola! Please check the attachment for a surprise 😊", html: "<b>Ola! Please check the attachment for a surprise! 😊</b>", //here is the magic attachments: [ { filename: "file.csv", content: csv, }, ], }, (err, info) => { if (err) { console.log("Error occurred. " + err.message); return process.exit(1); } console.log("Message sent: %s", info.messageId); // Preview only available when sending through an Ethereal account console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); });You should now receive an email on the receiver mailbox with the attached CSV file with the appropriate data! Boom! you’re done!
现在,您应该在接收器邮箱上收到一封电子邮件,其中包含带有适当数据的CSV文件! 繁荣! 你完成了!
Happy Grizzly 🐻 Coding!
快乐灰熊🐻编码!
Read more at https://grizzlybit.info
在https:// grizzlybit上了解更多信息。 信息
翻译自: https://medium.com/@zubair1024/how-to-send-csv-files-with-nodemailer-51f2026330f1
相关资源:微信小程序源码-合集6.rar