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

Help - 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
James Barnett
39,199 Points

Sorry 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.");

?>

Are you on a local host?

I'm testing on a server.

http://familiadiniz.com.br/contato.html

I'm testing on a server.

http://familiadiniz.com.br/contato.html

James Barnett
James Barnett
39,199 Points

I'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.

<?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
James Barnett
39,199 Points

You never set the my_email variable.

where?

The 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.

I 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
James Barnett
39,199 Points

Where is the start of your form, you have no opening form tag.

Check out this example of a subscribe form on codepen.