Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Paulo Dupke
Courses Plus Student 2,086 PointsHelp - Contact Form
Hello,
I'm trying to do a contact form. The html, css and the Jquery seens to be ok, but when i try to send, this error appears:
PHP Warning: mail() [<a href='function.mail'>function.mail</a>]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\Home\familiadiniz\Web\contact.php on line 22
Anyone could help me?
Thanks
11 Answers

James Barnett
39,199 PointsSorry about that I got confused and only half answered.
You forgot to set the a $from
address in the $header
and then pass $header
to the mail()
function.
Also instead having the script just die, a better way from a UX perspective would be to send the user to an error page, so they can then go back and fix their error.
This should fix you up ...
<?php
$to_email = "email@gmail.com";
$from = "contact@mydomain.com";
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$headers = "From:" . $email;
$subject = "[Contact Form] " . $_POST["subject"];
function isValidEmail($email) { return strpos($email, "@") !== false; }
if($name != "" && $email != "" && $message != "" && isValidEmail($email)) {
$full_message = "Name : ". $name . "\n";
$full_message .= "Email : ". $email . "\n";
$full_message .= "Message :\n\n". $message . "\n";
mail($to_email, $subject, $full_message, $headers);
header("location: Web/contato.html");
exit();
}
die("Your data is invalid.");
?>

Kevin Esther
8,539 PointsAre you on a local host?

Paulo Dupke
Courses Plus Student 2,086 PointsI'm testing on a server.

Paulo Dupke
Courses Plus Student 2,086 PointsI'm testing on a server.

James Barnett
39,199 PointsI'd guess there is a bug in your code near line 22 where you specify the email address this email is coming from and add it to the header.
If you post your PHP mail script to a pastebin, then we can take a look at it.
Alternatively, you can check out this PHP mail script it works.

Paulo Dupke
Courses Plus Student 2,086 Points<?php
$to_email = "email@gmail.com";
$name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"];
$subject = "[Contact Form] " . $_POST["subject"];
function isValidEmail($email) { return strpos($email, "@") !== false; }
if($name != "" && $email != "" && $message != "" && isValidEmail($email)) {
$full_message = "Name : ". $name . "\n";
$full_message .= "Email : ". $email . "\n";
$full_message .= "Message :\n\n". $message . "\n";
mail($to_email, $subject, $full_message);
header("location: Web/contato.html");
exit();
}
die("Your data is invalid.");
?>

James Barnett
39,199 PointsYou never set the my_email
variable.

Paulo Dupke
Courses Plus Student 2,086 Pointswhere?

Paulo Dupke
Courses Plus Student 2,086 PointsThe error message stops.
But i'm not receiving the message.
My html is like that:
<div id="form" class="grid_12 omega"> <h2>Contato</h2> <form action="contact.php" method="post">
<p>
<label for="name">Nome:</label>
<input name="name" id="name" type="text" class="required">
<span>Insira seu nome</span>
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="required">
<span>Insira um email válido</span>
</p>
<p>
<label for="subject">Assunto:</label>
<input name="subject" id="subject" type="text">
<span>Assunto</span>
</p>
<p>
<label for="message">Mensagem</label>
<textarea name="message" id="message" class="required"></textarea>
<span>Escreva sua mensagem</span>
</p>
<p class="submit">
<input type="submit" value="Enviar" class="btn-submit">
</p>
</form>
</div>
And the php is like the one you gave me.

Paulo Dupke
Courses Plus Student 2,086 PointsI send it wrong.
This is the correct.
<div id="form" class="grid_12 omega"> <h2>Contato</h2> <form action="contact.php" method="post">
<p>
<label for="name">Nome:</label>
<input name="name" id="name" type="text" class="required">
<span>Insira seu nome</span>
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="required">
<span>Insira um email válido</span>
</p>
<p>
<label for="subject">Assunto:</label>
<input name="subject" id="subject" type="text">
<span>Assunto</span>
</p>
<p>
<label for="message">Mensagem</label>
<textarea name="message" id="message" class="required"></textarea>
<span>Escreva sua mensagem</span>
</p>
<p class="submit">
<input type="submit" value="Enviar" class="btn-submit">
</p>
</form>
</div>

James Barnett
39,199 PointsWhere is the start of your form, you have no opening form
tag.
Check out this example of a subscribe form on codepen.