Installing PHPMailer on Ubuntu 22.04 LTS

Introduction

PHPMailer is a popular open-source library for sending email messages in PHP. It provides a set of classes and methods that allow developers to easily send emails using PHP code. PHPMailer supports various features such as attachments, HTML content, inline images, and more.

Prerequisites

  • Up and running ubuntu 22.04 LTS machine.
  • Basic knowledge in linux commands.
  • Internet connectivity.

In this post, We will show you how to install PHPMailer extension on ubuntu 22.04 LTS machine.

Step 1: Installing Composer

To install PHPMailer on Ubuntu 22.04 LTS, you typically don’t install it system-wide like you would with some other packages. PHPMailer is typically included in your PHP project using Composer, which is a dependency manager for PHP.

Here’s a step-by-step guide on how to install PHPMailer using Composer:

If you don’t have Composer installed on your system, you can install it globally by running the following commands in your terminal:

sudo apt update
sudo apt install php-cli unzip
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e5325b19b381bfd8b51397d54f4d8b0db70cfeb04264f5561fd238b9bcbcb6d4d20a611d45f9eabdb0f122581a4b4f86') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
php -r "unlink('composer-setup.php');"

Step 2: Create a New Project

Navigate to your project directory or create a new directory for your PHP project. Then, create a new composer.json file in your project directory with the following content:

{
    "require": {
        "phpmailer/phpmailer": "^6.5"
    }
}

Save the file.

Step 3: Installing PHPMailer

Run the following command in your project directory to install PHPMailer and its dependencies:

composer install

This command will download PHPMailer and its dependencies into a vendor directory within your project.

Step 4: Use PHPMailer in Your Code

Now you can use PHPMailer in your PHP code. Include the Composer autoloader at the beginning of your PHP file where you want to use PHPMailer.

<?php

require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Your PHPMailer code goes here

Replace the comment with your actual PHPMailer code.

Conclusion

That’s it! You have now successfully installed PHPMailer in your PHP project on Ubuntu 22.04 LTS using Composer. Make sure to adapt your PHPMailer code according to your specific use case, , If you still have questions, please post them in the comments section below.

Author

Installing PHPMailer on Ubuntu 22.04 LTS

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top