How to send and read email in Laravel (PHP)
Learn how to set up email functionality in Laravel with Composer. From creating a basic webapp to sending and reading emails through SMTP and APIs.
Laravel is an extraordinarily popular PHP framework for building powerful web applications. With a thriving community, Laravel enables pretty much any concept you can think of - including email. In this post we'll show you how to setup a basic Laravel application with Composer and enable email functionality. We will use two methods to send and read email in our app: SMTP connections and an email API provider.
Concept
To demonstrate email sending we will create a Laravel webapp with a view that provides a button that will send an email from one address to another. We will then use a second button to display the sent email. This will give us a good example of how to read and compose emails in PHP.
Getting starting
Let's get up and running by creating a new Laravel project. First verify you have PHP installed by opening a terminal and running
php -v
PHP command not found?
If you see that php
command is not found then you need to install PHP on your machine. On a Mac this can be done with Homebrew.
brew install php
Install Composer package manager
The next step is to install the PHP package manager composer.
Download the composer installation script from the downloads page:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Then use PHP to run the setup script:
php composer-setup.php --install-dir=bin --filename=composer
This will create a local file called composer
. We can install the executable globally by moving it to the user path.
mv composer.phar /usr/local/bin/composer
Create new Laravel app
Now we can use composer to create a new Laravel application:
composer create-project laravel/laravel php-laravel-email-examples
This command will create a new directory called php-laravel-email-examples
with a directory structure like so:
% tree -L 1
.
├── README.md
├── app
├── artisan
├── bootstrap
├── composer.json
├── composer.lock
├── config
├── database
├── lang
├── package.json
├── phpunit.xml
├── public
├── resources
├── routes
├── storage
├── tests
├── vendor
└── vite.config.js
Verify the app works
We can check that our new app works correctly by running the generated unit tests with the artisan test
command:
% php artisan test
PASS Tests\Unit\ExampleTest
✓ that true is true
PASS Tests\Feature\ExampleTest
✓ the application returns a successful response
Tests: 2 passed
Time: 0.04s
Run the webserver
We can also run the webserver and view the webapp in a browser using artisan
:
php artisan serve
This will start a server on localhost:8000
by default.
Creating a dummy view
Now that we have an application let us modify the default view page served by the index to include two buttons, one for sending one for receiving emails. Views are found in the resources/views
directory. Open the routes/web.php
file to view the current route setup:
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Notice the use of a welcome
template view. This refers to the welcome view inside the views directory:
resources/views/welcome.blade.php
We can replace this route with our own called index
:
Route::get('/', function () {
return view('index');
});
Then create a new template in the views directory called index.blade.php
.