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

Juan Guillermo Mariño
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juan Guillermo Mariño
Front End Web Development Techdegree Graduate 19,423 Points

$mail_to php function: not receiving email

Hi guys, I was trying to create a small form on workspaces but the email is not being sent.

here is my html and php:

HTML:

<html>
<head>
</head>
<body>
    <form name="form" action="form_handler.php" method="POST">
       <label class="name">Name</label>
       <br>
       <input name="name" type="text" required="required">
       <br>
       <label class="email">E-Mail</label>
       <br>
       <input name="email" type="email" required="required">
       <br>
       <label class="comment">Comment</label>
       <br>
       <textarea name="comment"></textarea>
       <br>
       <input type="submit" name="submit" value="Subscribe" align="center">
    </form>
</body>
</html>

PHP:

<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['comment'];

$mail_to = 'juangui097@gmail.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the feedback.');
        window.location = 'index.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Something went wrong. Please try again or contact at myPersonalEmail@email.com');
        window.location = 'index.html';
    </script>
<?php
}

?>

4 Answers

Alena Holligan
STAFF
Alena Holligan
Treehouse Teacher

In workspaces, the only email you can send to is “treehouse@localhost”

Frans Teja
Frans Teja
8,175 Points

Are you developing this code on localhost?

Cheers.

Frans Teja
Frans Teja
8,175 Points

If it doesnt output any errors, I assume that it's treehouse server problem. I did this mailing before on wordpress on localhost, and it didnt work. The wordpress php mailer only works when i put on my real website. Btw the $header variable looks strange, doest $header got set correctly?