2

I'm a beginner at node.js, but i want to know: is there any way to send folders with nodemailer?

I found a lot of attachment types like: path: 'F:asdasd\asd.rar' But i don't want only one file, i want the entire folder. Thanks for the help!

Speedy
  • 23
  • 4
  • It is not different from an email client app. Can you send folders with gmail? nope. You can attach all the files in the directory but not the directory itself. – ISAE May 13 '21 at 22:48
  • You could also compress the folder into a zip archive and send that. – cbr May 13 '21 at 22:49
  • Yea you have two options! You zip it! Or you attach the files one by one! You are programing so you can do multiple attachements! You read the files from a dir with fs.readdir()! Then you get the files and send it! You may like to use glob package too! to get the files more easily with glob pattern ! Including for recursive! But if you like to keep the structure and all! Zipping is the best way! And as ISAE explained that's what we do when we send email manually! For zipping you can use zlib package as per here https://medium.com/@harrietty/zipping-and-unzipping-files-with-nodejs-375d2750c5e4 – Mohamed Allal May 13 '21 at 22:58
  • Thanks for the help! I choosed the second option so would you like to give me an example for this fs.readdir() command? – Speedy May 13 '21 at 23:03
  • Can somebody help me with this? – Speedy May 13 '21 at 23:21
  • check the code example links that i included in my answer! I can add a complete example later – Mohamed Allal May 14 '21 at 00:47

1 Answers1

1

As per any manual client! You can't attach a folder directly! However you can attach multiple files! Or zip the folder and attach it!

Zipping can be the right best choice! However we can have two choices here! Zipping or attaching multiple files! Because we are programming we can do any of that easily! And in the way that we want!

Another choice would be to use a cloud storage and just add the link per the email message!

Before tackling this! It's nice to talk about how an email is send! And also the size limit!

NOTE: the answer go in two parts! How emails works! and How to attach a folder!

How emails works

Protocols

Protocols SMTP, EMAP AND POP are used between an email server (One of google servers for the gmail service)! And an email client (gmail web app client, thunderbird)!

You can check each protocol here

In resume:

SMTP

Simple Mail Transfer Protocol (SMTP) is the standard protocol for sending emails across the 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.

It's intersting to compare IMAP to POP3 ! And understand there flow better! Here a good article that do that and detailed explanation!

Now the protocol dectate no limit size! Also what about attachment! How the attachment are managed and send and handled by clients!

Attachment

I can refer to this stackoverflow link

In resume:

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).

Check the link to see an example!

Now that's how attachment are send! Part of the body! and with (MIME)! Just like http do with forms content! There is however no size limit restriction at the protocol levels!

Any size limit is set by the email server!

We can see different size limits as per the links bellow:

You can check the size array ! Gmail for example it's 25MB ! Outlook/Office 365 20 MB (10 MB for Exchange accounts), with up to 150 MB in Office 365

For attachement and all! The clients Know how to read and handle them! By text processing! And the handling of the MIME format!

What one can do if the limit is a problem

Use cloud storage services (like google drive)! And include a link!

One can use google drive api! Or drop box too! Create a file there first! Get the link! Then attach the link only part of the message!

Attaching a folder

Zipping

You can use the archiver module as per here

Need to ZIP an entire directory using Node.js

(there is a code example)

After done zipping one attach the file! That may be the best way!

Attaching multiple files

One should use fs.readdir() from the fs module

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

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

Here with code examples

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

Including recursion example:

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

One too can use the glob module ! And use glob notation! If glob's make it easier! (one may not need to do that and add more dependencies)

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

Cloud storages

One need to use the specific api client! And follow the api instructions! For example google drive:

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

Upload the files there! Then getting a link to it!

Which one include in the email message!

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97