PHP 3d ago 5 views 2 min read

How to send email notifications with PHPMailer in PHP

Install PHPMailer, configure SMTP settings, and send HTML emails from a PHP script using PHP 8.3 and the latest PHPMailer version.

Riya K.
Updated 22h ago
Sponsored

Cloud Hosting — blazing fast websites

Fully managed cloud hosting with free SSL, auto-backups and a friendly cPanel. Built for WordPress, Laravel and custom PHP apps.

Install the PHPMailer library and configure your SMTP server to send transactional emails directly from your PHP application. These steps target PHP 8.3.x, PHPMailer 6.9.x, and standard Linux environments.

Prerequisites

  • Linux server with PHP 8.3.x installed
  • Composer 2.7.x available in your shell
  • Active SMTP server access (e.g., Gmail, SendGrid, or a custom mail server)
  • SSH access to the server as root or a user with sudo privileges
  • A valid email address and SMTP credentials for your provider

Step 1: Create a project directory

Create a dedicated folder for your email script to keep dependencies isolated from your main application. Navigate to your desired location and initialize a new Composer project.

mkdir ~/phpmailer-example
cd ~/phpmailer-example
composer init

You will see a prompt asking for package name, author, and description. Press Enter to accept defaults or type your own values. This creates a composer.json file in your current directory.

Step 2: Install PHPMailer via Composer

Run the Composer require command to add the PHPMailer package to your project dependencies. This installs the library and its required autoload files automatically.

composer require phpmailer/phpmailer

Composer will download the package and update your composer.json and composer.lock files. You will see output indicating the package version installed, typically 6.9.1 or a similar recent release.

Step 3: Create the PHP email script

Create a new file named send-email.php in your project directory. Open the file in your text editor and paste the following code. This script initializes the PHPMailer object and sets up the default configuration.

      
Sponsored

Linux Dedicated Server

Rock-solid Linux dedicated servers with root access, KVM-IPMI and fully managed options. CentOS, Ubuntu, Debian, Rocky and AlmaLinux.

Tags: phptutorialEmailSMTPPHPMailer
0
Was this helpful?

Related tutorials

Comments 0

Login to leave a comment.

No comments yet — be the first to share your thoughts.