Tutorial: Send an email with attachments


This tutorial shows you how to send an email with PDF attachments. More specifically, it describes the scenario when the sending email address is a custom one (for example, no.reply@{your-app.domain}). In the steps below, you will first call the File Management Service to create the file attachments and then call the Email Service to actually send the email.

Prerequisites

To complete this tutorial, the following prerequisites must be in place:

  1. You must have a service access key that provides access both to the Email Service and the File Management Service. For the scope of this tutorial, this can be accomplished by setting the key’s scope to email-api:query:* email-api:mutation:* file-management-api:query:* file-management-api:mutation:*. For more information, see Generate a service access key.
  2. Since we will be using a custom email domain in the sending email address, the sending email inbox must first be validated. For more information, see Sending email from a custom email address.

Step 1: Create the attachment files

As stated above, the email must be sent with two PDF files as attachments. Assuming that you have both files on your local disk, follow these instructions:

  1. Using the endpoint of the File Management Service and your service access key in the x-api-key HTTP header, run the following mutation:

    mutation ($input:CreateFileInput) {
          createFile(input:$input) {
            id
            name
            status
            uploadUrl(expiresIn: 3600)
          }
        }
    

    In variables, make sure to replace the file name as necessary:

    {
          "input": {
            "name": "YOUR_FILE_NAME",
            "contentType": "application/pdf"
          }
        }
    

    The code listing above creates a PDF file in the SBS cloud. Note that the file is generated as protected by default. The file’s upload URL is set to expire in 1 hour (3600 seconds); you can modify this value as required.

    Take notice of the file ID and upload URL generated in the response; you will need these in the next steps.

  2. Upload the file content through a PUT HTTP request. In this example, we are using cURL (https://curl.se/) to make the HTTP request. If you prefer not to use cURL, you can achieve the same result with any other REST client capable of sending PUT requests to a server.

    On bash terminals, run:

    curl --location --request PUT 'YOUR_UPLOAD_URL' --header 'Content-Type: application/pdf' --data-binary '@/path/to/document.pdf'
    

    On Windows terminals, run:

    curl --location --request PUT "YOUR_UPLOAD_URL" --header "Content-Type: application/pdf" --data-binary "@C:\path\to\document.pdf"
    

  3. Repeat steps 1 and 2 for the second PDF file.

At this stage, you should be able to view each of the files created so far, using the following query:

query viewFile($input:FilesFilter) {
  files(filter:$input) {
    items {
      id
      name
      status
      downloadUrl(expiresIn: 3600)
    }
  }
}
{
  "input": {
    "id": "YOUR_FILE_ID"
  }
}

In the code listing above, we included the downloadURL in the response in case you want to quickly preview the file by opening this URL in the browser. Note that the download URL is set to expire in 10 minutes (600 seconds); you can modify this value if necessary.

Step 2: Send the email

Now that you have both attachments in the cloud, you can send the email using the endpoint of the Email Service. First, make sure that the x-api-key HTTP header contains your service access key, and then run the following mutation:

mutation sendEmail($input:SendMessageInput!) {
  sendMessage(input:$input) {
    id
  }
}
{
  "input": {
    "to": {
      "address": "RECIPIENT_EMAIL"
    },
    "from": {
      "address": "VALIDATED_EMAIL_ADDRESS",
      "name": "SENDER_NAME"
    },
    "subject": "Email with attachments",
    "htmlBody": "A very <strong>important</strong> email.",
    "attachments": [{
      "name": "A.pdf",
      "fileId": "YOUR_FIRST_FILE_ID"
    }, {
      "name": "B.pdf",
      "fileId": "YOUR_SECOND_FILE_ID"
    }]
  }
}

In the code listing above, replace the variable values as follows:

  • RECIPIENT_EMAIL - set this to the email address that will receive this email.
  • VALIDATED_EMAIL_ADDRESS - set this to the email address that is sending the email. Note that the email will be sent successfully only if this is a validated email inbox, as described in Sending email from a custom email address.
  • SENDER_NAME - the sender’s name will appear in the email client of the recipient.
  • YOUR_FIRST_FILE_ID - set this to the id of the first PDF created previously through the File Management Service.
  • YOUR_SECOND_FILE_ID - set this to the id of the second PDF created previously through the File Management Service.

Editing these variables is the minimum you should do to be able to send the email; you can, of course, edit other variables as well and further customize your email.