0

so I am new to PHP and I am currently making a contact form on a website so that the form answers can be captured and sent to my email address.

I am using JS, PHP and HTML for this, the mail function in PHP seems to send the info and I get all the messages saying 'sent/Yay it worked' but it never gets delivered to my email address and I really do not know why, it does not go to my spam or anything either.

I am so confused! I am using my own personal computer(Mac) and running php through the localhost on MAMP. When going on my terminal and typing 'mail' it says that all these 'messages' were undelivered??? I have also included a section of my php.ini file as I saw somewhere that this needs to be configured, I believe I have done this but it is still not sending the forms message to my email address. I am unsure what to do - please help! This is all very new to me so any help is very much appreciated, code below. Thanks.

Javascript -

$(document).ready(function() {
$("#myForm").validate({
    submitHandler: function() {
        //submit the form
$.post("<?php echo $_SERVER[PHP_SELF]; ?>", //post
            $("#myForm").serialize(), 
            function(data){
              //if message is sent
              if (data == 'Sent') {
                alert("sent it");
                $("#message").fadeIn(); //show confirmation message

            }else{
                alert("not sent");
            }
            //
        });
        return false; //don't let the page refresh on submit.
    }
}); //validate the form
});

PHP -

<?php  

//validate 

$test = mail('myemail@hotmail.co.uk', 'Test Email', 'Hope this works!');
if ( $test ) { echo 'Yay it worked!'; }
else { echo 'Oh no mail failed!'; }

if(isset($_POST['send'])){
echo 'Sent Email!';  

$to="myemail@hotmail.co.uk";
$name=$_REQUEST['firstname'];
$email=$_REQUEST['email'];
$subject="Contact Us";
$body="Name: $name \n\n Email Address: $email \n\n";
$sent=mail($to, $subject, $body);
echo 'Sent'; die;  
} 

?>

HTML -

<div class="container">
    <div style="text-align:center">
        <p>Leave us a message:</p>
    </div>
    <div class="row">
        <div class="column">
            <form name="myForm" action="" onsubmit="return validateForm()" method="post" style="background-color: #6495ED;">
                <label for="fname">First Name:</label>
                <input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:200px"><br>
                <label for="lname">Last Name:</label>
                <input type="text" id="lname" name="lastname" placeholder="Your last name.." style="width:200px"><br>
                <label for="lname">Number:</label>
                <input type="text" id="conNum" name="contactNum" placeholder="Your phone number.." style="width:200px; margin-left: 20px;"><br>
                <label for="lname">Email:</label>
                <input type="text" id="email" name="email" placeholder="Your email address.." style="width:200px; margin-left: 35px;"><br>
                <label for="subject">Subject:</label>
                <textarea id="subject" name="subject" placeholder="Write something.." style="height:80px;"></textarea>
                <input type="submit" value="Submit" name="send">
            </form>
            <div id="message">Thank you for your message</div>
        </div>
    </div>
</div>

PHP.ini File

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = myemail@hotmail.co.uk

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i
Paige
  • 35
  • 5

0 Answers0