1

The code "works" as in it gives the correct message when fields are filled out, but it does not send the email. Could this be something I need to set on go-daddy's end? or is the code wrong. I am new to all of this and still learning how everything works.

I am building a site for my wife and am very new, I got this info from a great video on you tube because the classes I am taking have not gotten this far. I am still on CSS and Bootstrap.

I put the php on a page in the root folder, I have double checked the file name. main_parser.php

<?php
if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['p']) ){
    $n = $_POST['n']; 
    $e = $_POST['e'];
    $p = $_POST['p'];
    $to = "redacted@gmail.com";
    $from = $e;
    $subject = 'Contact Form Message';
    $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$p.'</p>';
    $headers = "From: $from\n";
    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    if( mail($to, $subject, $message, $headers) ){
        echo "success";
    } else {
        echo "The server failed to send the message. Please try again later.";
    }
}
?>


--------------------------------------------

<script>
  function _(id){ return document.getElementById(id); }
  function submitForm(){
    _("main-submit").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "n", _("n").value );
    formdata.append( "e", _("e").value );
    formdata.append( "p", _("p").value );
    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "main_parser.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("main-form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("main-submit").disabled = false;
            }
        }
    }
    ajax.send( formdata );
  }
  </script>


--------------------------------------------------------------------

<form id="main-form" onsubmit="submitForm(); return false;">

        <div class="col-lg-4 space label-adj">
          <label for="" class="form-label">JOIN THE CAMPAIGN TODAY!</label>
        </div>


      <div class="form-row">
        <div class="col-lg-4 space">
          <input id="n" type="text" class="form-control" placeholder="Name" required>
        </div>

      </div>
      <div class="form-row">
        <div class="col-lg-4 space">
          <input id="e" type="email" class="form-control" placeholder="E-Mail Address" required>
        </div>

      </div>
      <div class="form-row">
        <div class="col-lg-4 space">
          <input id="p" type="Phone" class="form-control" placeholder="Phone Number" required>
        </div>

      </div>
      <div class="form-row space">
        <div class="col-lg-4">
          <button id="main-submit" type="submit" class="btn btn-dark btn-block submit-b"><span id="status"></span>Submit</button>

        </div>

      </div>
    </form>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • I sure hope you didn't just post someone's *real* email address in a public forum like that... – Niet the Dark Absol May 25 '20 at 21:28
  • @NiettheDarkAbsol thanks, I've just gone ahead and removed it – bear May 25 '20 at 21:31
  • @Saino_Moore does it echo the 'success' message? It could be getting blocked by spam filters at Google Mail's end, there's many things at play here. – bear May 25 '20 at 21:33
  • Where is `$email` set? Empty `From` header might throw a spam filter. – user3783243 May 25 '20 at 21:33
  • I get success message, just no email. I had found the error in $email after I posted here, sorry didn't have time to update before people answered. You guys are amazing. I have it hosted on go-daddy so it's not local. Nothing showing up in the spam folder on the email address either. – Saino_Moore May 25 '20 at 21:43
  • Welcome, bear. First, You've posted your question well, in my opinion. Your question reads well and I like your sample code. I was wondering why you aren't using jquery as well. I am used to seeing something along these lines: {$.ajax({ type: "POST", url: "email.php", data: data, success: function(){ $('.success').fadeIn(1000); } }); } Check out this other StackOverflow article: https://stackoverflow.com/questions/15300470/jquery-ajax-form-using-mail-php-script-sends-email-but-post-data-from-html-fo – Su Llewellyn May 25 '20 at 21:46
  • Thanks Su Llewellyn, I am still learning. Working my way through a course on udemy and have just started Javascript, Jquery is still 6+ hours away, and at my current rate of completion that = 3-4 days at the least. – Saino_Moore May 25 '20 at 21:54

1 Answers1

-1

Your problem is probably somewhere else because ...

mail():

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

php.net

Marco
  • 7,007
  • 2
  • 19
  • 49