Most Popular
We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.
Sign up
Resetting passwords is a critical way to make your app accessible to users, but setting up email notifications for password resets from scratch can be tedious and error-prone. While Laravel does support password reset email templates, Laravel emails can be unreliable and prone to being caught in spam filters, and the default email templates are not particularly customizable. It might also be desirable to set up a backup channel like SMS or a backup email provider in case the primary fails.
That’s where Courier comes in. Courier offers complete notification infrastructure with an API that simplifies some of the most complex notification logic, allowing you to notify customers across multiple channels (including email, SMS, push, chat apps, and more). You can also use Courier's no-code web tools to design templates and communication sequences, and make use of its analytics dashboard to monitor delivery and engagement metrics.
By using Courier to send Laravel notifications, you can send multi-channel alerts via email, SMS, push, and other channels based on your users’ preferences or actions, with the results all being reported back to one centralized analytics and logging console.
In this tutorial, we'll show you how to implement PHP Laravel password reset notifications the right way, using the Courier PHP SDK. The examples in this tutorial will use Gmail to send mail, but Courier supports a number of other email service providers as well.
The Courier SDK uses HTTPlug to send requests, which defaults to using the Guzzle HTTP client. Make sure that this is included as a dependency in your Laravel project by running:
1composer require guzzlehttp/guzzle
Add the Courier PHP SDK package to your Laravel project using Composer:
1composer require trycourier/courier --with-all-dependencies
If you don’t have a Courier account, create one now.
Before you can send emails using Courier, you must configure an email service provider. In the Courier app, navigate to Channels and select your email service provider from the Provider Catalog. For this tutorial, we will be using Gmail.
Next, select Sign in with Google and give Courier access to your Google account. Now you have configured Gmail as an email provider.
Note that Courier does offer channel failover (eg. to SMS) or provider failover in the event that the primary email provider (in this case, Gmail) fails.
Navigate to Designer in Courier, and click the Create Template button to create a new template, and name it Reset password. If you check the box Use AI to generate notification content, an email template will be generated for you, based on your notification name. Click Create Template again.
You will now be prompted to select the notification channels you want to be able to send your notifications to. Select Email as your notification channel, and add Gmail as its provider in the drop-down box (which will be available to you if you configured this in step 2). Then, click on your new email channel on the left of your screen to start adding content to your notification template.
Use Courier’s template editor to write the content for your password reset notification. Courier allows variables in its email templates, which you can add using curly braces. Courier will highlight these variables in green for easy visibility.
Click on the three dots icon on your notification template to edit your brand. You can add a brand logo, change your brand color scheme, and add headers, footers, and links to your company social network profiles.
Changing your branding will result in the branding being changed on your password reset email when it is sent:
Now click Publish Changes to publish the new notification, and then click the settings button for your notification template, which is near the top left corner of the window.
In the settings, you will see a Notification ID, which is the ID of this template. You will use this later, to refer to this notification template inside your Laravel application.
For the purposes of this demonstration, we will trigger the Courier API from an HTTP controller. In production, you will most likely want to adapt this code to send from a custom notification channel or using a queued job.
Create a new controller in your Laravel project called NotificationController:
1php artisan make:controller NotificationController
Add the following code to this controller:
1<?php23namespace App\Http\Controllers;45use App\Http\Controllers\Controller;6use Illuminate\Http\Request;7use Courier\CourierClient;89class NotificationController extends Controller10{11public function password_reset()12{13$courier = new CourierClient("https://api.courier.com/", "<YOUR_COURIER_AUTH_TOKEN>");1415$result = $courier->sendEnhancedNotification(16(object) [17'to' => [18'email' => "<RECEIVER_EMAIL>"19],20'template' => "<NOTIFICATION_ID>",21'data' => [22'userName' => "<USER_NAME>",23'passwordResetLink' => "<RESET_PASSWORD_LINK>",24'companyName' => "<COMPANY_NAME>",25],26],27);2829return [30"status" => 1,31"data" => $result->requestId32];33}34}
Replace <YOUR_COURIER_AUTH_TOKEN>
with your Courier API key, which you can find in the Courier app by navigating to Settings → API Keys. Note that it is bad security practice to store an authorization token in a controller — this should only be done for testing purposes, and you should not store these credentials in source control. You should instead store tokens and credentials as environment variables and access them via the Laravel config.
Replace <NOTIFICATION_ID>
with your template ID, which is in your notification template settings, as described above.
The data
object should contain the variables that are used in the template. In this tutorial, our template contains three variables — userName, passwordResetLink, and companyName — however, you can include any variables you like, providing you set them in the data object in your code:
1'data' => [2'userName' => "<USER_NAME>",3'passwordResetLink' => "<RESET_PASSWORD_LINK>",4'companyName' => "<COMPANY_NAME>"5],
Replace <USER_NAME>
with Max Overdrive, <RESET_PASSWORD_LINK>
with http://example.com/reset, and <COMPANY_NAME>
with ACME Inc.
Finally, replace <RECEIVER_EMAIL>
with your own email address, so that when you run your Laravel code, you can verify the email was sent to you.
Configure the Laravel route for your notifications by navigating to the routes/api.php
file in your Laravel project and configure a new route for your NotificationController’s password-reset request:
1<?php23use Illuminate\Http\Request;4use Illuminate\Support\Facades\Route;5use App\Http\Controllers\NotificationController;67Route::get('password-reset', [NotificationController::class, 'password_reset']);
Run your Laravel application with the php artisan serve
command. By default, your Laravel app is served on HTTP port 8000.
Now, send a GET request to your password reset URL by entering the following address into your browser:
1http://127.0.0.1:8000/api/password-reset
Or use curl to test from the command line:
1curl http://127.0.0.1:8000/api/password-reset
This should cause the password email to be sent. If you ensure that the value of the email
key in the Laravel code is your own email address, you can test that the email is sent to you. If successful you will see a JSON response resembling:
1{"status":1,"data":"xxxxxxxxxxx"}
If you receive an error, you will receive a message to assist with debugging.
Check your email inbox, and you should find the password reset notification there.
When you’re testing your code by sending an email to yourself, it’s easy to be sure whether the notification was sent or not. But if you want to check if an email was sent to a user, you will need to check the Courier logs.
Navigate to the Logs page in the Courier app. You'll see a list of all notifications sent from your account, with their status, including whether each one was sent, delivered, opened, or clicked, or whether there were any errors with sending.
This tutorial covered how to integrate the sending of password reset notifications in Courier into your Laravel codebase, using the Courier PHP SDK. You can use this same technique for sending other kinds of notifications in Courier such as welcome emails, shopping cart abandonment notifications, and order confirmation emails.
You can also use the Courier PHP SDK to interact with almost anything you might want to do in Courier, including updating notification preferences, invoking automations, or updating audiences.
Please contact us if you want to further discuss your project and unique requirements.
We have SDKs in Ruby, Go, Python, Javascript, Node.js, and React.
Sign up
How to Set Up Automatic Push Notifications Based on Segment Events
Push notifications have carved their own niche as a powerful tool for continuous user engagement. Regardless of whether an app is actively in use, they deliver your messages straight to your user's device. Two key players that can combine to enhance your push notification strategy are Segment and Courier. In this tutorial, we show you how to set up Courier to listen to your Segment events and then send push notifications to an Android device based on data from these events.
Sarah Barber
November 17, 2023
How to Send Firebase Notifications to iOS Devices Using Courier
This tutorial explains how to send push notifications to iOS devices from your iOS application code using Firebase FCM and Courier’s iOS SDK.
Martina Caccamo
November 01, 2023
Free Tools
Comparison Guides
Send up to 10,000 notifications every month, for free.
Get started for free
Send up to 10,000 notifications every month, for free.
Get started for free
© 2024 Courier. All rights reserved.