Along with the PHP mail() function and solutions such as FormMail, there is also phpMailer that can be used to send mail through your website. In this article we will walk you through the process of sending mail using phpMailer.

Download the PHPMailer script

to begin using PHPMailer, you will first need to download it. You may locate PHPMailer through this direct download from their page on Github.

After you have downloaded the zip file, you will want to unzip and extract it to your public_html directory within your hosting account. When using cPanel to extract the zip file, it will place all files within a directory labeled PHPMailer-master. Any file paths in this article will reference that directory but feel free to adjust it as you see fit. After the files are uploaded and extracted, you will then edit your site to use the PHPMailer code.

Add an HTML contact form to your page

As an email script won't be very helpful without a method to send the email, you will now create a contact form on your site. The following is an example of a basic contact form that you can place within the desired page to handle anyone who wants to send you mail.

<form method="post" action="email.php">
Email: <input name="email" id="email" type="text" /><br />

Message:<br />
<textarea name="message" id="message" rows="15" cols="40"></textarea><br />

<input type="submit" value="Submit" />

As you can see, the form action is set to email.php. This form action determines how the information that is submitted to the form is processed and determines what happens once the form is submitted.

Adding the form action using PHPMailer

As the information from the form is passed over to the email.php script, we will need to place some code there. Here, we will provide the an example code you can use for your contact form. Be sure to read over the comments and adjust as you see fit.


// $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

$mail = new PHPMailer();

// set mailer to use SMTP

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

$mail->SMTPAuth = true; // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: This email address is being protected from spambots. You need JavaScript enabled to view it.
// pass: password
$mail->Username = " This email address is being protected from spambots. You need JavaScript enabled to view it. "; // SMTP username
$mail->Password = "password"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress(" This email address is being protected from spambots. You need JavaScript enabled to view it. ", "Jeff Matson");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML

$mail->Subject = "You have received feedback from your website!";

// $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;

echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;

echo "Message has been sent";

When a user visits your contact form and sends a message, it will then pass the information over to the email.php file that is defined as the form action, which will then send the email.

PHPMailer has a very large number of options to fit your individual needs. You may want to view more information on particular options on the PHPMailer Github project page.

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?


2012-06-22 9:28 am
It's a very good explanation for a novice like me who was gropping for the right direction. Thanks for posting
n/a Points
2014-12-12 2:32 am

I came to realize that this program often fails to deliver messages when website viewers put gmail/yahoo mail address in the form input. How can I solve this issue? 

2,342 Points
2014-12-12 10:15 am
This can vary based on how the recipient's mail provider is handling mail. Are you getting a bounceback message? Is the mail being delivered to the user's spam folder?
n/a Points
2015-04-17 8:36 pm

As of Apr 2015, this does not work on WebHostingHub.  If you set "$mail->SMTPDebug = 2; ", you will see that the entire transcript of the interaction.  It exactly replicates what you might do by hand successfully with telnet or openssl, but you still get 535 user authentication failures with phpMail.  I don't know why.  Something about the smtp protocol in current use. Here's a workaround: 1) visit - 2) In addition to the displayed code, you will need smtp.php and sasl.php.  You can get them by clicking "Download Script" 3) You will also need an sasl client.  I used "PLAIN" - that client can be found at: 4) Upload your php script, and the three support scripts to the appropriate place (try your webroot as a start) 5) See below code fragment, edited from Step 1 code, verified to work on my WebHostingHub account. // configuration that works for WebHostingHub - as of Apr 2015     require("smtp.php");     require("sasl.php"); //SASL authentication     $smtp=new smtp_class;     $smtp->host_name="";     $smtp->host_port=587;    // not secure - but it's running on the host's server system     $smtp->ssl=0;     $smtp->start_tls=0;        //which ports use which protocols?  I don't know     $smtp->localhost="localhost";     $smtp->direct_delivery=0;     $smtp->timeout=10;     $smtp->data_timeout=0;     $smtp->debug=1;        // if ==1, will show php include path, where they should be                                         //if you don't put the support files in the right place     $smtp->html_debug=1;     $smtp->pop3_auth_host="";     $smtp->user=""; // SMTP Username     $smtp->realm="";     $smtp->password="ObviousPassword"; // SMTP Password for anActiveAccount on WebHostingHub     $smtp->workstation="";     $smtp->authentication_mechanism="PLAIN"; 


Thanks to Brett T. on the support team for providing the link for Step 1.

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Would you like to ask a question about this page? If so, click the button below!

Help Center Search

Current Customers

Email: Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.