-2

I redesigned my website and I am working on getting the forms to work as the final step on my site. But for some reason I am using the same code on my site and it is not working, I dont know what the problem is.

$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];

$mail_to = 'myemail';
$subject = 'Site Mail';

$body_message = 'From: '.$field_name."\n";
$body_message .= 'Email: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

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

$mail_status = mail($mail_to, $subject, $body_message, $headers);

and the form is

         <form name="myform" action="success.php" method="post" enctype="multipart/form-data">
        <input type="text" placeholder="Your Name *" name="name" size="100" required="">
        <article>
        <input type="text" placeholder="Your Email *" name="email" size="30" required="">
        </article>
        <article>
        <textarea placeholder="Your Message" name="message" cols="40" rows="3" required=""></textarea>
          <div class="btn-wrap  text-center">
        <a href="javascript: submitform()">
            <button class="btn-gray btn-mod btn-w btn-medium btn-rounded" id="submit" name="submit" type="submit">Send Message</button></a>
        </div>
         </article>

         </form>
Jordan
  • 1
  • are you on localhost – lyndact Nov 27 '15 at 02:19
  • Possible duplicate of [Troubleshooting PHP Mail](http://stackoverflow.com/questions/1658043/troubleshooting-php-mail) – Tristan Nov 27 '15 at 02:21
  • $headers = 'From: '.$field_email."\r\n"; won't get you there these days - you need to have your sender email "From" as an address on your site - see just posted http://stackoverflow.com/questions/33948622/php-mail-function-server-and-localhost-not-working/33948920#33948920 – Steve Nov 27 '15 at 02:24

2 Answers2

0

If you are running this code on your local machine, then you need to have a mail server installed and configured with PHP to send mail. It may be worth looking at PHP Mailer http://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail

reayn3
  • 363
  • 5
  • 16
0

If you are running this on Windows then you definitely need a mail transfer agent to send mail. Running on Linux, that shouldn't be necessary. Even when it does send however getting the message past spam filters it may encounter can be tricky.

There is another problem with your code however that is more serious. Just as with code which handles SQL there is a risk of code injection. Never use user input in mail headers unfiltered. I would recommend reading up on mail injection. I also recommend PHPMailer.

An artical I wrote on the subject might be of some help to you https://facebook.com/notes/coder-central/code-injection-user-input-and-parseable-code-the-dangers/787127538029773/

Chris Root
  • 617
  • 6
  • 16