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 Build a Simple PHP Application Wrapping Up The Project Sending The Contact Form Email

Ader Reynoso
Ader Reynoso
4,968 Points

Email error

I really like this course! I will appreciate your help in this: Why when I send the email form I received this message: "There was a problem sending the email: Could not instantiate mail function." What else do I need to do?

Thanks in advance for your help!

Calvin Nix
Calvin Nix
43,828 Points

Could you provide the code you are using?

Ader Reynoso
Ader Reynoso
4,968 Points

Thanks for your help!

Im receiving this message: Hay un error No se puede mandar el email Message body empty

this is my code: <?php if($_SERVER["REQUEST_METHOD"] =="POST"){ $name = trim ($_POST['name']); $email = trim ($_POST['email']); $message = trim($_POST['message']); if ($name =="" OR $email =="" OR $message ==""){ echo "Por favor tienes que poner tu Nombre, tu E-mail y tu Mensaje,"; exit; } foreach($_POST as $value) { if(stripos($value, 'Content-Type:') !== FALSE ) { echo "Existe algún problema con la información que acabas de mandar"; exit; } } if ($_POST ["address"] != "") { echo "Hay un error en la información que acabas de mandar"; exit; } require_once("inc/phpmailer/PHPMailerAutoload.php"); $mail = new PHPMailer(); if(!$mail->ValidateAddress($email)){ echo "Por favor tienes que poner un correo valido"; exit; }

$email_body = "";
$email_body = $email_body ."Name: " .$name ."<br>";
$email_body = $email_body ."Email: " .$email ."<br>";
$email_body = $email_body ."Mensaje:" .$message. "\n";




//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "aderreyra@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "here my pass";

//Set who the message is to be sent from
$mail->setFrom($email, 'First Last');

    //Set who the message is to be sent to
$mail->addAddress("aderreyra@gmail.com", 'Ader Reynoso');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';



    if(!$mail->Send()) {
      echo "Hay un error No se puede mandar el email " . $mail->ErrorInfo;
      exit;
    }

header("Location: contact.php?status=thanks");
exit;

} ?> <?php $titleName = "Contacto"; $section = "contact"; include('inc/header.php'); ?>

<div class="section page"> <div class="wraper">

    <h1>Contact</h1>
    <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {?>
    <p>Thanks for the Email.I&rsquo;ll be in touch Shortly.</p>
        <?php } else{ ?>


            <p>I&rsquo;d love to hear from you! Complete the form to send me an e-mail.</p>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                    <table>
                        <tr>
                            <th>
                        <label for="name">Name</label>
                            </th>
                            <td>
                        <input type="text" name="name" id="name">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="email">E-mail</label>
                            </th>
                            <td>
                        <input type="text" name="email" id="email">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="message">Message</label>
                            </th>
                            <td>
                        <textarea name="message" id="message"></textarea> 
                            </td>
                        </tr>
                        <tr style="display:none;">
                            <th>
                        <label for="address">Address</label>
                            </th>
                            <td>
                        <input type="text" name="address" id="address">
                        <p>"Si eres humano por favor no llenes esta información"</p>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Send">
                </form>
            <?php } ?>  
</div>

</div> <?php include('inc/footer.php'); ?>

3 Answers

I'm not sure if this is your problem without seeing your code but If you're on localhost you will need to configure an SMTP server to send the email. If you don't have an SMTP server running on localhost you can configure your mail to send through an external service like gmail by using this PHPMailer example - https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Ader Reynoso
Ader Reynoso
4,968 Points

Hi Friend! I tried the code you send me I received this message:" Hay un error No se puede mandar el email Message body empty" Please if you can help me I'll apprecite a lot your help. This is mi code: <?php if($_SERVER["REQUEST_METHOD"] =="POST"){ $name = trim ($_POST['name']); $email = trim ($_POST['email']); $message = trim($_POST['message']); if ($name =="" OR $email =="" OR $message ==""){ echo "Por favor tienes que poner tu Nombre, tu E-mail y tu Mensaje,"; exit; } foreach($_POST as $value) { if(stripos($value, 'Content-Type:') !== FALSE ) { echo "Existe algún problema con la información que acabas de mandar"; exit; } } if ($_POST ["address"] != "") { echo "Hay un error en la información que acabas de mandar"; exit; } require_once("inc/phpmailer/PHPMailerAutoload.php"); $mail = new PHPMailer(); if(!$mail->ValidateAddress($email)){ echo "Por favor tienes que poner un correo valido"; exit; }

$email_body = "";
$email_body = $email_body ."Name: " .$name ."<br>";
$email_body = $email_body ."Email: " .$email ."<br>";
$email_body = $email_body ."Mensaje:" .$message. "\n";




