Skip to content

IMAP Save Attachment

Type: imap-save-attachment  ·  Package: SMTP / IMAP Email Activities v1.0.5  ·  Output: List<string>

Downloads and saves the attachments of an email to a local folder, with their original names. Key in processes that receive documents by email (invoices, spreadsheets) to process them afterwards, without loading the bytes into memory with IMAP Read.

Given the mailId of an email (the .Id returned by IMAP Read), it opens folder in read-only mode, downloads the message and saves its attachments to outputPath:

  • If outputPath does not exist, it is created.
  • If you set attachmentName, only attachments with that exact name (case-insensitive) are saved; if you omit it, all are saved.
  • Each file keeps its original name (without paths); an attachment with no name is saved as attachment_N. If a file with that name already exists, it is overwritten.
  • It does not change the email’s state (it stays unread if it was).

Use the same folder you read the email from: the UID is only valid within its folder.

ParameterTypeDefaultDescription
hoststring—IMAP server.
usernamestring—Username.
passwordcredential—Password or App Password. Use credential("name")["password"].
mailIdstring—Email UID (from email.Id). Must be numeric.
outputPathstring—Local folder where attachments are saved. Created if missing.
ParameterTypeDefaultDescription
portint993IMAP port.
folderstring"INBOX"Folder that holds the email.
attachmentNamestring—Exact name of the attachment to save. If omitted, all are saved.
securitystring"ssl"Encryption: auto, none, ssl or starttls.

Returns a List<string> with the full path of each saved attachment, in the order they appear in the email. If none was saved, the list is empty. The log says how many attachments were saved; if you set attachmentName and it is not in the email, it warns The requested attachment was not found in the email. Since version 1.0.5 the output is declared and the Designer offers where to store it.

MessageCause and fix
Invalid 'mailId'. It must be the numeric UID returned by imap-read.mailId is not a number (for example, an Outlook .Id or empty text). Pass email.Id from IMAP Read.
'mailId' is required. / 'outputPath' is required.The parameter is missing or the expression returned null.
Message not found errorThe email is no longer in folder (it was moved or deleted) or was read from another folder.
Authentication or connection errorSame as in IMAP Read.

Save each email’s attachments in its own folder and check what arrived:

IMAP Read host = "imap.gmail.com" username = credential("mail-bot")["user"] password = credential("mail-bot")["password"]
subject = "Invoice" hasAttachments = true → output: emails
For Each items = emails itemVariable = email
└─ activities:
IMAP Save Attachment
host = "imap.gmail.com" username = credential("mail-bot")["user"] password = credential("mail-bot")["password"]
mailId = email.Id outputPath = "C:/invoices/" + email.Id → output: files
Log message = count(files) + " file(s) from " + email.From
  • IMAP Read — get the emails (and their mailId).
  • List Files — check what ended up in the folder.