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

Imon Dela Rosa
Imon Dela Rosa
7,383 Points

Please I need help. I keep receiving this error message even I put the right format on email address field.

Warning: preg_match() expects parameter 2 to be string, object given in /Applications/MAMP/htdocs/shirts4mike/inc/phpmailer/class.phpmailer.php on line 919 You must specify a valid email address.

Marcin Robert Kaźmierczak
Marcin Robert Kaźmierczak
33,570 Points

We need to know what is in 919 line in class.phpmailer.php file, and what is the code for input field in your contact form.

2 Answers

Imon Dela Rosa
Imon Dela Rosa
7,383 Points

Hi! marcin, I solved the error I put the wrong variable. I put the $mail as the address of the sender. So I changed the $mail variable of the PHPMailer object into $email. Thank you.

     if(!$mail->ValidateAddress($mail)){
        echo "You must specify a valid email address.";
        exit;
    }

//after:

 if(!$mail->ValidateAddress($email)){
        echo "You must specify a valid email address.";
        exit;
    }
Imon Dela Rosa
Imon Dela Rosa
7,383 Points
// Contact form.php

<?php 
// ob_start()

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 address and 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["address"] != ""){
        echo "Your form submission has an error.";
        exit;
    }

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

    if(!$mail->ValidateAddress($mail)){
        echo "You must specify a valid email address.";
        exit;
    }

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

header("Location: contact.php?status=thanks");
exit;
}
    // TODO: Send Email


// ob_flush(); 
?>
<?php 
$pageTitle = "Contact Mike";
$section = "contact";
include('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 { ?>

    <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>

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

        <table>
        <tr>
            <th>

            <label for="name">Name</label>  
        </th>
            <td>
                <input type="text" name="name" id="name">
            </td>
        </tr>

        <tr>
            <th>

            <label for="email">Email</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>Humans (and frogs): please leave this field blank.</p>
            </td>
        </tr>



        </table>

        <input type="submit" value="Send">

    </form>

    <?php } ?>

        </div> <!-- wrapper -->
    </div> <!-- section page -->
<?php include('inc/footer.php'); ?>

// class.phpmailer.php the last line is the 919

return (boolean)preg_match(
        '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
                    '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
                    '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
                    '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
                    '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
                    '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
                    '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
                    '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
                    '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
                    $address
                );

// Appreciate your help. Thanks.