Email Attachments as Zip File using Salesforce Apex
0 views
Jul 24, 2025
Blog Post: https://www.infallibletechie.com/2025/07/email-attachments-as-zip-file-using-salesforce-apex.html
View Video Transcript
0:06
Hello everyone. In this video, we are
0:08
going to see how to attach multiple
0:12
files as a single zip file and send an
0:16
email using Salesforce Apex. My use case
0:20
is very simple. I have files in
0:23
Salesforce and I wanted to send an email
0:28
with uh those files as one zip file. I
0:33
don't want to attach them as single uh
0:37
sorry as multiple files. I my recommend
0:39
is to have only one single attachment
0:42
which will contain all the files in it.
0:46
So I have to zip it and into one file
0:49
and then I have to send it. Let's see
0:51
how to do that or achieve that using
0:53
Salesforce Apex.
0:57
In Salesforce Apex, we have
1:00
compression.zip writer.
1:03
Using this uh uh standard Salesforce uh
1:06
class, we should be able to zip multiple
1:11
uh uh files into one single uh zip file
1:15
and then using single email message in
1:18
Salesforce, we can send an email.
1:23
There are multiple methods in
1:25
compression.zip writer. I have shared
1:29
the link
1:31
uh Salesforce article link using which
1:34
you should be able to see all the
1:36
methods that are available in
1:39
compression.ziper
1:41
class. Here I have hardcoded the content
1:45
document ids just for reference.
1:49
uh but you can definitely dynamically
1:50
fetch it and then you should be able to
1:52
pass the content document ids.
1:58
Okay. So here what I'm doing is I have
2:02
initiated compression.zip writer. Next I
2:06
have added two content document ids to
2:08
this list.
2:10
I am fetching content version using the
2:13
content document ids and then using the
2:15
add entry method I am passing the file
2:19
name, the version data.
2:22
Next I'm creating a blob.
2:25
So using writer.get archive I will get a
2:28
blob.
2:32
So using single email message
2:35
what I'm doing is I am making use of
2:40
this blob as the body
2:44
in the messaging email file attachment.
2:47
I'm getting the blob and then I'm get
2:49
I'm passing that blob as the body and
2:52
then I'm sending an email.
2:55
Let's test this code.
3:01
So this is my developer edition or
3:10
okay. So here we have to replace this
3:14
with our email id.
3:20
So if I execute,
3:24
I should
3:27
receive an email
3:32
with zip attachment
3:35
and uh the body should be
3:39
test email notification in the email and
3:42
the subject of the email should be zip
3:44
testing.
3:46
So this is the email I got it and I
3:49
received that in a zip file. So this
3:53
contains two files in this one's zip
3:56
file.
3:59
So make use of compression.zip writer
4:01
class
4:04
to zip multiple files and send as one
4:08
single attachment.
4:11
Please check the video description. In
4:13
the video description, I have shared my
4:15
blog post. From the blog post, you
4:17
should be able to get the sample Apex
4:19
code which was referenced in this video.
4:29
I hope it was helpful.
4:36
Thank you for watching.
#Email
#Internet Software
#Programming