# Email attachments Learn how to test the email attachments with Mailosaur. ## Before you begin The examples shown below are based on two key assumptions: 1. You have already created a basic automated test using our [getting started guides](/docs/automation). 2. You have a chosen assertion library that you will use to test the values shown below. ## How many attachments The `attachments` property of a message contains an array of attachments. The length of this array corresponds to the number of files attached to the email. ```nodejs console.log(message.attachments.length) // 2 ``` ```python print(len(message.attachments)) # 2 ``` ```java System.out.println(message.attachments().size()); // 2 ``` ```csharp Console.WriteLine(message.Attachments.Count); // 2 ``` ```ruby puts(message.attachments.length) # 2 ``` ```php print(count($message->attachments)); // 2 ``` ```go fmt.Println(len(message.Attachments)) // 2 ``` ## Testing the file name and content type Each attachment contains metadata on the file name and content type. ```nodejs const firstAttachment = message.attachments[0] console.log(firstAttachment.fileName) // "contract.pdf" console.log(firstAttachment.contentType) // "application/pdf" ``` ```python first_attachment = message.attachments[0] print(first_attachment.file_name) # "contract.pdf" print(first_attachment.content_type) # "application/pdf" ``` ```java Attachment firstAttachment = message.attachments().get(0); System.out.println(firstAttachment.fileName()); // "contract.pdf" System.out.println(firstAttachment.contentType()); // "application/pdf" ``` ```csharp var firstAttachment = message.Attachments[0]; Console.WriteLine(firstAttachment.FileName); // "contract.pdf" Console.WriteLine(firstAttachment.ContentType); // "application/pdf" ``` ```ruby first_attachment = message.attachments[0] puts(first_attachment.file_name) # "contract.pdf" puts(first_attachment.content_type) # "application/pdf" ``` ```php $firstAttachment = $message->attachments[0]; print($firstAttachment->fileName); // "contract.pdf" print($firstAttachment->contentType); // "application/pdf" ``` ```go var firstAttachment = message.Attachments[0] fmt.Println(firstAttachment.FileName) // "contract.pdf" fmt.Println(firstAttachment.ContentType) // "application/pdf" ``` ## Testing attachment file size The `length` property returns the size of the attached file (in bytes). ```nodejs const firstAttachment = message.attachments[0] console.log(firstAttachment.length) // 4028 ``` ```python first_attachment = message.attachments[0] print(first_attachment.length) # 4028 ``` ```java Attachment firstAttachment = message.attachments().get(0); System.out.println(firstAttachment.length()); // 4028 ``` ```csharp var firstAttachment = message.Attachments[0]; Console.WriteLine(firstAttachment.Length); // 4028 ``` ```ruby first_attachment = message.attachments[0] puts(first_attachment.length) # 4028 ``` ```php $firstAttachment = $message->attachments[0]; print($firstAttachment->length); // 4028 ``` ```go var firstAttachment = message.Attachments[0] fmt.Println(firstAttachment.Length) // 4028 ``` ## Embedded Images If an email contains [embedded images](/docs/automation/images#6-embedded-images) then the `contentId` attribute will be populated and will have a corresponding entry in the [images array](/docs/automation/images#6-embedded-images). ```nodejs const firstAttachment = message.attachments[0] console.log(firstAttachment.fileName) // "contract.pdf" console.log(firstAttachment.contentType) // "application/pdf" ``` ```python first_attachment = message.attachments[0] print(first_attachment.file_name) # "contract.pdf" print(first_attachment.content_type) # "application/pdf" ``` ```java Attachment firstAttachment = message.attachments().get(0); System.out.println(firstAttachment.fileName()); // "contract.pdf" System.out.println(firstAttachment.contentType()); // "application/pdf" ``` ```csharp var firstAttachment = message.Attachments[0]; Console.WriteLine(firstAttachment.FileName); // "contract.pdf" Console.WriteLine(firstAttachment.ContentType); // "application/pdf" ``` ```ruby first_attachment = message.attachments[0] puts(first_attachment.file_name) # "contract.pdf" puts(first_attachment.content_type) # "application/pdf" ``` ```php $firstAttachment = $message->attachments[0]; print($firstAttachment->fileName); // "contract.pdf" print($firstAttachment->contentType); // "application/pdf" ``` ```go var firstAttachment = message.Attachments[0] fmt.Println(firstAttachment.FileName) // "contract.pdf" fmt.Println(firstAttachment.ContentType) // "application/pdf" ``` ## Testing content ID The `contentId` property identifies embedded attachments within an email. ```nodejs const firstAttachment = message.attachments[0] console.log(firstAttachment.contentId) // "ii_1435fadb31d523f6" ``` ```python first_attachment = message.attachments[0] print(first_attachment.content_id) # "ii_1435fadb31d523f6" ``` ```java Attachment firstAttachment = message.attachments().get(0); System.out.println(firstAttachment.contentId()); // "ii_1435fadb31d523f6" ``` ```csharp var firstAttachment = message.Attachments[0]; Console.WriteLine(firstAttachment.ContentId); // "ii_1435fadb31d523f6" ``` ```ruby first_attachment = message.attachments[0] puts(first_attachment.content_id) # "ii_1435fadb31d523f6" ``` ```php $firstAttachment = $message->attachments[0]; print($first_attachment->contentId); // "ii_1435fadb31d523f6" ``` ```go var firstAttachment = message.Attachments[0] fmt.Println(firstAttachment.ContentId) // "ii_1435fadb31d523f6" ``` ## Fetching an attachment as base64 You can fetch the content of an attachment, encoded as base64, using the “get attachment” method: ```nodejs const firstAttachment = message.attachments[0] const file = await mailosaur.files.getAttachment(firstAttachment.id) const base64 = file.toString('base64') console.log(base64) ``` ```python first_attachment = message.attachments[0] file = mailosaur.files.get_attachment(first_attachment.id).content base64 = base64.b64encode(file) print(base64) ``` ```java Attachment firstAttachment = message.attachments().get(0); byte[] file = mailosaur.files().getAttachment(firstAttachment.id()); String base64 = new String(Base64.getEncoder().encode(file)); System.out.println(base64); ``` ```csharp var firstAttachment = message.Attachments[0]; var file = mailosaur.Files.GetAttachment(firstAttachment.Id)); var base64 = Convert.ToBase64String(file); Console.WriteLine(base64); ``` ```ruby first_attachment = message.attachments[0] file = mailosaur.files.get_attachment(first_attachment.id) base64 = Base64.encode64(file) puts base64 ``` ```php $firstAttachment = $message->attachments[0]; $file = $mailosaur->files->getAttachment($firstAttachment->id); $base64 = base64_encode($file); print($base64); ``` ```go var firstAttachment = message.Attachments[0] var file, _ = m.Files.GetAttachment(firstAttachment.Id) base64 := b64.StdEncoding.EncodeToString(file) fmt.Println(base64) ``` ## Saving an attachment to disk You can also write an attached file to disk: ```nodejs const firstAttachment = message.attachments[0] const file = await mailosaur.files.getAttachment(firstAttachment.id) fs.writeFileSync(firstAttachment.fileName, file) ``` ```python first_attachment = message.attachments[0] file = mailosaur.files.get_attachment(first_attachment.id).content f = open(first_attachment.file_name, 'wb') f.write(file) f.close() ``` ```java Attachment firstAttachment = message.attachments().get(0); byte[] file = mailosaur.files().getAttachment(firstAttachment.id()); Files.write(Paths.get(firstAttachment.fileName()), file); ``` ```csharp var firstAttachment = message.Attachments[0]; var file = mailosaur.Files.GetAttachment(firstAttachment.Id)); File.WriteAllBytes(firstAttachment.FileName, file); ``` ```ruby first_attachment = message.attachments[0] file = mailosaur.files.get_attachment(first_attachment.id) File.open(first_attachment.file_name, 'wb') { |fp| fp.write(file) } ``` ```php $firstAttachment = $message->attachments[0]; $file = $mailosaur->files->getAttachment($firstAttachment->id); $f = fopen($firstAttachment->fileName, "w") or die("Unable to open file!"); fwrite($f, $file); fclose($f); ``` ```go var firstAttachment = message.Attachments[0] var file, _ = m.Files.GetAttachment(firstAttachment.Id) var err = os.WriteFile(firstAttachment.FileName, file, 0644) if err != nil { // handle error } ``` #### See also - [Extracting links](/docs/automation/links) - [Extracting codes](/docs/automation/codes)