Send With Confidence
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.
Time to read: 13 minutes
Email is as important a communication tool as ever. To help you better leverage email, I’m going to show you how to send email using PHP’s Mezzio framework and Twilio SendGrid’s API.
Specifically, you’re going to learn how to send emails with both plaintext and HTML bodies, and that includes a PDF attachment. You’re also going to learn how to use Twilio SendGrid’s transactional templates functionality to make creating email bodies simpler both by the development team as well as any other team within your organization.
Sounds good? Let’s begin.
As a way of making this tutorial more meaningful, pretend that the code that we’ll write is part of a fictitious, online ecommerce shop built with Mezzio, named The Little PHP Shop—specifically, the part immediately after a customer makes a purchase. At that point in the user flow, the customer receives an email, thanking them for their purchase, and includes a PDF invoice for their records.
We’re going to create a Handler class to send the post-purchase email, which will receive purchase details from an order completed by the customer. With that purchase information, the Handler class will then use several classes in the SendGrid PHP API to build and send the purchase confirmation email.
Of these, the most important are “SendGrid\Mail\Mail” and “\SendGrid.” “SendGrid\Mail\Mail” is the object that stores all of the properties for an email message that we’ll send through Twilio SendGrid. The “SendGrid” object forms the transport layer, facilitating the sending of emails through Twilio SendGrid.
To complete this tutorial, you'll need the following 4 things in your local development environment:
We first need to create the base application. To do that, we’re going to, as always, use the Mezzio Skeleton to do it for us. Once we scaffold the base application, we’ll switch to the newly created project directory. Then run the following commands in your terminal, following the prompts for the first one:
When we invoke the class, it has access to the application’s dependency injection (DI) container, from which it’ll retrieve the configuration details that we stored in “config/autoload/mail.global.php.”
After that, it’ll instantiate a new “SendGrid\Mail\Mail” object and set the from and reply to details by passing the respective configuration details to calls to “Mail,” “setFrom,” and “setReplyTo” methods, respectively. After that, it’ll return the instantiated “Mail” object.
To use it, though, you have to register it with the DI container. To do that, in “src/App/src/ConfigProvider,” add the following entry to the “factories” element in the array returned from the “getDependencies” method.
Running the command above does four things for us:
With “EmailSenderHandler.php” created, we now need to refactor it so that it can send emails. To do that, we’ll first refactor “EmailSenderHandler’s” constructor, replacing the “TemplateRendererInterface” parameter with 3 new parameters. These will initialize 3 new class member variables:
You can see the revised constructor in the example below, along with the related class member variables. Replace the existing class member variable and the constructor with this code.
This retrieves a “\SendGrid\Mail\Mail” object from the DI container, preinitialized with the sender and reply to email details, and initializes a new object named “$message.” It then instantiates a new “SendGrid” object, named “$mailer” for sending the mail message. Finally, it retrieves the mail configuration from the application’s configuration. Then, it uses these to initialize and return the “EmailSenderHandler” object.
With all of those changes, there’s one last change to make before we can test the code and send an email. We have to update the routing table so that the default route uses our new Handler class as the route’s handler, instead of “HomePageHandler.” To do that, replace the default route’s definition in “config/routes.php” with the following example.
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.