Mar 19, 2024

How to send email attachments from PHP Laravel app?

Amal Raju

In this tutorial, we'll learn how to send attachments like PDFs, DOC, Images from a PHP Laravel application or API. We'll be using Engagespot's unified notification library for sending emails via PHP Laravel with any email provider of your choice (Sendgrid, AWS SES etc).

Prerequisites for sending emails from Laravel

  1. Engagespot account (Get one free here)

  2. Any email provider of your choice

Enable e-mail channel in Engagespot console

Engagespot supports sending notifications via multiple channels including email. If you're a first-time user, login to your Engagespot console, navigate to "Channels" -> "Email" and enable any email provider of your choice.

The best thing is that you don't have to learn the API documentations of these providers. Once you integrate Engagespot, you can switch between these providers without any coding.

Configure your email provider

To begin, the first step is configuring the email channel within Engagespot, where you'll find support for various email providers such as Sendgrid, AWS SES etc.

Create an email template

Within the Engagespot console, crafting a tailored email template for sending messages is a straightforward process. Simply navigate to the "Templates" menu, where you can create and enable the email channel for your chosen template. Find more about templates here

Installing Engagespot PHP library

Install the Engagespot PHP library using Composer:

composer require engagespot/php-sdk

Initialise the library

Include the Engagespot PHP SDK in your PHP code and create an instance of EngagespotClient with your API key, API secret.

use Engagespot\EngagespotClient  

//API-key and API-secret is from engage spot dashboard
$apiKey = '#########';
$apiSecret = '#########';

// Create an instance of EngagespotClient
$engagespot = new EngagespotClient($apiKey, $apiSecret);

You can find your API_KEY and API_SECRET from the Engagespot console.

Create the user in Engagespot

Engagespot requires you to create your user's profile in Engagespot using the createOrUpdateUser() method as follows.

$engagespot->createOrUpdateUser('any-unique-id-of-the-user', [ 'email' => '' ]);

Sending emails with attachments

Let's quickly revisit the steps we've done so far

  1. We enabled and configured the email provider in Engagespot.

  2. Created a notification template with content for email channel.

  3. Installed the engagespot/php-sdk library in our Laravel project.

  4. Initialised the EngagespotClient instance.


Now, all you have to do is, trigger the template using the send() function in EngagespotClient instance, along with the attachment object.

The attachment object

Attachment object is something that you'll send as part of the send notification request payload. It should contain -

content: The base64 encoded body of the file (pdf/doc/img etc)

name: The filename

contentType: MIME Type of the attached file. For example: application/pdf

  "content"=>"base64 encoded file content ",
  "name"=>"Filename for example - sample.pdf",
  "contentType"=>"MIME Type of attachment for example - application/pdf"

Writing the send() function

You should pass the templateIdentifier for the template you created in Engagespot console, along with the recipientid, and the attachment payload.

$templateIdentifier = 'template-identifier'; // notification templates identifier from engagespot dashboard
$recipients = [''];//unique identifier of user in engagespot
$notificationData = [
    'notification' => [
        'templateIdentifier' => $templateIdentifier,
        'data' => [
            'attachments' => [
                    'content' => "BASE64_CONTENT",
                    'name' => "sample1.pdf",
                    'contentType' => "application/pdf" 
      'sendTo' => [
          'recipients' => $recipients,
$response = $engagespot->send($notificationData);

Amal Raju

Share this post