0

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@ADDRESS.DONTSHOW';
$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> 
clearyb123
  • 17
  • 6
  • `error_reporting(~1);` what's that tilde doing there? – Funk Forty Niner Sep 28 '16 at 16:01
  • 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 Sep 28 '16 at 16:08
  • Hm... it might be because your input's broken/on separate lines `` - Try putting it in one line; I've seen that happen before. Same thing for your ` – Funk Forty Niner Sep 28 '16 at 16:09
  • also make sure that you did fill in all the fields. – Funk Forty Niner Sep 28 '16 at 16:10
  • Okay so I've edited on to the same line and still no email. I'm receiving the – clearyb123 Sep 28 '16 at 16:15
  • I've had this problem if an input field gets disabled in JavaScript after the input somewhere. I'm guess that you aren't using AJAX since the form is using the POST method. Add this code at the TOP of the PHP, and tell me what you get echo(var_dump($_POST['subject'])); exit(); – Native Coder Sep 28 '16 at 16:15
  • if you're using what @NativeCoder wrote, than that would explain it. I am unable to reproduce with what you posted. – Funk Forty Niner Sep 28 '16 at 16:17
  • @NativeCoder I get the message NULL – clearyb123 Sep 28 '16 at 16:18
  • Okay thanks for your help @Fred-ii- and no I am not using AJAX. – clearyb123 Sep 28 '16 at 16:19
  • 1
    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 Sep 28 '16 at 16:23
  • One thing that may help you in the future is the shorthand assignment operator. I'll post an "answer" with some code. I know it doesn't solve the issue that your POSTed variable is undefined, but It really will help down the road – Native Coder Sep 28 '16 at 16:27

2 Answers2

0

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';
}
Native Coder
  • 1,792
  • 3
  • 16
  • 34
  • Hi guys! So i've been working on it for awhile adding in your suggestions and changing things around and sometimes i'm not receiving emails even when I add first names and last names with out subjects / with messages. Very confused person right now!! Obviously not going to give up but do you think it could be server related? My site is www.benjackcleary.com if you want to check it out. Thanks for all your help – clearyb123 Sep 29 '16 at 00:12
  • Could it be that your email client is sending the emails to Spam because the subject line is blank? (I just filled out the form both with and without a subject line, lemme know how it goes) – Native Coder Sep 29 '16 at 13:59
0

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.

clearyb123
  • 17
  • 6