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" />
</form>

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.

<?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("PHPMailer-master/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 = "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->IsHTML(true);

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

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

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
Name

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?

Comments

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? 

Staff
2,185 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?

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

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: support@WebHostingHub.com 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.