Email attachments
Learn how to test the email attachments with Mailosaur
Before you begin
The examples shown below are based on two key assumptions:
- That you have already create a basic automated test using our getting started guides.
- 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"
Fetching an attachment as base64
You can fetch the content of an attachment, encoded as base64, using the “get attachment” method:
const firstAttachment = message.attachments[0]
const file = await mailosaur.files.getAttachment(firstAttachment.id)
const base64 = file.toString('base64')
console.log(base64)
first_attachment = message.attachments[0]
file = mailosaur.files.get_attachment(first_attachment.id).content
base64 = base64.b64encode(file)
print(base64)
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);
var firstAttachment = message.Attachments[0];
var file = mailosaur.Files.GetAttachment(firstAttachment.Id));
var base64 = Convert.ToBase64String(file);
Console.WriteLine(base64);
first_attachment = message.attachments[0]
file = mailosaur.files.get_attachment(first_attachment.id)
base64 = Base64.encode64(file)
puts base64
$firstAttachment = $message->attachments[0];
$file = $mailosaur->files->getAttachment($firstAttachment->id);
$base64 = base64_encode($file);
print($base64);
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:
const firstAttachment = message.attachments[0]
const file = await mailosaur.files.getAttachment(firstAttachment.id)
fs.writeFileSync(firstAttachment.fileName, file)
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()
Attachment firstAttachment = message.attachments().get(0);
byte[] file = mailosaur.files().getAttachment(firstAttachment.id());
Files.write(Paths.get(firstAttachment.fileName()), file);
var firstAttachment = message.Attachments[0];
var file = mailosaur.Files.GetAttachment(firstAttachment.Id));
File.WriteAllBytes(firstAttachment.FileName, file);
first_attachment = message.attachments[0]
file = mailosaur.files.get_attachment(first_attachment.id)
File.open(first_attachment.file_name, 'wb') { |fp| fp.write(file) }
$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);
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
}