PHP not sending an email to my mailbox, while the onready state change function runs

function _(id) {
    return document.getElementById(id);
}

function submitForm() {
    _("button").disbled = true;
    _("status").innerHTML = "Please wait...";
    var formdata = new FormData();
    formdata.append( "email", _("email").value );
    formdata.append( "subject", _("subject").value );
    formdata.append( "message", _("message").value );
    var ajax = new XMLHttpRequest();
    ajax.open("POST", "send_email.php"); // parameter: method, url, boolean (optional)
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("status").innerHTML = '<p>Thanks your message has been sent successfully.</p>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("button").disabled = false;
            }
        }
    }
    ajax.send(formdata);
}

<?php
    if ( isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["message"]) ) {
        $email = $_POST["email"];
        $subject = $_POST["subject"];
        $message = $_POST["message"];
        
        $to = "myname@gmail.com";
        $from = $email;
        $subject = "Contact From Message";
        $message = "<b>Email Address:</b> ".$email. " <br><b>Subject: </b>  ".$subject." <p> ".$message." </p>";
       
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: $contentType; charset=utf-8\r\n";
        $headers .= "Content-Transfer-Encoding: 8bit\r\n";
        $headers .= "From: =?UTF-8?B?";
        $headers .= "?= <$from>\r\n";
        $headers .= "Reply-To: $replyTo\r\n";
        $headers .= "X-Mailer: PHP/". phpversion();
        
        $confirm = mail($to, $subject, $message, $headers);
        if ($confirm) {
            echo "success";
        } else {
            echo "The server failed to send the message. Please try again later.";
        }
    }
?>


    <form method="POST" id="contact-form" onsubmit="submitForm(); return false;">
      <h2 id="header-contact">Contact</h2>
      <label>
        <span class="contact">Your Email</span>
        <input id="email" class="contact_input" type="text" name="email" placeholder="name@hotmail.com">
      </label> 

      <label>
        <span class="contact">Subject</span>
        <input id="subject" class="contact_input contactinfo" type="text" name="message">
      </label>

      <label>
        <span class="contact">Message</span>
        <textarea id="message" name="comment" class="contact_input contactinfo"></textarea>
      </label>

      <label>
        <input id="button" type="submit" value="SEND">
        <br>
        <span id="status"></span>
      </label>
    </form>

Anybody an idea, what the problem is and how to solve that?

Does your mail server allow you to send emails using the form-fillers email address as the from-address? That means you could send emails as if they were from me, and I’d like to think that a commercial host might not allow that.

Other than that, how far through the code does it get? Presumably the PHP code you show in line with the JS and html code is actually in the send_email.php file?

There is no error showing in the console log and besides the html, js and php are seperated files. How do I allow using the form-filters email address as the form-address?

If the host doesn’t allow if, then you don’t. As you’re sending the email (albeit to yourself) use your email address, and include their email address in the message text, and maybe as a “reply-to” address.

Might not be that, though.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.