0

Actually, I am using LOCALHOST. I've created a form to allow visitors to contact me via email. I am using PHP mail function but it doesn't work. The error is which is mentioned in the else statement.

HTML

<form  id="contact" method="post" action="contact us.php">
        <h1>Contact us</h1>
        <label for="subject">Subject:(required)</label>
        <input type="text" name="subject" placeholder="Enter your subject.." required/></br>
        <label for="email">E-mail:(required)</label>
        <input type="email" name="email" placeholder="Enter your email.." required/></br>
        <label for="website">Website:(optional)</label>
        <input type="url" name="website" placeholder="Enter your website.."/></br>
        <label for="message">Message:(required)</label>
        <textarea placeholder="Enter your message.." required="required" rows="12" name="message"></textarea></br>
        <input type="submit" name="send" value="Send message"/>
    </form>

PHP

 <?php
if(isset($_POST['send']))
{
$to = "some@email.com";
$from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$header = "From:".$from;
    mail($to, $subject, $message, $header);
    if(mail($to, $subject, $message, $header))
    {
        echo "<script>alert('mail was sent successfully!');</script>";
    }
    else
    {
        echo "<script>alert('mail was not sent!');</script>";
    }
}
?>

The main error which appears in the browser is:

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:\xampp\htdocs\PCzone\contact us.php on line 268
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 2
    You are calling `mail()` twice. – Ivar Jun 02 '16 at 09:14
  • 2
    Check this link it may help you http://stackoverflow.com/questions/19132171/send-email-from-localhost-running-xammp-in-php-using-gmail-mail-server – Amit Ray Jun 02 '16 at 09:20
  • Please take a look into your http servers error log file. That is where you can actually _read_ what exactly the issue is instead of having to _guess_. – arkascha Jun 02 '16 at 09:20
  • As per your answer (which should have been an edit) you don't appear to have an SMTP server installed on your localhost, which makes me think you're running this on Windows. Either update you php.ini to point at an actual SMTP server, or install one. – Jonnix Jun 02 '16 at 09:25

3 Answers3

0

you have to setup mail server on your local machine or use smtp lib to send email OR just install Sendmail on your local machine then change config with your SMTP detail

smtp_server=smtp.gmail.com
smtp_port=465
auth_username=user@gmail.com
auth_password=your_password
Saurabh Chandra Patel
  • 12,712
  • 6
  • 88
  • 78
0

If your trying to work on local you have do some settings.Code seems to be perfect please check one on server. I hope it will work also check on spam and trash too.

srinivas
  • 109
  • 12
-1

I have change settings in

php.ini

Now code runs successfully .

Sabby
  • 403
  • 3
  • 15