1

I want to send an Email using PHP when a contact form is submitted ..

Here's my index.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Simple PHP Contact Form</title>
    <meta name="HandheldFriendly" content="true">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
  <div class="wrapper">  
    <div id="contact_form">
    <form name="form1" id="ff" method="post" action="insert.php">
    <h1>Responsive HTML5 Contact Form Demo</h1>
    <p>This is a working demo of HTML5 and PHP Responsive Contact Form. The form is so simple, that you can implement it in a few minutes.</p>    
        <label>
        <span>Name*:</span>
        <input type="text" placeholder="Please enter your name" name="name" id="name" required autofocus>
        </label>

        <label>
        <span>City*:</span>    
        <input type="text" placeholder="Please enter your city" name="city" id="city" required>
        </label>

        <label>
        <span>Phone:</span>
        <input type="tel" placeholder="Please enter your phone" name="phone" id="phone">
        </label>

        <label>
        <span>Email*:</span>
        <input type="email" placeholder="youremail@gmail.com" name="email" id="email" required>
          </label>

        <input class="sendButton" type="submit" name="Submit" value="Send">

    </form>
    </div>
   </div>

</body>
</html>

Here's my insert.php:

<?php

// Get values from form
$name=$_POST['name'];
$city=$_POST['city'];
$phone=$_POST['phone'];
$email=$_POST['email'];

$to = "mymail@gmail.com";
$subject = "Future Tutorials Contact Form Test";
$message = " Name: " . $name . "\r\n City: " . $city . "\r\n Phone: " . $phone . "\r\n Email: " . $email;


$from = "FutureTutorials";
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; 

if(mail($to,$subject,$message,$headers))
{
  print "Hello";
  // Created by Future Tutorials
}else{
  echo "Error! Please try again.";
}



?>

When I press the submit button it shows the following Error Message

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()

. But I couldn't find the cause of the error here.

The original email I have omitted here. Thanks

User57
  • 2,453
  • 14
  • 36
  • 72
  • look at this [stack here](http://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script). hope it helps – MKAD Mar 01 '17 at 08:24
  • @User57, start removing the @ sign. I just run your code without any issue, so I think the problem is in the configuration of the sendmail more then in you script. Check this discussion if it can help you http://stackoverflow.com/questions/786221/mail-returns-false – Simone Cabrino Mar 01 '17 at 08:33
  • Ok Now I got another Error , `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()` – User57 Mar 01 '17 at 08:38
  • http://stackoverflow.com/questions/19132171/send-email-from-localhost-running-xammp-in-php-using-gmail-mail-server try this in your localhost – ashanrupasinghe Mar 01 '17 at 09:01

1 Answers1

0

what error message it writes? I recommend you to use phpmailer.

sandrop
  • 29
  • 2
  • 7