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

daniwao
daniwao
13,125 Points

Depreciated Code?

Hi i was wondering if the $mail -> ValidateAddress($email) is depreciated code from this lesson. I see that Randy made this in 2012 with the PHPMailer. I'm trying this now and it's not showing up at all. Here's my code below.

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

$mail = new PHPMailer();

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

My include folder looks exactly the same. inc/phpmailer/class.phpmailer.php

3 Answers

Hi!

I've just checked PHPMailer on GitHub, class.phpmailer.php file and there is method validateAddress on its place. Check it out: https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php#L856

I think you problem is you type: ValidateAddress, but should validateAddress. It's not a class name, method's name should start with lowercase letter.

Also, it can be very useful to turn on error reporting in PHP: http://www.php.net//manual/en/function.error-reporting.php

ini_set('display_errors', 'On');
error_reporting(E_ALL);
daniwao
daniwao
13,125 Points

Thank you Alex, I'm wondering where should I put in the error reporting in PHP. Should I place it within my index.php file or in every file?

The good place for this is config.php file because it included on the top of all other project's php files.

But also you can turn on the error reporting globally in a php.ini file. What kind of a local server are you using? MAMP, XAMPP, self-installed php with Apache?

daniwao
daniwao
13,125 Points

I'm using MAMP and also I tried changing the ValidAddress to lower case validAddress and it still does not work. Here's my code below.

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

$mail = new PHPMailer();

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

Hm, strange. I've just take a look at my code and it is the following:

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

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

It seems that upper case 'ValidateAddress' is valid. I was wrong.

Try to enable error reporting and see if any errors appears. Enable PHP Error Reporting in MAMP

daniwao
daniwao
13,125 Points

I turned on the Enable PHP and this is the error that I get after I click Submit when there is no valid email present.

Warning: require(PHPMailerAutoload.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/inc/phpmailer/class.phpmailer.php on line 575

Fatal error: require(): Failed opening required 'PHPMailerAutoload.php' (include_path='.:/Applications/MAMP/bin/php/php5.5.10/lib/php') in /Applications/MAMP/htdocs/inc/phpmailer/class.phpmailer.php on line 575

Now I know that this means to look inside my class.phpmailer.php file and here is what it says on that file on line 575.

if (version_compare(PHP_VERSION, '5.1.2', '>=')) { $autoload = spl_autoload_functions(); if ($autoload === false or !in_array('PHPMailerAutoload', $autoload)) { require 'PHPMailerAutoload.php'; } }

Not sure why I need the Autoloader. I downloaded the recent version of PHP Mailer from the Github repo. Could use all help!

Alexander Sokol Hampton Paulk Randy Hoyt

I think, now you can try putting the file PHPMailerAutoload.php inside /Applications/MAMP/htdocs/inc/phpmailer/ directory. Or better all files from downloaded from GitHub PHP Mailer in this directory.

Or you can download version of class.phpmailer.php https://dl.dropboxusercontent.com/u/55243134/class.phpmailer.php.zip that I use and try replacing you version with mine.