Sending and Receiving Email Attachments

Master sending and receiving email attachments with SMTP emails. Learn how to upload, download, and attach files using MailSlurp.

  • Table of contents

smtp attachments Attachments are a way to send and receive files with in emails.

Sending attachments

Send attachments using the compose page in the dashboard or by using the sendEmail methods with a list of attachment IDs. Attachment IDs can be created by uploading files before sending (see next section). Here is an example using the NodeJS client:

await mailslurp.sendEmail(inbox.id!, {
  to: [inbox.emailAddress!],
  subject: 'attachment test',
  attachments: [attachmentId.toString()],
});

Uploading attachments

To upload attachments use the upload attachment method and pass a base64 encoded string for the file content. An array containing an attachment ID will be returned. Use the attachment ID to send the attachment or download it.

// notice that an array with one attachment id is returned
const [attachmentId] = await mailslurp.uploadAttachment({
  base64Contents: Buffer.from('hello').toString('base64'),
  contentType: 'text/plain',
  filename: 'test.txt',
});

Downloading attachments

Here is how to download files found on an email object.

const attachment =
  await mailslurp.attachmentController.downloadAttachmentAsBase64Encoded({
    attachmentId: email.attachments?.[0]!,
  });
const content = new Buffer(
  attachment.base64FileContents!,
  'base64'
).toString('utf8');
expect(content).toEqual('hello');