-4

I can't seem to get the form to send a message to my email. Is it maybe due to the server? Here's my code:

<?php
$action=$_REQUEST['action'];
if ($action=="")    /* display the contact form */
    {
    ?>
                <form id="contact-form" action:"" method="post" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-md-6">
                        <div class="form-group">
                        <input type="hidden" name="action" value="submit">
                            <label for="name">
                                Name</label>
                            <input type="text" class="form-control" id="name" placeholder="Enter name" required />
                        </div>
                        <div class="form-group">
                            <label for="email">
                                Email Address</label>
                            <div class="input-group">
                                <span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span>
                                </span>
                                <input type="email" class="form-control" id="email" placeholder="Enter email" required /></div>
                        </div>
                        <div class="form-group">
                            <label for="subject">
                                Subject</label>
                            <select id="subject" name="subject" class="form-control" required="required">
                                <option value="na" selected="">Choose One:</option>
                                <option value="General">General</option>
                                <option value="Hiring">Hiring</option>
                                <option value="My Work">My Work</option>
                            </select>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="message">
                                Message</label>
                            <textarea name="message" id="message" class="form-control" rows="9" cols="25" required
                                placeholder="Message"></textarea>
                        </div>
                    </div>
                    <div class="col-md-12">

                        <button type="submit" class="btn btn-skin pull-right" id="btnContactUs">
                            Send Message</button>
                    </div>
                </div>
                </form>
                  <?php
    } 
else                /* send the submitted data */
    {
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $subject=$_REQUEST['subject'];
    $message=$_REQUEST['message'];
    if (($name=="")||($email=="")||($subject=="")||($message==""))
        {
        echo "All fields are required.";
        }
    else{        
        $from="From: $name, $email";
        $subject="Message sent using your contact form";
        mail("nilsbittmannmyers@yahoo.co.uk", $subject, $message, $from);
        echo "Message sent!";
        }
    }  
?>

THANKS :) Im using byet as a web host by the way, I think they have PHP enabled in the free service, excuse me, I'm a bit of a noob when it comes to coding, to say the least XD XD

  • You should use a service like mailchimp or postmarkapp to send mail. Aside that, you're passing only From: $name, $email as a header. You are undoubtedly getting caught in a spam filter by doing this since the domains on the sending mail likely don't match the outgoing server. Use Reply-To instead, and either pass full mail headers or use swiftmailer to do it for you. – Kai Qing Sep 11 '14 at 21:24
  • 1
    `mail()` not sending has usually little to do with the surrounding code, but with the server configuration. Even if you provided actual details on that, there's no help available here. Investigate PHPMailer/SwiftMailer as [typically advised](https://www.google.com/search?q=site:stackoverflow.com+php%20mail%20function%20doesn't%20work&gws_rd=ssl). – mario Sep 11 '14 at 21:24
  • Also, check isset on all request fields. look into trim() as well. Lots to do here to clean this whole thing up. – Kai Qing Sep 11 '14 at 21:25

1 Answers1

2

You're using a colon action:"" instead of an equal sign which should be action="" which is the main issue in your code.

You're also missing a name attribute for the email and name input fields.

I.e.: name="email" and name="name"

Also, the From: should initially be an email, and not a name as you have in
$from="From: $name, $email";

It's best to use something like this:

$from = "From: ". $name . " <" . $email . ">\r\n";

as your header's 4th parameter.

That way you will have a proper From Email with the person's name appearing in the Email also.

Using $from="From: $name, $email"; will most likely end up in Spam or rejected altogether.

Consult the PHP.net Website for details on mail() and headers:

a few headers options:

$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";

in your case would be:

$from  = "From: ". $name . " <" . $email . ">\r\n";
$from .= 'Reply-To: ' . $email . "\r\n";
$from .= 'Return-Path: ' . $email . "\r\n";

Sidenote:

I noticed you are using enctype="multipart/form-data"

Unless you're wanting to attach/upload a file, it isn't required for what you're using your form as, it's safe to remove it.


Footnotes:

Using error reporting would have signaled these errors, including Undefined index... warnings:

error_reporting(E_ALL);
ini_set('display_errors', 1);

placed just beneath your opening <?php tag.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141