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

Jimmy Maurice
Jimmy Maurice
9,379 Points

PhpMailer works locally but not on my server distant.

I can't figure it out why PhpMailer works on my local server whereas it don't on my remote server. I send the same project folder in both places and in the first case i recieved the mail and on the other i didn't. Is it possible that it comes from a name conflict or something ? At the root of my server (distant), i've got all the files of my website, especially a file named "contact.php". (mysite.com/contact.php) And at the same level, i've got my version of "Shirts 4 Mike" project in a folder. (mysite.com/Shirts4Mike/contact.php) Does it could cause an error ?

The error message is : Fatal error: Call to undefined function: stripos() in /mnt/169/sda/7/a/mydomain/mysite/contact.php on line 14

But i cut and paste the snippet from the "Shirts 4 Mike" project.. ???

And this 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 "You must specify a value for name, email adresse et message.";
        exit;
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            echo "There was a problem with the information you entered.";    
            exit;
        }
    }

    if ($_POST["adresse"] != ""){
        echo "Your form submission has an error.";
        exit;
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
        echo "Vous devez spécifier une adresse mail valide.";
        exit;
    }

    $email_body = "";
    $email_body = $email_body . "Nom: " . $name . "<br>";
    $email_body = $email_body . "Email: " . $email . "<br>";
    $email_body = $email_body . "Message: " . $message;

    $mail->SetFrom($email, $name);
    $address = "contact@myserver.com";
    $mail->AddAddress($address, "mysitename");
    $mail->Subject    = "mysitename - Formulaire de Contact | " . $name;
    $mail->MsgHTML($email_body);


    if(!$mail->Send()) {
      echo "Une erreur s'est produite. Le mail n'a pas pu être envoyé." . $mail->ErrorInfo;
      exit;
    } 

    header("Location: contact.php?status=merci");
    exit;
}
?> 
<?php 
    $pageTitle = "Contact";
    include('inc/header.php'); ?>


<div class="section-page">

        <h1>Contact</h1>

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "merci") { ?> 

            <p class="merci">Votre message à bien été envoyé !</p>
            <p class="accueil"><a href="caps.php">Retour aux casquettes ?<a/></p>

        <?php } else { ?>

        <p>Complétez tous les champs pour nous envoyer un email !</p>

        <form action="contact.php" method="post" class="contact">

            <label for="name">Nom </label><br>
            <input type="text" name="name" id="name"><br>

            <label for="email">Email </label><br>
            <input type="text" name="email" id="email"><br>

            <label for="message">Message </label><br>
            <textarea name="message" id="message"></textarea><br>

            <label style="display: none;" for="adresse">Adresse (Ne pas rempir..)</label><br>
            <input style="display: none;" type="text" name="adresse" id="adresse"><br>

            <input type="submit" value="envoyer">
        </form>

 <?php } ?>

    </div>

Does someone ever had the same problem ? Thank's !

jason chan
jason chan
31,009 Points

Have you tried changing the chmod permissions? Contact the admin of the server see if they allow it.

4 Answers

Does your server support the latest version of PHP, as i have had an issue like this before

Jimmy Maurice
Jimmy Maurice
9,379 Points

I think you right Furquan ahmad. I'll check this out. :) Thanks!

Jimmy Maurice
Jimmy Maurice
9,379 Points

Php Version 4.4.3-dev
Well done ! Thanks again !

No problem

Jimmy Maurice
Jimmy Maurice
9,379 Points

Thank's Jason. It was a problem ou php version.