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. That you have already create a basic automated test using our getting started guides.
  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.

console.log(message.attachments.length) // 2
print(len(message.attachments)) # 2
System.out.println(message.attachments().size()); // 2
Console.WriteLine(message.Attachments.Count); // 2
puts(message.attachments.length) # 2
print(count($message->attachments)); // 2
fmt.Println(len(message.Attachments)) // 2

Testing the file name and content type

Each attachment contains metadata on the file name and content type.

const firstAttachment = message.attachments[0]
console.log(firstAttachment.fileName) // "contract.pdf"
console.log(firstAttachment.contentType) // "application/pdf"
first_attachment = message.attachments[0]
print(first_attachment.file_name) # "contract.pdf"
print(first_attachment.content_type) # "application/pdf"
Attachment firstAttachment = message.attachments().get(0);
System.out.println(firstAttachment.fileName()); // "contract.pdf"
System.out.println(firstAttachment.contentType()); // "application/pdf"
var firstAttachment = message.Attachments[0];
Console.WriteLine(firstAttachment.FileName); // "contract.pdf"
Console.WriteLine(firstAttachment.ContentType); // "application/pdf"
first_attachment = message.attachments[0]
puts(first_attachment.file_name) # "contract.pdf"
puts(first_attachment.content_type) # "application/pdf"
$firstAttachment = $message->attachments[0];
print($firstAttachment->fileName); // "contract.pdf"
print($firstAttachment->contentType); // "application/pdf"
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).

const firstAttachment = message.attachments[0]
console.log(firstAttachment.length) // 4028
first_attachment = message.attachments[0]
print(first_attachment.length) # 4028
Attachment firstAttachment = message.attachments().get(0);
System.out.println(firstAttachment.length()); // 4028
var firstAttachment = message.Attachments[0];
Console.WriteLine(firstAttachment.Length); // 4028
first_attachment = message.attachments[0]
puts(first_attachment.length) # 4028
$firstAttachment = $message->attachments[0];
print($firstAttachment->length); // 4028
var firstAttachment = message.Attachments[0]
fmt.Println(firstAttachment.Length) // 4028

Embedded Images

If an email contains embedded images then the contentId attribute will be populated and will have a corresponding entry in the images array.

const firstAttachment = message.attachments[0]
console.log(firstAttachment.contentId) // "ii_1435fadb31d523f6"
first_attachment = message.attachments[0]
print(first_attachment.content_id) # "ii_1435fadb31d523f6"
Attachment firstAttachment = message.attachments().get(0);
System.out.println(firstAttachment.contentId()); // "ii_1435fadb31d523f6"
var firstAttachment = message.Attachments[0];
Console.WriteLine(firstAttachment.ContentId); // "ii_1435fadb31d523f6"
first_attachment = message.attachments[0]
puts(first_attachment.content_id) # "ii_1435fadb31d523f6"
$firstAttachment = $message->attachments[0];
print($first_attachment->contentId); // "ii_1435fadb31d523f6"
var firstAttachment = message.Attachments[0]
fmt.Println(firstAttachment.ContentId) // "ii_1435fadb31d523f6"