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 Using A Third-Party Library

Ader Reynoso
Ader Reynoso
4,968 Points

Third part Library error

Hi Friends! I cant overcome this problem, please I need your help: This is the message I am receiving after click the form contact:"Hay un error No se puede mandar el email Message body empty" a 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; }

//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 here my password";

//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';
$email_body = "";
$email_body = $email_body ."Name: " .$name ."<br>";
$email_body = $email_body ."Email: " .$email ."<br>";
$email_body = $email_body ."Mensaje:" .$message. "\n";



    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'); ?> '''

Where is my mistake¿?

This is drive me crazy thanks a lot for your help!

Ader.

4 Answers

Hey there,

Try using this:

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

Hey there,

Try using this:

$email_body = "";
$email_body .= "Name: " .$name ."<br>";
$email_body .= "Email: " .$email ."<br>";
$email_body .= "Mensaje:" .$message. "\n";
Ader Reynoso
Ader Reynoso
4,968 Points

Thanks Yorick for your answer!

But I continue receiving the same message "Message body empty"

Thanks for your time! Ader.

Unfortunately i can't help you any further as i paused my subscription. If you could tidy up your PHP code i'll take another look !

Also ::

Shouldn't there be somewhere:

$mail->setBody($email_body);

( Also i never did this tutorial so i have no clue )

Cheers

Gareth Borcherds
Gareth Borcherds
9,372 Points

Es cierto lo que ha dicho Yorick Toma. Estas usando un "class" de PHP. Cuando hiciste $mail = new PHPMailer(); tu has empezado a usar este class. El problema que tienes es que no has hecho todas las configuraciones para enviar un mensaje. Has utilizado $mail->Subject lo cual es la función para decir a esta class que es la sujeta del correo. Si quieres decir esta class que es el mensaje del correo, tienes que utilizar the función qua lo hace. Creo que is lo que ha dicho Yorick arriba, pero puede ser algo diferente como $mail->message = $email_body; Me entiendas?

Sorry if the spanish isn't that great, it's been awhile since I learned. Here's the answer again in English and slightly more elaborate.

It's correct what Yorick Toma said above. You are using the PHP class PHP mailer and when you use $mail = new PHPMailer(); you started to use that class by initializing it and storing it in the variable $mail. The problem that you have is that to send a message you have to set all the correct settings before sending it. It appears you didn't set the message body by calling the correct function to do so. You set the subject correctly by using $mail->Subject and you would need to do something similar for setting the email body message. It could be as Yorick suggests above, or it could be something different like $mail->Message. I haven't done the task either and looked for the files, but if you share the class file you are including, I'm sure I could tell you what to set.

Hope this helps. Buena Suerte.