//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "aderreyra@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "***********"; I put my password here

//Set who the message is to be sent from
$mail->setFrom($email, 'First Last');

    //Set who the message is to be sent to
$mail->addAddress("aderreyra@gmail.com", 'Ader Reynoso');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';



    if(!$mail->Send()) {
      echo "Hay un error No se puede mandar el email " . $mail->ErrorInfo;
      exit;
    }

header("Location: contact.php?status=thanks");
exit;

} ?> <?php $titleName = "Contacto"; $section = "contact"; include('inc/header.php'); ?>

<div class="section page"> <div class="wraper">

    <h1>Contact</h1>
    <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {?>
    <p>Thanks for the Email.I&rsquo;ll be in touch Shortly.</p>
        <?php } else{ ?>


            <p>I&rsquo;d love to hear from you! Complete the form to send me an e-mail.</p>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                    <table>
                        <tr>
                            <th>
                        <label for="name">Name</label>
                            </th>
                            <td>
                        <input type="text" name="name" id="name">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="email">E-mail</label>
                            </th>
                            <td>
                        <input type="text" name="email" id="email">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="message">Message</label>
                            </th>
                            <td>
                        <textarea name="message" id="message"></textarea> 
                            </td>
                        </tr>
                        <tr style="display:none;">
                            <th>
                        <label for="address">Address</label>
                            </th>
                            <td>
                        <input type="text" name="address" id="address">
                        <p>"Si eres humano por favor no llenes esta información"</p>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Send">
                </form>
            <?php } ?>  
</div>

</div> <?php include('inc/footer.php'); ?>

Ader Reynoso
Ader Reynoso
4,968 Points

Thanks friend! I think this is the problem, but I uploaded in my server and I received the same message Should I configure the same in my hosting? Is there a tutorial how to do that?

Thanks again!

Ader Reynoso
Ader Reynoso
4,968 Points

Hay un error No se puede mandar el email Message body empty <?php if($_SERVER["REQUEST_METHOD"] =="POST"){ $name = trim ($_POST['name']); $email = trim ($_POST['email']); $message = trim($_POST['message']); if ($name =="" OR $email =="" OR $message ==""){ echo "Por favor tienes que poner tu Nombre, tu E-mail y tu Mensaje,"; exit; } foreach($_POST as $value) { if(stripos($value, 'Content-Type:') !== FALSE ) { echo "Existe algún problema con la información que acabas de mandar"; exit; } } if ($_POST ["address"] != "") { echo "Hay un error en la información que acabas de mandar"; exit; } require_once("inc/phpmailer/PHPMailerAutoload.php"); $mail = new PHPMailer(); if(!$mail->ValidateAddress($email)){ echo "Por favor tienes que poner un correo valido"; exit; }

$email_body = "";
$email_body = $email_body ."Name: " .$name ."<br>";
$email_body = $email_body ."Email: " .$email ."<br>";
$email_body = $email_body ."Mensaje:" .$message. "\n";




//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "aderreyra@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "my password here";

//Set who the message is to be sent from
$mail->setFrom($email, 'First Last');

    //Set who the message is to be sent to
$mail->addAddress("aderreyra@gmail.com", 'Ader Reynoso');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';



    if(!$mail->Send()) {
      echo "Hay un error No se puede mandar el email " . $mail->ErrorInfo;
      exit;
    }

header("Location: contact.php?status=thanks");
exit;

} ?> <?php $titleName = "Contacto"; $section = "contact"; include('inc/header.php'); ?>

<div class="section page"> <div class="wraper">

    <h1>Contact</h1>
    <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") {?>
    <p>Thanks for the Email.I&rsquo;ll be in touch Shortly.</p>
        <?php } else{ ?>


            <p>I&rsquo;d love to hear from you! Complete the form to send me an e-mail.</p>
                <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
                    <table>
                        <tr>
                            <th>
                        <label for="name">Name</label>
                            </th>
                            <td>
                        <input type="text" name="name" id="name">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="email">E-mail</label>
                            </th>
                            <td>
                        <input type="text" name="email" id="email">
                            </td>
                        </tr>
                        <tr>
                            <th>
                        <label for="message">Message</label>
                            </th>
                            <td>
                        <textarea name="message" id="message"></textarea> 
                            </td>
                        </tr>
                        <tr style="display:none;">
                            <th>
                        <label for="address">Address</label>
                            </th>
                            <td>
                        <input type="text" name="address" id="address">
                        <p>"Si eres humano por favor no llenes esta información"</p>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Send">
                </form>
            <?php } ?>  
</div>

</div> <?php include('inc/footer.php'); ?>