Google Apps Script to Download All Gmail Mail Attachments PDF & Images to Google Drive Full Project
Dec 21, 2025
Buy the full source code of application here
https://buy.stripe.com/cN2bM2bZveFaarKcaZ
Show More Show Less View Video Transcript
0:00
Uh hello friends. Today in this tutorial
0:02
we will be building a very interesting
0:03
project inside which we will be
0:05
downloading all our uh Gmail attachments
0:08
which includes PDF, images, videos, all
0:11
that stuff inside our Google Drive. So
0:14
we will be implementing this inside
0:17
Google appcript. Google appcript is a
0:19
special technology brought to you by
0:20
Google which lets you build app for
0:22
their uh suits and products. So if you
0:25
just search on Google Google appcript
0:27
this is their special special scripting
0:29
language which is developed by Google
0:32
for lightweight application development
0:34
in the Google workspace platform. So in
0:36
this we will be downloading all these
0:38
attachment automatically inside our
0:41
Google drive. So inside a folder. So
0:43
first of all let me just show you if you
0:45
just open your Gmail account here. So
0:48
gmail.com if you open it let me just uh
0:50
tell you the logic that we will be using
0:52
here. So if you want to search for
0:54
attachment here you will you will have
0:56
this query has colon then attachment. So
1:00
if you just type this query inside your
1:02
search bar then it will show you all
1:04
your attachments inside your uh Gmail
1:06
account. So if you want to automatically
1:09
download all these attachments inside
1:11
your Google drive into a specific folder
1:13
you will write the script. So all the
1:16
source code regarding this uh script
1:18
guys I have given the link in the video
1:20
description. This is my blog post. So if
1:23
you want the full source code of the
1:25
script, you can go to the video
1:27
description. Just follow the
1:28
instructions and just uh copy paste this
1:30
code. I will write this code line by
1:32
line. I will explain you the meaning of
1:34
this code. So just go to the video
1:36
description if you want the full source
1:37
code. So now let me just uh give you the
1:40
live demo. So inside of a Google drive,
1:43
let me just make a new folder here. So
1:45
new folder. We will make this as images.
1:48
The name of the folder create it. So now
1:52
this is my brand new script guys. So I
1:54
need to paste the folder ID. So in order
1:56
to get the folder ID guys what what I
1:59
need to do is that we need to select
2:00
this
2:02
folder and then we need to click get
2:05
link. We need to get this link here. So
2:08
if I just copy this and uh this is the
2:11
folder ID. Let me just delete this
2:15
before question mark. Just delete all
2:16
this stuff. After this slash symbol this
2:19
is a folder ID. So you need to simply
2:21
copy this folder ID and simply you need
2:23
to paste it. So this folder ID will be
2:25
different for you if you just create the
2:27
folder. So simply paste it. Now there is
2:30
the option here of run here. First of
2:31
all save your project. So it is saving
2:34
it. Now you can run this project. Click
2:36
the run option
2:38
and uh now it will run your project. So
2:41
for the very first time if the project
2:44
runs it will ask for the permissions.
2:45
You need to grant the permission here.
2:47
So you will see authorization required.
2:49
So click on review permissions and you
2:52
need to select your Google account. So
2:53
select your Google account. Click on
2:55
advanced and go to this unsave. So you
2:59
need to grant permissions first of all.
3:01
So you can see it is saying that this
3:03
will allow read compose and permanently
3:05
delete all your mail from Gmail. So
3:07
click on allow. So you need to grant
3:09
permissions. So now it will execute it.
3:11
Execution started and it is saying that
3:14
drive is not defined. Let me just see
3:16
guys what is the problem.
3:19
Uh
3:20
I think guys we need to grant
3:22
permissions as well inside services. If
3:24
you are running it for the very first
3:25
time, click on service. Add a service
3:28
and you need to select this API drive
3:31
API. You need to add this. Click on add.
3:33
First of all, select this API. Click
3:35
add. So it is adding this service in the
3:38
background. So if you are building it
3:40
for the very first time, you need to add
3:42
this service explicitly. Now you can see
3:44
drive API is added. So once again if I
3:47
click run execution start started. It is
3:50
saying that drive files.insert is not a
3:52
function. Let me just see guys. Oh sorry
3:55
this needs to be uh not capital I. This
3:57
needs to be small I. So that is the
4:00
problem guys. So just change it to small
4:02
I once again if I run this.
4:06
So it is saying review permissions.
4:08
Click
4:10
grant the permissions here.
4:13
uh click allow. So I have shown you the
4:16
step-by-step process guys how to do
4:19
this. So you will see execution started.
4:22
So now guys you can see automatically
4:25
all the images all the attachments are
4:27
being downloaded here. You will see here
4:29
all the images have been downloaded. So
4:31
it is downloading here automatically.
4:33
This this is all the attachments which
4:35
are automatically downloaded here. So
4:37
you will see so it will take hardly uh 5
4:41
to 10 seconds depending upon your
4:42
images. So it has successfully
4:45
downloaded it. It contains PDF documents
4:47
as well. You can see PDF document
4:49
images. So if you contain videos as
4:51
well, it will uh automatically download.
4:54
You can see execution completed. So it
4:56
has downloaded all the attachments.
5:00
So I think it is downloaded 1 2 3 4 5 6
5:03
7 8 9 10 11 12 13. So 13 attachments
5:07
were there and it has downloaded all of
5:09
them. So it's a great script that you
5:11
can write here if you want to import all
5:13
your Gmail attachments to your Google
5:15
Drive folder. You can do this very
5:17
easily inside Google appcript. Now let
5:19
me just show you how to do the do this.
5:22
So you need to be having a Google
5:24
account for this and go to this URL
5:25
which is script.google.com.
5:27
So this is their advanced editor to
5:29
build applications. So click on new
5:31
project
5:33
and automatically your function will be
5:36
started. So now delete all this stuff
5:38
and make your function which will be
5:40
save
5:42
Gmail
5:45
to drive. So this you can give any name
5:48
of your function here
5:50
parenthesis. Now first of all add the
5:52
service here guys. Add your Google drive
5:55
service once again which is very easy.
5:57
Just select your API
5:59
drive API. Click add. So it is adding
6:02
the service. So make sure that you add
6:03
this service because once you will get
6:06
error if you don't add it. So first of
6:08
all guys what we need to do is that we
6:09
need to declare some variables which is
6:11
will be folder ID and search query. So
6:15
folder ID will be the ID which you will
6:17
create inside your Google Drive. So let
6:20
me just create it. Uh let me delete this
6:23
folder. remove it and once again create
6:26
a new brand new folder which will be
6:29
drive
6:30
sorry Gmail
6:32
images
6:34
and if I create this folder so you will
6:36
see the folder is created if I select
6:39
this you need to simply get the link of
6:41
the folder just copy this link and paste
6:45
it here and you only need to get the ID
6:48
so before the question mark you will
6:51
this is the folder ID here so simply
6:54
Ctrl C copy it and paste it this folder
6:58
ID that's it and the search query I have
7:01
already told you has colon attachment so
7:04
this will get all the attachment inside
7:06
your Gmail so this is kind of a filter
7:08
inside Gmail now guys what we need to do
7:11
is that we need to get the threads so
7:13
inside Gmail API there is a threads kind
7:15
of a parameter so we will use Gmail app
7:19
dot search there is a method out there
7:21
inside this what we need to pass here.
7:23
We need to pass the search query
7:26
and then we need to pass the number 0 to
7:28
10. So it will in one batch it will
7:32
download 10 attachments. So this is
7:34
meant by it in single execution it will
7:36
only download 10 attachments. You can
7:38
just change this number accordingly and
7:40
after that guys we will uh just loop it.
7:44
We will use the for each loop for each
7:47
for each thread for each execution what
7:49
we want we need to extract the messages.
7:53
So again we will make another variable
7:56
which will be messages. So there is a
7:58
method here thread dot get messages. So
8:02
it will get all the messages for a
8:03
single thread. So once again what we
8:06
need we need to loop it. Again we will
8:08
use messages for each and for each
8:11
message we need to get the attachments.
8:14
So for getting the attachments guys
8:16
there is a method out there which will
8:18
be uh get attachment. So once again we
8:21
will declare a variable which will be
8:24
attachments
8:26
and uh message dot get attachment. So
8:30
this is a method guys and here we need
8:32
to pass some arguments. This is a
8:34
object. So we need to pass this
8:37
parameter which is include inline
8:39
images. So this needs to be false. You
8:42
can make it true as well. And then we
8:45
there's a second option which will be
8:47
include attachments. This needs to be
8:49
true because we need to download the
8:51
attachments. So after getting the
8:53
attachments guys we once again we need
8:55
to again use the for for each loop for
8:59
iterate for each attachment.
9:03
For each attachment guys, we need to
9:05
insert it to our Google drive. So this
9:07
is very easy guys. There is a logic
9:09
here. So just two lines of code. So this
9:12
will insert the attachment
9:18
to Google Drive folder. So this is very
9:20
easy. So we will use drive dot files
9:24
dotinsert method. And inside this guys
9:27
what we need to do is that we need to uh
9:31
this needs to be
9:37
uh inside this this object takes three
9:40
properties. First is the title. We can
9:42
get the title of the attachment by
9:44
attachment dot get title
9:49
get name sorry get names not title get
9:52
name. So this will be the name of the
9:54
attachment. Then we have the name type
9:56
which is the extension content type. So
9:59
here we can get the content type by
10:01
attachment dot get content type. This is
10:04
a method.
10:06
And uh lastly guys what we need to do is
10:08
that there is a option here which in
10:11
which folder we need to insert it. So
10:14
there's a parents property parents
10:16
inside this this needs to be array and
10:19
inside this object we there is a
10:21
property of id and here we need to pass
10:23
the folder ID. So whichever folder you
10:25
need to insert it, you need to pass the
10:27
folder ID. That's it. And after put it,
10:30
put a comma here. This takes two
10:32
arguments and then it takes the actual
10:34
blob object. So we can we need to
10:36
convert our attachment to blob. We can
10:38
do this by attachment dotcopy blob. So
10:41
this will return the blob actual blob
10:44
that needs to be inserted. That's it
10:46
guys. Our script is completed. I have
10:49
explained you each line of code. If you
10:51
have any sort of doubt, you can repeat
10:52
this video and uh just understand the
10:55
code.
10:57
And now to first of all save the
10:59
project. And now we can run this
11:01
project. If I run it for the very first
11:05
time again it will ask for the
11:06
permissions. So we need to grant the
11:08
permissions. So it will take some time.
11:11
So this is our bl brand new folder.
11:14
Click on review permissions. Click the
11:16
Google account
11:18
and uh click advanced. [snorts]
11:21
And you need to click the allow button
11:23
to allow the permissions. So you can see
11:26
execution started. So now you will see
11:29
our attachments will be automatically
11:31
downloaded here. [snorts] You can see it
11:33
is execution started. Now you can see
11:35
all the attachments are being downloaded
11:37
here. So these are the attachments which
11:40
are downloaded. So this is very simple
11:43
guys. How simple is that? How to
11:44
download all attachments of Gmail
11:46
account to your Google Drive folder. It
11:48
is as simple as that using Google
11:50
appcript. So we have I have shown you
11:53
the complete example. If you want to
11:55
download all the source code, you can go
11:57
to the video description. I have given
12:00
the source code in the video
12:01
description. This is my blog post. So
12:03
just download all the source code and
12:05
please like the video guys. Please
12:06
comment comment on this also. And I will
12:09
be seeing you in the next
