Back
Tutorials
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
Engagespot account (Get one free here)
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:
Initialise the library
Include the Engagespot PHP SDK in your PHP code and create an instance of EngagespotClient
with your API key, API secret.
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.
Sending emails with attachments
Let's quickly revisit the steps we've done so far
We enabled and configured the email provider in Engagespot.
Created a notification template with content for email channel.
Installed the
engagespot/php-sdk
library in our Laravel project.Initialised the
EngagespotClient
instance.
Great!
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
Writing the send() function
You should pass the templateIdentifier
for the template you created in Engagespot console, along with the recipient
id, and the attachment payload.