Knowledgebase: Email Help
How to use PHPMailer to send Emails (SMTP Authentication)
Posted by Bangar C, Last modified by Shyam U on 22 November 2017 11:58 PM

What is PHPMailer:

PHPMailer is a code library to send emails safely and easily via php codes from a webserver.

PHPMailer is a PHP class that provides a package of functions to send email . The two primary features are sending HTML Email and e-mails with attachments . You can use any feature of SMTP-based e-mail , multiple recepients via : to , CC , BCC , etc. In short: PHPMailer is an efficient way to send e-mail within PHP .

Why to use PHPMailer:

As you may know, it is simple to send mails with the PHP mail() function . So why use PHPMailer ? Isn’t it slower ? Yes that’s true , but PHPMailer makes it easy to send e-mail , makes it possible to attach files , send HTML e-mail , etc . With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function. Also, the mail() function requires a local mail server to send out emails. PHPMailer can use a non-local mail server (SMTP) if you have authentication.

How do I use phpMailer to Send Email through PHP

 In this article we will walk you through the process of sending mail using phpMailer.

 To begin using PHPMailer, you will first need to download it at the below url.


 After you have downloaded the zip file, you will want to unzip and extract it to your public_html directory within your hosting account.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.

You can create a file called contact.html and save the below code.

<form action="email.php" method="POST">
    <div class="row">
        <label for="name">Your name:</label><br />
        <input id="name" class="input" name="name" type="text" value="" size="30" /><br />
    <div class="row">
        <label for="email">Your email:</label><br />
        <input id="email" class="input" name="email" type="text" value="" size="30" /><br />
    <div class="row">
        <label for="message">Your message:</label><br />
        <textarea id="message" class="input" name="message" rows="7" cols="30"></textarea><br />
    <input id="submit_button" type="submit" value="Send email" />


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
$name = $_REQUEST['name'] ;
$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 = ""; // 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:
// pass: password
$mail->Username = ""; // SMTP username
$mail->Password = "email_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("", "Name");

// 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 ="Name    : {$name}\n\nEmail   : {$email}\n\nMessage : {$message}";

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

echo "Thank you for contacting us. We will be in touch with you very soon.";

Note: Make sure to be place all your file's (Ex: contact.html, email.php, PHPMailerAutoload.php etc..) in same location (Directory).

Comments (2)
01 August 2015 05:17 PM
Thanks for your article! PHPmailer works fine on localhost, but fails to work to upload images using fastwebhost mail server (works ok for text). I get an error "We do not authorize the use of this system to transport unsolicited, 220 and/or bulk e-mail." I'm not sure if it is linked to the failed image upload. Can you point to a tutorial to use the "$mail->addAttachment" or any other method to upload images?
05 November 2015 11:22 PM
tryed the code out but i am having some issues . please would appreciat it if we could communicate further .please do drop your skype
Post a new comment
Full Name:
CAPTCHA Verification 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).