This article demonstrates how to install PHPMailer and use it to send e-mails. With PHPMailer, you can send e-mail messages programmatically using PHP.
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:
user@server [~] cd public_html
To install PHPMailer, type the following command:
user@server [~/public_html] composer require phpmailer/phpmailer
To test the installation, follow these steps:
user@server [~/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.
user@server [~/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:
user@server [~] cd public_html
To install PHPMailer, type the following command:
user@server [~/public_html] git clone https://github.com/PHPMailer/PHPMailer.git
To test the installation, follow these steps:
user@server [~/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.
user@server [~/public_html] php mailer.php
The file does not produce any visible output, but a mail is sent to the recipient.
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.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.