This article shows several methods for installing and sending mail with PHP and PHPMailer.
Composer is a dependency manager for PHP that you can use to install packages required by a PHP project.
To install PHPMailer using Composer, follow these steps:
[email protected] [~] cd public_html
To install PHPMailer, type the following command:
[email protected] [~/public_html] composer require phpmailer/phpmailer
To test the installation, follow these steps:
[email protected] [~/public_html] vi mailer.php
Type (or paste) the following code into the editor. Replace the items in red with the actual values for your account and save the file:
<?php use PHPMailer\PHPMailer\PHPMailer; require 'vendor/autoload.php'; $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = gethostname(); $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'password'; $mail->setFrom('[email protected]'); $mail->addAddress('[email protected]'); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the body.'; $mail->send(); ?>
Test the setup by running the file from the command line or visiting the page in your web browser. The command line is shown below.
[email protected] [~/public_html] php mailer.php
The file does not produce any visible output, but a mail is sent to the recipient.
Git is a version-control system that can be also be used to install software.
To install PHPMailer using Git, follow these steps:
[email protected] [~] cd public_html
To install PHPMailer, type the following command:
[email protected] [~/public_html] git clone https://github.com/PHPMailer/PHPMailer.git
To test the installation, follow these steps:
[email protected] [~/public_html] vi mailer.php
Type (or paste) the following code into the editor. Replace the items in red with the actual values for your account and save the file:
<?php use PHPMailer\PHPMailer\PHPMailer; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = gethostname(); $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'password'; $mail->setFrom('[email protected]'); $mail->addAddress('[email protected]'); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the body.'; $mail->send(); ?>
Test the setup by running the file from the command line or visiting the page in your web browser. The command line is shown below.
[email protected] [~/public_html] php mailer.php
The file does not produce any visible output, but a mail is sent to the recipient.
Windows hosting packages do not have a command line, so you must do the installation using the graphical tools in Plesk.
To install PHPMailer using Plesk, follow these steps:
Select Remote at the top of the screen:
In the Remote Git Repository text box, paste the URL for the PHPMailer Git repository (https://github.com/PHPMailer/PHPMailer.git):
Add a destination directory for the PHPMailer files:
Click
To test the installation, follow these steps:
<?php use PHPMailer\PHPMailer\PHPMailer; require 'PHPMailer/src/PHPMailer.php'; require 'PHPMailer/src/SMTP.php'; $mail = new PHPMailer(); $mail->isSMTP(); $mail->Host = 'example.com'; $mail->SMTPAuth = true; $mail->Username = '[email protected]'; $mail->Password = 'password'; $mail->setFrom('[email protected]'); $mail->addAddress('[email protected]'); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the body.'; $mail->send(); ?>
PHPMailer supports many more features than are shown here. For more information about PHPMailer, please visit https://github.com/PHPMailer/PHPMailer.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.