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.

PHP

What's wrong with contact page? I keep getting this message " Could not instantiate mail function."

This is my code I'm using notepad++

I have it a folder under my main directory named "contact" within that folder I have this file named index.php I also have class.phpmailer.php, but not in the same directory... can I get some help please?

I'm currently at Cleaning URLs with Rewrite Rules

<?php 

require_once("../inc/config.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);

    if ($name == "" OR $email == "" OR $message == "") {
        $error_message = "You must specify a value for name, email address, and message.";
    }

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

    if (!isset($error_message) && $_POST["address"] != "") {
        $error_message = "Your form submission has an error.";
    }

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

    if (!isset($error_message) && !$mail->ValidateAddress($email)){
        $error_message = "You must specify a valid email address.";
    }

    if (!isset($error_message)) {
        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "<br>";
        $email_body = $email_body . "Email: " . $email . "<br>";
        $email_body = $email_body . "Message: " . $message;

        $mail->SetFrom($email, $name);
        $address = "miguelbpa13@gmail.com";
        $mail->AddAddress($address, "Shirts 4 Mike");
        $mail->Subject    = "Shirts 4 Mike Contact Form Submission | " . $name;
        $mail->MsgHTML($email_body); 

        if($mail->Send()) {
            header("Location: " . BASE_URL . "contact/?status=thanks");
            exit;
        } else {
          $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
        }

    }
}

?><?php 
$pageTitle = "Contact Mike";
$section = "contact";
include(ROOT_PATH . 'inc/header.php'); ?>

    <div class="section page">

        <div class="wrapper">

            <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 { ?>

                <?php
                    if (!isset($error_message)) {
                        echo '<p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>';
                    } else {
                        echo '<p class="message">' . $error_message . '</p>';
                    }
                ?>

                <form method="post" action="<?php echo BASE_URL; ?>contact/">

                    <table>
                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Email</label>
                            </th>
                            <td>
                                <input type="text" name="email" id="email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="message">Message</label>
                            </th>
                            <td>
                                <textarea name="message" id="message"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
                            </td>
                        </tr> 
                        <tr style="display: none;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>Humans (and frogs): please leave this field blank.</p>
                            </td>
                        </tr>                   
                    </table>
                    <input type="submit" value="Send">

                </form>

            <?php } ?>

        </div>

    </div>

<?php include(ROOT_PATH . 'inc/footer.php') ?>
Logan R
Logan R
22,989 Points

Is this a server on your computer or is it hosted by a company?

Aaron Graham
Aaron Graham
18,033 Points

I fixed your code so it will display correctly. The markdown cheatsheet will give you some idea as to the different formatting options available. Having your code display correctly will help others better understand your issue, and give you a more accurate response.

3 Answers

Aaron Graham
Aaron Graham
18,033 Points

I have never used PHPMailer, so I don't have any real world experience, but from their GitHub page, it looks like you need to require the autoloader instead of the class file you are currently requiring. When you have a class that inherits from other classes, you need some way to load all the required classes. One way would be to require() each class individually. Obviously, that wouldn't scale well. Another way is to use an autoloader. Autoloaders can be configured to search certain paths for the required classes and load them without having to require() each individual file. Your error, "Could not instantiate mail function", would be consistent with not having all the required classes loaded; PHPMailer() isn't able to find all the classes it needs to build the object you are trying to create an instance of.

Edit: Sorry, I meant to give you some code. I think the problem is here:

require_once(ROOT_PATH . "inc/phpmailer/class.phpmailer.php");

try this instead:

require_once(ROOT_PATH . "inc/phpmailer/PHPMailerAutoload.php");

That is, of course, assuming you have all the PHPMailer files installed in the same directory.

thanks so much!

This is still not answered... I have had to rewrite alot of this code to pass, and evidently it won't accept variables for the email address etc.

Robert Mews
Robert Mews
11,540 Points

I too am receiving this error and the PHPMailerAutoload include file did not resolve the issues.

Robert Mews
Robert Mews
11,540 Points

Edit to my recent post: I fixed the issue as you will need to instal mail config on some server instances: http://stackoverflow.com/questions/1944631/could-not-instantiate-mail-function-why-this-error-occuring