www.webdeveloper.com
Results 1 to 13 of 13

Thread: Valid email addresses entered into our form result in PHP not sending email properly

  1. #1
    Join Date
    Dec 2013
    Posts
    23

    Valid email addresses entered into our form result in PHP not sending email properly

    I built a simple form for suggestions from visitors to our website. It allows users to choose whether or not to include a name or email address. If they enter nothing at all (or the email is invalid) I simply insert "anonymous".

    If I enter garbage or nothing at all into the email field, the email is properly sent using "anonymous". If, however, I enter a valid email address then no email is sent. I must have gone over this a hundred times and I just cannot find the error. Any guidance would be greatly appreciated. Thanks.

    HTML Code:
    <form id="contact" method="post">
                  <fieldset> 
                    <textarea rows="3" name="text" id="text" placeholder="Enter your comments here"></textarea>
                    <div class="formgroup">
                    <label>Name (optional):</label>
                    <input name="name" id="name" type="text" placeholder="Name">
                    </div>
                    </br>
                    <div class="formgroup">  
                    <label>Email (required if you would like a response):</label>
                    <input name="email" id="email" type="text" placeholder="Email address"> 
                    </div>
                  </fieldset>
                  <p id="errortext" class="error_text">The comment field is empty. Please enter your comments.</p>
                  <input type="submit" value="Send Message" name="submit" class="btn btn-secondary-dk" >
                </form>
    
    
    <script type="text/javascript">
    	$("#contact").submit(function(e){
    	  e.preventDefault();
    	  var text = $("#text").val();
    	  var name = $("#name").val();
    	  var email = $("#email2").val();
    	  if(!name){
    		name = "anonymous";
    	  };  
      
    	  var dataString = 'name=' + name + '&text=' + encodeURIComponent(text) + '&email=' + email;
    	  
    	  if (text.length > 10){
    	    document.getElementById('errortext').style.display = "none";
    		$.ajax({
    		type: "POST",
    		url: "libs/contact.php",
    		data: dataString,
    		success: function(e){
    		  	$('#basic-modal-content').modal();
    			return false;
    		}
    		});
    	  } else if (text.length < 10) {
    		$('.error_text').fadeIn(1000);
    	  };
    	 
    	  return false;
    	});
    </script>
    
    And this PHP:

    PHP Code:
    // Email Submit
    // Note: filter_var() requires PHP >= 5.2.0

    if ($_POST["email"] = "" || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
      
    $_POST["email"] = "anonymous@anonymous.com";
    }

    if ( isset(
    $_POST['email']) && isset($_POST['name']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
     
      
    // detect & prevent header injections
      
    $test "/(content-type|bcc:|cc:|to:)/i";
      foreach ( 
    $_POST as $key => $val ) {
        if ( 
    preg_match$test$val ) ) {
          exit;
        }
      }
      
    echo 
    $_POST['email'];
      
      
    //send email
      
    mail"janedoe@url.com""Website suggestions from: ".$_POST['name'], $_POST['text'], "From:" $_POST['email'] );
       


  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    661
    How about your first if statement with the single = sign?

  3. #3
    Join Date
    Dec 2013
    Posts
    23
    I can look into it ginerjm, but I can tell you this code does send email if the email field is left blank or the email is constructed in a way that isn't valid. For some strange reason, the only time it fails to work is when the email is valid.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,331
    Try using the function empty() to test if a $_POST value is empty or not.

    $isItEmpty = empty($_POST["email"]);

    the variable will be true or false.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,331
    Also you need to work on security a bit, you should not be saving data in to a $_POST variable.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Yep:
    PHP Code:
    if ($_POST["email"] = "" || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
      
    $_POST["email"] = "anonymous@anonymous.com";

    Because you used "=" instead of "==", the first part of the if condition will set $_POST['email'] to an empty string, which will evaluate to FALSE. I'm guessing that PHP is then first evaluating the filter_var() result, since "||" has precedence over "=", so it will also return false if $_POST['email'] was valid (since you are negating the result). Therefore, a valid email address does not get reset to "anonymous", but ends up being set to "" by the first part of your conditional expression. So changing it to "==" should help, or as mentioned above, using the empty() function could make it clearer and less prone to the same bug (which we've all committed at one time or another ).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Dec 2013
    Posts
    23
    Ahhhhhh yes. Thank you all. Even after ginerjm mentioned it I didn't pick up on the error. Now I see it.

    And that security, yes thanks for the tip. I'll rework this while I'm at it.

    Thank you all again. I really appreciate the help.

  8. #8
    Join Date
    Dec 2013
    Posts
    23
    No matter what I seemed to try last night, so long as email input type="text" then $isItEmpty = empty($_POST["email"]); was true and I could not figure out why the email address disappeared like that. The names match on form and post.

    So, I used a workaround. I changed the email input to type="email" but my understanding is that this is HTML5 only. So, I'd still like to address the fact that the email isn't properly passed then input type="text".

    But that's not all...

    Here's a new issue that I did not notice previously and it's so strange I'm not even sure how to debug it.

    I've tested the form with dozens of email addresses from a variety of real and fictional domains. Most were properly sent. But for some strange reason, any time I use real or fictional emails from @gmail.com or @yahoo.com the emails are not sent. E.g.,

    someperson@wazzup.com
    heyyou@hotmail.com
    perogi@yabazahoo.com
    perogi@yabazahoo.net
    perogi@zebra.net
    janesmith@yahoo.com <-----not sent
    wow@yahoo.org
    bazooka@yahoo.net
    bobjones@gmail.com <----not sent

    Any idea why email addresses from such specific domains would cause an issue like this?

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    661
    can we see you new and improved code??

  10. #10
    Join Date
    Dec 2013
    Posts
    23
    It's nearly identical to the code above with only two changes:

    In the HTML, I changed type to "email":

    <input name="email" id="email" type="email" placeholder="Email address">

    In the PHP, I corrected the == issue you had pointed out yesterday:

    if ($_POST["email"] == "" || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $_POST["email"] = "anonymous@anonymous.com";
    }

    I just don't understand how, given the simplicity of the code, gmail and yahoo email addresses fail to send. Unless I'm using filter_var and FILTER_VALIDATE_EMAIL incorrectly?

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,246
    Have you checked your spam/junk folder on the receiving end?

    You might find you have more consistent results if you always use the same From email address, where that address is a valid email account on the web host, then set the "sender's" address in an additional Reply-To header (separated from the From header with "\r\n").
    PHP Code:
    $headers "From: your.name@your.domain.com\r\nReply-To: ".$_POST['email'];
    mail"janedoe@url.com""Website suggestions from: ".$_POST['name'], $headers); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Dec 2013
    Posts
    23
    I did check the spam folder and nothing has appeared there. It's very strange. I get confirmation that the email was sent but it never shows up in my inbox if the sender's email is either gmail or yahoo. Absolutely every other domain I've tried - real and fictional - work fine.

    This evening I will try your suggestion re: the From address and let you know how that works out. Thanks NogDog

  13. #13
    Join Date
    Dec 2013
    Posts
    23
    Quote Originally Posted by NogDog View Post
    Have you checked your spam/junk folder on the receiving end?

    You might find you have more consistent results if you always use the same From email address, where that address is a valid email account on the web host, then set the "sender's" address in an additional Reply-To header (separated from the From header with "\r\n").
    PHP Code:
    $headers "From: your.name@your.domain.com\r\nReply-To: ".$_POST['email'];
    mail"janedoe@url.com""Website suggestions from: ".$_POST['name'], $headers); 
    Well I am happy to report that this worked like a charm NogDog, thank you for the advice. I wish I could figure out why gmail and yahoo addressed will not work via my form on my host, but this at least allows the form to work for the time being, and that's great.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles