有没有办法用nodemailer发送文件夹?

Is there any way to send folders with nodemailer?

我是 node.js 的初学者,但我想知道:有没有什么方法可以用 nodemailer 发送文件夹?

我发现了很多附件类型,例如:路径:'F:asdasd\asd.rar' 但我不想要一个文件,我想要整个文件夹。 感谢您的帮助!

按照任何手动客户端!您不能直接附加文件夹!但是您可以附加多个文件!或者压缩文件夹并附加它!

压缩可能是正确的最佳选择!但是我们可以在这里有两个选择! 压缩或附加多个文件!因为我们正在编程,所以我们可以轻松地完成任何事情!并以我们想要的方式!

另一种选择是使用云存储,只需在每个电子邮件中添加 link!

在解决这个问题之前!很高兴谈论如何发送电子邮件!还有大小限制!

注意:答案分为两部分! 电子邮件的工作原理!和 如何附加文件夹!

电子邮件的工作原理

协议

协议 SMTP、EMAP 和 POP 在电子邮件服务器(gmail 服务的 google 服务器之一)之间使用!还有一个电子邮件客户端(gmail 网络应用程序客户端,thunderbird)!

您可以检查每个协议here

简历中:

SMTP

简单邮件传输协议 (SMTP) 是通过 Internet 发送电子邮件的标准协议。

IMAP

The Internet Message Access Protocol (IMAP) is a mail protocol used for accessing email on a remote web server from a local client. IMAP allow multi clients simultanous access!

POP3

Post Office Protocol version 3 (POP3) is a standard mail protocol used to receive emails from a remote server to a local email client. POP3 allows you to download email messages on your local computer and read them even when you are offline.

比较 IMAP 和 POP3 很有趣!并更好地理解那里的流动!这里有一个 good article 这样做和详细的解释!

现在协议没有限制大小!还有执着呢!客户端如何管理、发送和处理附件!

附件

我可以参考这个Whosebug link

简历中:

To embed an attachment into an email we usually use Multipurpose Internet Mail Extensions (MIME) described in RFC 2045.

Basically, to send an attachment we divide our email into parts, so the first part is the text and the next part(s) is an attachment(s) or vice versa.

To see the original email structure, we can click "Show original" on an email with attachment(s).

检查 link 以查看示例!

这样附件就可以发送了! body的一部分!和(MIME)!就像 http 处理表单内容一样!但是,在协议级别上 没有大小限制 限制!

任何大小限制都是由电子邮件服务器设置的!

根据下面的 links,我们可以看到不同的大小限制:

你可以检查大小数组!例如 Gmail 是 25MB! Outlook/Office 365 20 MB(Exchange 帐户 10 MB),Office 365 中最多 150 MB

对于附件和所有!客户知道如何阅读和处理它们!通过文字处理!以及MIME格式的处理!

限制有问题怎么办

使用云存储服务(如google驱动器)!并包含一个 link!

可以用google驱动api!或者投递箱!首先在那里创建一个文件!获取link!然后附上邮件的link部分!

附加文件夹

压缩

您可以按照此处使用 archiver 模块

Need to ZIP an entire directory using Node.js

(有代码示例)

完成压缩后附上文件!这可能是最好的方法!

附加多个文件

应该使用 fs 模块中的 fs.readdir()

https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback

https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options

这里有代码示例

https://www.geeksforgeeks.org/node-js-fs-readdir-method/

包括递归示例:

https://whosebug.com/a/45130990/7668448

也可以使用 glob 模块!并使用 glob 符号!如果 glob 让它变得更容易! (可能不需要这样做并添加更多依赖项)

https://www.npmjs.com/package/glob

云存储

需要使用特定的api客户端!并遵循 api 说明!例如google驱动器:

https://developers.google.com/drive/api/v3/quickstart/nodejs

在那里上传文件!然后得到一个link就可以了!

电子邮件中包含哪一个!