How to install PHPMailer in Ubuntu? PHPMailer is a widely-used library for sending emails via PHP. Its robust feature set and compatibility make it a preferred choice for developers. If you’re running Ubuntu and need to integrate PHPMailer into your application, this guide will walk you through the process step by step. By the end, you’ll have how to install PHPMailer in Ubuntu and configured for use.
Prerequisites
prior to diving into the installation, make sure you have the following:
- Ubuntu: Any advance version of Ubuntu (20.04, 22.04, etc.).
- PHP Installed: Verify that PHP is installed on your system by running:
php -v
If PHP isn’t installed, you can install it using this command:
sudo apt update
sudo apt install php - Composer: PHPMailer can be easy to installed using Composer, a dependency manager for PHP. Install it by following the commands below:
sudo apt install composer
composer -V - Internet Connection: PHPMailer’s installation requires downloading packages from the internet.
How to Install PHPMailer in Ubuntu? A Step-by-Step Guide
Step 1: Update Your System
Begin by confirm your system packages are up to date. Open a terminal and run:
sudo apt update && sudo apt upgrade -y
Step 2: Navigate to Your Project Directory
Decide where you want to use PHPMailer. Navigate to the root of your PHP project directory using:
cd /path/to/your/project
Replace /path/to/your/project with the actual path to your PHP application.
Step 3: Install PHPMailer via Composer
To install PHPMailer, execute the following command within your project directory:
composer require phpmailer/phpmailer
This command downloads and installs PHPMailer and its dependencies. Once the installation is complete, you should see a vendor directory in your project folder. This directory contains the PHPMailer library.
Step 4: Verify Installation
Ensure PHPMailer has been successfully installed by checking its presence in composer.json. Run:
cat composer.json
Look for an entry similar to:
"require": {
"phpmailer/phpmailer": "^6.0"
}
Step 5: Configure PHPMailer
Now, let’s configure PHPMailer to send emails. Create a new PHP file (for example email_test.php) in your project directory:
nano email_test.php
Add the following sample code to configure and send an email:
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Replace with your SMTP server
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Replace with your email
$mail->Password = 'your_email_password'; // Replace with your email password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Recipient Name');
// Content
$mail->isHTML(true);
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using PHPMailer.';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Replace the placeholders (smtp.example.com, [email protected], your_email_password, etc.) with your actual SMTP server details and credentials.
Step 6: Test the Configuration
Save the file and exit the editor. To test the set-up, run the PHP script:
php email_test.php
If everything is set-up correctly, you should see:
Message has been sent
Check the recipient’s inbox to ensure the email is delivered. If there are errors, review the error message and adjust the SMTP configuration accordingly.
Step 7: Secure Your Credentials
Storing sensitive information like email credentials directly in your PHP files is not recommended. Use environment variables or a configuration file outside the webroot to secure them. For example:
1. Create a .env file in your project directory:
nano .env
Add your credentials:
SMTP_HOST=smtp.example.com
[email protected]
SMTP_PASS=your_email_password
2. Use a library such as vlucas/phpdotenv to load environment variables in your PHP script:
composer require vlucas/phpdotenv
3. Update your script to use these variables:
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(DIR);
$dotenv->load();
$mail->Host = $_ENV['SMTP_HOST'];
$mail->Username = $_ENV['SMTP_USER'];
$mail->Password = $_ENV['SMTP_PASS'];
Troubleshooting Tips
- Port Issues: If you encounter connection issues, check if the SMTP port (587 or 465) is open on your server.
- Firewall Restrictions: Ensure that your server’s firewall allows outgoing connections to the SMTP server.
- Error Details: Use $mail->SMTPDebug = 2; in the try block for detailed error messages during debugging.
- TLS and SSL: Verify whether your SMTP server requires TLS or SSL and update the $mail->SMTPSecure setting accordingly.
Conclusion
Install PHPMailer in Ubuntu, By following this guide, you’ve successfully installed and configured PHPMailer on Ubuntu. With its powerful features and ease of use, PHPMailer simplifies the process of sending emails in PHP applications. Whether you’re setting up email notifications or newsletters, PHPMailer is a reliable choice. Always remember to secure your credentials and test your setup thoroughly before deploying to a production environment.