0

I created a form and php to send form details to my email. But when run the web pages via wampserver through localhost it displays an error as "Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\My original web\PHP\ContactUs.php on line 22". I can't find where the error in line 22.

<div class="container">
  <form name="htmlform" method="POST" action="../PHP/ContactUs.php">
  
    <label for="name">Name</label>
    <input type="text" id="name" name="name" placeholder="Your Name">

    <label for="email">Email Address</label>
    <input type="text" id="email" name="email" placeholder="Your Email Address">

     <label for="phone">Phone Number</label>
    <input type="text" id="phone" name="phone" placeholder="Your Phone Number">

    <label for="Message">Message</label>
    <textarea id="message" name="message" placeholder="Write You Something" style="height:200px"></textarea>

    <input type="submit" value="Submit">
  </form>
  
  <?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "pinkmaidbeautysalon@gmail.com";
   $name = $_REQUEST['name']; 
  $email = $_REQUEST['email'];
  $phone = $_REQUEST['phone'];
   $message = $_REQUEST['message'];
  
  //send email
  mail($admin_email, "$name", "$phone", "$message", "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 
  
<?php
  }
?>
Iresha Shyamean
  • 61
  • 1
  • 3
  • 12
  • 1
    Rather than using `$_REQUEST` ( which will work with both $_POST & $_GET methods ) it would be better to use $_POST. The error suggests that you are trying this on your development systm and do not have a mailserver running – Professor Abronsius May 27 '17 at 06:20
  • @RamRaider how to set a mailserver? – Iresha Shyamean May 27 '17 at 06:21
  • That is a fairly broad subject and you have to ask whether or not it is worth the time and effort of setting up your own mailserver on your development system when the php script is going to be hosted on the internet later anyway? I would add some logic to the php code to check if you are running on `localhost` and if so to not send the email. Alternatively, try googling for `"free mail server for windows"` or have a look at https://stackoverflow.com/questions/16830673/wamp-send-mail-using-smtp-localhost ~ there are lots of solutions there – Professor Abronsius May 27 '17 at 06:26
  • @RamRaider without using mail server, is it unable to send details to email? – Iresha Shyamean May 27 '17 at 06:35
  • Yes - you need a mailserver to send emails – Professor Abronsius May 27 '17 at 06:44

2 Answers2

0

the best option for you is use PHPmailer this will allow you to send the mail properly and will save your lots of time.
How To Use PHPMailer this tutorial will guide you.

Satish51
  • 119
  • 6
0

You should use headers. And you can use any third party server test tool to test the mail in localhost. Eg :- "test mail server tool"

    <?php

    $admin_email = "pinkmaidbeautysalon@gmail.com";
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['email'];
    $phone = $_REQUEST['phone'];
    $message = $_REQUEST['message'];
        if(!empty($email))
        {
            $headers = 'From:'.'pinkmaidbeautysalon@gmail.com' . "\r\n" .
                'Reply-To: Pink maid beauty saloon <pinkmaidbeautysalon@gmail.com>' . "\r\n" .
                'X-Mailer: PHP/' . phpversion() . "\r\n" .
                'Content-type: text/html; charset=iso-8859-1';

            //mail (from,subject,body,headers)
            mail($admin_email,"Your saloon", "$name "."$phone "."$message", $headers);
            echo "Email successfully sent";


        }
        mysqli_close($con);

    ?>
varman
  • 8,704
  • 5
  • 19
  • 53