0
votes

I've written a contact form and when I type in 'name, email, and message' I receive the email. However, when I type in the subject it takes me to the 'Thank you' page but I never seem to receive the email.

I've tried changing the different variables in the PHP script but that doesn't work. I can't work out what's stopping the email from being sent. See code below.

<?php 

error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");

if(isset($_POST['submit'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$formcontent = "From: $first_name $last_name \n \n Message: $message";
$to = '[email protected]';
$mailheader = "From: $email \r\n";
mail($to, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! Your kind message has been recieved!";
}
?> 
   <form method="POST" action="mail.php">
                    <div class="form-inline">
                        <label for="Firstname">Name</label>
                        <input type="text" class="form-control" 
                        id="Firstname" name="first_name" placeholder="First Name">  
                        <input type="text" class="form-control" 
                        id="Lastname" name="last_name" placeholder="Last Name">
                    </div>          
                    <div class="form-group">
                        <label for="Email">E-mail</label>
                        <input type="email" class="form-control" 
                        id="Email" name="email" placeholder="Email Address">
                    </div>
                    <div class="form-group">
                        <label for="Subject">Subject</label>
                        <input type="text" class="form-control" 
                        id="Subject" name="subject" placeholder="Let me know what it's about">
                    </div>  
                    <div class="form-group">
                        <label for="Message">Message
                        </label> 
                        <textarea  class="form-control" 
                        id="Message" name="message" rows="6" placeholder="Send me something awesome!"></textarea>
                    </div>  
                    <button type="submit" name="submit" class="btn btn-default">Submit
                    </button>               
                </form> 
2
error_reporting(~1); what's that tilde doing there?Funk Forty Niner
OOPS! That was meant to be error_reporting (-1) the error that is brought up is "undefined index: subject" but I have declared it in the php and in the html. @Fred-ii-clearyb123
Hm... it might be because your input's broken/on separate lines <input type="text" class="form-control" id="Subject" name="subject" placeholder="Let me know what it's about"> - Try putting it in one line; I've seen that happen before. Same thing for your <textarea> and email.Funk Forty Niner
also make sure that you did fill in all the fields.Funk Forty Niner
so what you posted is "all" your code? You didn't leave anything out, right? If you made any changes and didn't upload the new files, then you may have something still in cache (memory). That I've seen happen a lot. When in doubt, use a conditional statement against all your variables/POST arrays or better yet, a ternary operator. There isn't anything else I can add here.Funk Forty Niner

2 Answers

0
votes

I don't see anything wrong with the code you posted that would stop the subject line from being POSTed to php. In other words, based JUST on the code that you posted, php should be able to access

$_POST['subject'];

But just in case, try changing the submit button to an input, like so

`<input type="submit" name="submit" value="Submit">`

Secondly, it may be useful in the future to always have a subject line, even if the person who filled out the form did not provide one. (This can really help you sort out your emails down the road). I would use PHP shorthand assignment operator. It looks like this

$subject = isset($_POST['subject']) ? $_POST['subject'] : "Default Subject Line";

This is the exact same thing as doing this

if(isset($_POST['subject'])){
    $subject = $_POST['subject'];
} else {
    $subject = 'Default Subject Line';
}
0
votes

So Its worked!!!! I think after a while of playing around the emails finally came through. It could be on of two things refreshing the cache on the website and changing the submit button to an input. All in all its still very slow on sending the email but that could be to do with the server. I'll be testing with 100 emails to make sure.