简体   繁体   中英

Simple contact form won't work - php

Again - I've looked and troubleshooted for about an hour and cannot get the php contact form to work. I get the "Thank you!" email, but I haven't received the email to my inbox ever. I've done it about 20 times.

<div class="container">
<div class="row">
    <div class="col-lg-12">
      <h5>Want to contact me quickly? Use this form!</h5>
      <form id="contact" method="post" action="submit.php" class="form" role="form">
      <div class="row">
        <div class="form-group col-lg-4">
          <label>Name</label></br>
          <input type="text" name="name" placeholder="Enter your full name" required autofocus>
        </div>
        <div class="form-group col-lg-4">
          <label>Email address</label></br>
          <input type="email" name="email" placeholder="Enter your email address" required autofocus>
        </div>
        <div class="form-group col-lg-4">
          <label>Subject</label></br>
          <input type="text" name="subject" placeholder="Subject">
        </div>
        <div class="form-group col-lg-12">
          <label>Message</label>
          <textarea name="comments" name="message" data-provide="markdown-editable" rows="6" placeholder="Let's chat!" style="width:100%"></textarea>
        </div>
        <div class="form-group col-lg-12 form-action">
          <input type="hidden" name="Submit" value="contact">
          <button type="submit" class="btn btn-default">Submit</button>          
        </div>
    </form>
    </div>
</div>

PHP:

<?php
if ($_POST['Submit'])
{
$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$comments=$_POST['comments'];
$to='MY EMAIL ADDRESS';
$from='website';

if (mail($to, $subject, $name, $email, $comments))
{
    echo 'Thank you!';
}
else
{
    echo 'Something went wrong! Try again';
}
}?>

Any help is greatly appreciated. I have my MAMP server on and I get the "thank you!" message every time, but I haven't gotten any emails. Also, I'd really like it to show the "thank you!" and then return to my webpage, but I can't even get the php to work so I maybe should hold off on that part.

You have a wrong call of mail() function. It should be like this:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

So, for your case:

<?php
if ($_POST['Submit'])
{
    $name=$_POST['name'];
    $email=$_POST['email'];
    $subject=$_POST['subject'];
    $comments=$_POST['comments'];
    $to='email@email.com';
    $from='website';

    $message = 'Message: ' . $comments . "\r\n";
    $message .= 'From: ' . $from;

    $headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";

    if (mail($to, $subject, $message, $headers))
    {
        echo 'Thank you!';
    }
    else
    {
        echo 'Something went wrong! Try again';
    }
}
?>

The mail() function simply returns true if the message has been queued without error in your system... but it doesn't tell you whether the email was actually sent or not. I see you're using Mac OS X, so you'll have to configure SMTP settings (provided by your ISP) in the PHP environment to get mail sending working.

Alternately, you can use a service like Amazon SES to send emails through PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM