Email attachments API to send and download attachments in code or tests

Use MailSlurp's attachment endpoints to upload, send, receive, and download attachments.

MailSlurp offers an email attachment API so you can send and receive attachments in code or tests without a mailserver. Most actions use the /attachments/ endpoints. See the AttachmentController for method documentation.

How attachments in emails work

The SMTP specification for email messaging lays out many official headers and properties of an email. It is however not very specific about atttachments. Using the MIME spec, an email and its attachments are transfered using a string multipart messagethat is base64 encoded as 7-bit ASCII text. MailSlurp makes attachment encoding easy.

MailSlurp attachment processes

Attachments can be sent and received in tests and code or using the MailSlurp dashboard. Create a free account to get started.

Send attachments with emails

Before you send an attachment first upload it in the dashboard. This will take care of the MIME encoding etc.

upload attachments

You can do the same in code using the AttachmentController uploadAttachment endpoint. Take a file convert it into a Base64 encoded string. POST it to the endpoint and it will return an array whose first element is the ID of the uploaded attachment.

PHP example

Here is an example of uploading an attachment in PHP:

private function uploadAttachment(): array
{
    // a path to some file you want to attach
    $pathToAttachment = $this->pathToAttachment;

    // read file contents
    $contents = file_get_contents($pathToAttachment);

    // encode the file contents to a base64 encoded string for uploading
    $base64Contents = base64_encode($contents);

    // extract file name from path
    $filename = basename($pathToAttachment);
    
    // get the mime type from the file name
    $contentType =  mime_content_type($filename);

    // set upload options
    $uploadOptions = new MailSlurpModelsUploadAttachmentOptions();
    $uploadOptions->setFilename($filename);
    $uploadOptions->setContentType($contentType);
    $uploadOptions->setBase64Contents($base64Contents);

    // now upload using attachment controller
    $attachmentController = new MailSlurpApisAttachmentControllerApi(null, $this->config);

    // returns [$attachmentId]
    return $attachmentController->uploadAttachment($uploadOptions);
}

NodeJS example

The same methods apply for other languages. Here is the same example of base64 encoding a file in Javascript:

/**
 * Upload base 64 encoded file
 * Return array containing attachment ID as first element
 * @returns {Promise<string[]>}
 */
async function uploadAttachment() {
    const fileBase64Encoded = await readFile(pathToAttachment, { encoding: 'base64' });
    const attachmentController = new MailSlurp(config).attachmentController;

    return attachmentController.uploadAttachment({
        base64Contents: fileBase64Encoded,
        contentType: 'text/plain',
        filename: basename(pathToAttachment)
    })
}

Attachment size limits

The MIME email standard does not specify a limit to attachments. See the email attachment size limits guide for more information.

Receive and download attachments

Attachments that are sent to MailSlurp mailboxes are stored and are available by ID. They can be viewed in the dashboard or using the API.

view attachment

Waiting for emails to arrive

You can wait for an expected emails using the WaitForEmailController. Retrieve an email to view the attachment IDS.

Download attachments by ID

Call the downloadAttachmentAsBase64Encoded to download an attachment as a base 64 encoded string. This makes consuming binary content easier for some clients. Base64 decode the content string to a byte array to access the file. This example uses the NodeJS library.

async function canReceiveAttachment(inboxId) {
  const waitForController = new MailSlurp(config).waitController;

  const email = await waitForController.waitForLatestEmail(inboxId, 60000, true)

  expect(email.attachments.length).toEqual(1);

  const emailController = new MailSlurp(config).emailController
  const attachmentDto = await emailController.downloadAttachmentBase64(email.attachments[0], email.id)

  expect(attachmentDto.base64FileContents).toBeTruthy()
  expect(attachmentDto.sizeBytes).toBeTruthy()
  expect(attachmentDto.contentType).toBeTruthy()
}

You can also call the downloadAttachmentAsBytes method and MailSlurp returns an octet-stream of bytes in response. These can be streamed to disk or a buffer and read later.

Related content

API Reference

Build powerful applications and test suites using real email addresses in code. Create inboxes on demand then send and receive emails and attachments in Javascript, Go, PHP, Ruby, Java, C#, REST and more.

Official MailSlurp documentation

Developer documentation and guides for MailSlurp's email APIs. REST, SDKS, Graphql, SMTP and more. MailSlurp is an email and SMS API service for develop

Ruby Mailer SDK - send and receive emails in Rails, Rspec and more using MailSlurp's official email Gem

Receive email and attachments with Rails, Rspec, and Ruby without SMTP using MailSlurp Ruby Gem.

Temporary email api

Temporary email API for disposable email accounts. Free for personal use with REST API and clients in NodeJS, CSharp and more.

Send attachments with an Email API

How to send email attachments using MailSlurp API.

Email attachment size limit

Email MIME type message attachment file size limits and conventions

Mailinator alternative

Alternatives to Mailinator for test email accounts. Create real email addresses using MailSlurp

Generate API clients using Swagger

How to create SDK libraries a language of your choice

Send email with Axios JS (and receive email too!)

Send email in Javascript with MailSlurp email API

Base64 file uploads

How to encode files as Base 64 encoded strings in several languages

Using Attachments

Use MailSlurp's attachment endpoints to upload, send, receive, and download attachments.

Using custom domains with MailSlurp

Email custom domains with API access

Create email inboxes using MailSlurp's API

Create email inboxes using MailSlurp's API

MailSlurp API entities and how to use them

MailSlurp API entities and how to use them

Get emails from code (how to receive emails programmatically)

How to receive emails directly in code, applications, and tests. Use MailSlurp to send and receive SMTP emails.

Sending email in Javascript and NodeJS (Typescript examples)

Sending email in Javascript and NodeJS (Typescript examples)

Ready to dive in?Start building email applications today.