A community of people sharing their web development knowledge.

Generally, your setup would include a form being sent to a PHP script for processing. In this case, there is a basic HTML file, contact_us.html, that makes up a feedback form. You will need to set the action of your form to “email.php” for the PHP script to process. Below is the code you need for the form in your page.

Because we are using the PHPMailer instead of the generic php mail function, we’ll begin to update our “email.php” file. If you look in your PHPMailer folder you’ll see a README file that includes sample PHP code. The sample PHP code should look like below:

The great thing about this example is that it includes comments, which explain what most of the code does.

Final Configuration of the PHPMailer code

We’ve updated the sample code to work with our initial html contact form (first code example on this page). We updated the code to include additional comments, and the final code we used is below.

Note you will also need an additional file for the code below. Upload the PHPMailerAutoload zip file to your cPanel and extract it into your domain root folder. This file was taken from github and is up to date as of 1/13/2015. It is designed to create the proper structure when extracted. If you want the most up to date copy, you can get it from here, but will need to create a lib/PHPMailer directory in your account to upload it to.

[code]<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST[‘email’] ;
$message = $_REQUEST[‘message’] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require(“lib/PHPMailer/PHPMailerAutoload.php”);

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = “mail.example.com”; // specify main and backup server

// $message is the user’s message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST[‘message’] ;
$mail->Body = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
echo "Message could not be sent.

";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo “Message has been sent”;
?>
[/code]
So in the test the user first visits contact_us.html. When they fill in their email address and comment, the information is posted to email.php. email.php calls PHPMailer, and then sends us the data that the user submitted.