How to Send Email from Localhost in PHP

How to Send Email from Localhost in PHP

I will explain how to send email from localhost in PHP, So Email functionality is very important almost every web based application, web project, so web developers checked  the email functionality on local server like XAMPP, WAMP or any other local server before uploading email functionality on the live server.

There are many way you can send the email in php by using PHPMailer and php mail() function but in this article, i will show you how to send email from localhost in php by using PHPMailer. You can use any local server like, XAMPP, WAMP etc., but i am using XAMPP server as a localhost server.

In this article need followings.
  • PHPMailer File
  • email.php(HTML Form)
  • sendemail.php(PHP Code), also
  • SMTP server of any of your mail accounts like gmail, yahoo, outlook, hotmail, etc.(but in this article, i am using gmail SMTP server) 

Follows These Steps

Step 1.   Download PHPMailer
     Go to browser and type phpmailer,  you can download from any link but, I will provide one of the link screen shown below

Send Email from Localhost in PHP

Step 2.   Now click Clone or download after clicked you can see two options one is Open in Desktop and another one is Download ZIP, screen shown below

Send Email from Localhost in PHP

Step 3.  Now  you can click Download ZIP

Now go to downloaded file(PHPMailer-master) and extract that file, copy the extracted file and paste in project root folder for example my project root folder is tutorials, path like C:\xampp\htdocs\tutorials\PHPMailer-master

Step 4.   Create email.php File

    <!DOCTYPE html>
    <title>Sending Email using PHP</title>
    <fieldset style="width: 300px;">
         <legend>Contact Us</legend>
         <form action=" sendemail.php" method="post">
         <table border="0" cellspacing="5" cellpadding="5">
           <td>Email To:</td>
           <td> <input type="text" name="mailto" id="mailto" required /></td>
           <td><input type="text" name="subject" id="subject" required /></td>
           <td><textarea id="message" name="message" rows="4" style="width: 166px;" required></textarea></td>
            <td><input type="submit" name="Submit" value="Send"></td>

Step 5.   Create sendemail.php File

    include 'PHPMailer-master/PHPMailerAutoload.php';

    $mail = new PHPMailer;

    $mail->isSMTP();                           // Set mailer to use SMTP
    $mail->Host = '';    // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;               // Enable SMTP authentication
    $mail->Username = '';    // SMTP username
    $mail->Password = '*******'; // SMTP password
    $mail->SMTPSecure = 'ssl';      // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                  // TCP port to connect to

    $mail->setFrom('', 'gowebtuts');
    $mail->addReplyTo('', 'gowebtuts');
    $mail->addAddress('');   // Add a recipient
    $mail->addCC('');             //Add cc Address
    $mail->addBCC('');         //Add bcc Address

    $mail->isHTML(true);  // Set email format to HTML
    $mail->Subject = $_POST['subject'];
    $mail->Body     = $_POST['message'];
    $mail->addAddress($_POST['mailto']);   // Add a recipient

        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        echo 'Message has been sent';


  • Include PHPMailer-master/PHPMailerAutoload.phpat
  • Username section put the correct gmail id
  • Password section put the correct password
  • Put correct email id at XYZ
  • Put correct cc and bcc address.
  • SMTPSecure section put ssl and port section put 465 also you can change SMTPSecure Section tls but make sure also changed in port section put 587.

Step 6.   Now go to browser and type http://localhost/tutorials/mail.php, screen shown below

Send Email from Localhost in PHP

then put value in the required fields, screen shown below

Send Email from Localhost in PHP

Now click Send button, screen shown below

Send Email from Localhost in PHP

Step 7.   Now just check email is sended or not, login gmail account and check email, screen shown below

Send Email from Localhost in PHP

So Email is Successfully Send.

                 I hope, it helped you to understand, How to send email from localhost in PHP......Thanks


Hi...I am Md Sahjad Karim and I am Engineering Graduate (B.E in Computer Science and Engineering) working as a Web Developer and write Blog Articles. was launched on july 2016 to provide free online hints, tips, guides and tutorials related to technology(PHP, CodeIgniter, WordPress, Blogger etc). My primary goal is to provide the good, quality and easily to understand the articles to readers around the world.
    Blogger Comment
    Facebook Comment


Unknown said...

you can visit here for the help.

Unknown said...

Message could not be sent.Mailer Error: SMTP connect() failed.

Post a Comment

Thanks For Visiting Blog...