Introduction
This guide builds on the basics outlined in a previously written guide:
Using the PHPMailer library to send messages via SMTP
In an effort to provide a simple and concise guide here, we've left out all of the basics of using PHPMailer. Please review the guide above if you are not already familiar with the basics of using PHPMailer.
Procedure
This guide assumes that you have downloaded the PHPMailer library to the following directory. Please note that CPANELUSERNAMEHERE represents the cPanel username of your own cPanel user and should not be used literally on your own server.
/home/CPANELUSERNAMEHERE/PHPMailerTest
This guide also assumes that you are logged in as the cPanel user via the Terminal icon in cPanel or via SSH. Please do not ever execute PHP scripts that are intended to be used in a cPanel account as the root user, as this is a major security risk for the entire server.
-
With the PHPMailer library in the location mentioned above, you would copy the example form into your domain's document root. In this example, we will assume we are using the Primary Domain of the cPanel account, so the document root is the public_html directory.
cp -v ~/PHPMailerTest/examples/contactform.phps ~/public_html/testForm.php
-
Double-check the location of the files to make sure that you are using the correct path with the following command:
find ~/ -name PHPMailer.php -or -name Exception.php -or -name SMTP.php
-
Open the testForm.php script with the text editor of your choice and find the "require" statement, which looks like this:
require '../vendor/autoload.php';
-
Remove the "require" statement from step 2 and replace it with the following "require" statements. Be sure to use the full path to the files from Step 2 to avoid problems.
require '/home/CPANELUSERNAMEHERE/PHPMailerTest/src/Exception.php';
require '/home/CPANELUSERNAMEHERE/PHPMailerTest/src/PHPMailer.php';
require '/home/CPANELUSERNAMEHERE/PHPMailerTest/src/SMTP.php'; -
Update the email addresses used within the example contact form to the appropriate ones. For simplicity, this guide will use one email address throughout, and it is recommended it be an email address hosted as part of the cPanel account. You should also have the password for that email address ready to add to the script.
The following command will replace all required email addresses with whatever Email address you use for MYEMAILADDRESS@MYDOMAIN.TLD below. This will also create a backup of the testForm.php file at ~/public_html/testForm.php.bak so that you can revert the change if it doesn't do what you want or what you were expecting.
sed -i.bak 's/[a-z]\+@example.com/MYEMAILADDRESS@MYDOMAIN.TLD/g' ~/public_html/testForm.php
-
Determine the domain to use for the SMTP host. We will use the server's hostname for this guide because this is the most reliable option. Use the following command and note down the domain it returns:
hostname
-
Locate the following lines within the testForm.php script:
$mail->isSMTP();
$mail->Host = 'localhost';
$mail->Port = 25; -
Replace those lines with the following:
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'HOSTNAME.GOES.HERE'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'MYEMAILADDRESS@MYDOMAIN.TLD'; // SMTP username
$mail->Password = 'EMAIL-ACCOUNT-PASSWORD-GOES-HERE'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect toBe sure to replace the Email address and hostname with your own. Also, disable the SMTPDebug option before using the form in production. Leaving the debug option on in production is a security hazard.
-
Before proceeding, take note of where the script will log errors. The place in which any PHP errors will be logged depends on the PHP your server uses. The following command tails both the main Apache error log and the location where a local error log may be, depending on your type of PHP. This command will produce an error if the local error log doesn't exist, but you can ignore that.
tail -fn0 /etc/apache2/logs/error_log ~/public_html/error_log
You may also want to check the ~/logs directory if the above command doesn't show you any error logs and the script is producing a 500 error when you test it. The ~/logs directory is used when your server is making use of PHP-FPM, and the specific log name includes your domain which is why it was not added to the command above.
-
Test the script by visiting it directly in your browser. If you've set up the script in the public_html directory as outlined in this guide, you'd use your primary domain like this:
https://myprimarydomain.tld/testForm.php
Fill out the form and then submit it. You'll find at the top of the resulting page a large amount of debug output that may help you to determine if there is a problem with the SMTP transaction and then a message with the following if the test was successful:
Message sent! Thanks for contacting us.
Common Issues
You may receive an error when using this a form like this to send mail through a remote server:
503 AUTH command used when not advertised
This error indicates that you are making a connection to the remote server without using TLS or SSL, and the remote server does not allow insecure connections. Make sure you have enabled the appropriate security options in the script when connecting to the remote server. In the example above, we use TLS to make the connection and use the secure SMTP port:
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect to
Comments
0 comments
Article is closed for comments.