How to send password reset emails using PHPMailer in PHP
Configure SMTP settings, compose the reset message, and deliver the email securely using PHPMailer with PHP 8.3 and Composer.
This guide shows you how to send password reset emails using PHPMailer with PHP 8.3. You will configure SMTP credentials, compose the reset message, and deliver the email securely. The steps target a standard Linux environment with PHP 8.3.x and Composer 2.7.x.
Prerequisites
- Operating system: Ubuntu 24.04, Debian 12, or AlmaLinux 9.
- PHP 8.3.x installed and running.
- Composer 2.7.x installed globally.
- A valid SMTP server account (e.g., Gmail, SendGrid, or Postmark) with app-specific password enabled.
- SSH access to the server with sudo privileges.
Step 1: Create a project directory
Create a new directory for your PHP application and navigate into it.
mkdir password-reset-app
cd password-reset-app
Initialize a new Composer project to manage dependencies.
composer init --name="vendor/project" --description="Password reset application" --type="project"
You will see a composer.json file created in the current directory.
Step 2: Install PHPMailer via Composer
Install the latest stable version of PHPMailer using Composer. This installs the library and its dependencies automatically.
composer require phpmailer/phpmailer
Composer will output a progress bar and confirm the installation. You will see the package added to composer.json and the files placed in vendor/.
Step 3: Create the email sender script
Create a file named send-reset.php in your project root. This script will instantiate PHPMailer, configure the SMTP settings, and send the email.
touch send-reset.php
Open the file in your editor and paste the following code. Replace the placeholder values with your actual SMTP credentials.