Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

raquel pinto
raquel pinto
12,549 Points

Contact Form was submit successfull, but I did not receive any email on Gmail, not even on span (XAMPP, AJAX, HTML, PHP)

I really need help. It should be a very simple contact form and I can't make it work. My local host (XAMPP) says it was submit successfully, but I do not receive anything on my gmail. I am using html5, ajax, php. If someone has a clue what is wrong..

my html code:

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Website Contact Form</title> <script> function (id){ return document.getElementById(id); } function submitForm(){ _("mybtn").disabled = true; _("status").innerHTML = 'please wait ...'; var formdata = new FormData(); formdata.append( "n", _("n").value ); formdata.append( "e", _("e").value ); formdata.append( "m", _("m").value ); var ajax = new XMLHttpRequest(); ajax.open( "POST", "test.php" ); ajax.onreadystatechange = function() { if(ajax.readyState == 4 && ajax.status == 200) { if(ajax.responseText == "success"){ _("my_form").innerHTML = '<h2>Thanks '+("n").value+', your message has been sent.</h2>'; } else { _("status").innerHTML = ajax.responseText; _("mybtn").disabled = false; } } } ajax.send( formdata ); } </script> </head> <body> <form id="my_form" onsubmit="submitForm(); return false;"> <p><input id="n" placeholder="Name" required></p> <p><input id="e" placeholder="Email Address" type="email" required></p> <textarea id="m" placeholder="write your message here" rows="10" required></textarea> <p><input id="mybtn" type="submit" value="Submit Form"> <span id="status"></span></p> </form> </body> </html>

my php

<?php

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

my xampp config (php.init)

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

; For Win32 only. ; http://php.net/sendmail-from sendmail_from = raqueltravincas@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ; http://php.net/sendmail-path sendmail_path ="\"D:\xampp\sendmail\sendmail.exe\" -t"

my xampp config (sendemail)

smtp_server=smtp.gmail.com smtp_port=587 auth_username=raqueltravincas@gmail.com auth_password=******** (I put my gmail password) force_sender=raqueltravincas@gmail.com