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 trialJason Pohly
8,379 PointsNewer version of PHP Mailer
When using the code for the newest version of PHPMailer, the "$mail = new PHPMailer()" line in the "contact.php" file doesn't seem to work. Whether or not I put a valid email address in the form, it would only return a blank page.
I played around with it for a bit, couldn't get it to work with the new PHPMailer version. Instead, I downloaded the Project Files from this site, and replaced the "class.phpmailer.php" file in my directory with the older "class.phpmailer.php" file from your site. When I did this, the contact form worked correctly.
3 Answers
Jason Pohly
8,379 PointsI think I figured this out...
Just to be clear, everything works as it should with the exact code and file scheme from the tutorial if the old version of PHPMailer is used (Version 5.2.1, which was previously available on Google code, and is currently available in the downloadable zip file on the tutorial page).
When using the new version of PHPMailer (Version 5.2.7, currently available on GitHub), the exact code and file scheme from the tutorial does not work. It produces the following errors:
Warning: require(PHPMailerAutoload.php): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/inc/phpmailer/class.phpmailer.php on line 571
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 571
Line 571 in class.phpmailer.php (v. 5.2.7) says this:
require 'PHPMailerAutoload.php';
it is the last/innermost line in the "__contstruct" function:
public function __construct($exceptions = false)
{
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
exit("Sorry, PHPMailer will only run on PHP version 5 or greater!\n");
}
$this->exceptions = ($exceptions == true);
//Make sure our autoloader is loaded
if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
$autoload = spl_autoload_functions();
if ($autoload === false or !in_array('PHPMailerAutoload', $autoload)) {
require 'PHPMailerAutoload.php';
}
}
}
In order to make everything work correctly with v.5.2.7, all I had to do was include "PHPMailerAutoload.php" in my "inc/phpmailer" directory. At first, I thought that I also had to add the code that Justin Wiswell mentioned above -
require_once("inc/phpmailer/PHPMailerAutoload.php");
- in the "contact.php" script. But I found out that this code is not necessary.
Jason Pohly
8,379 PointsAndrew Shook,
I know it's that line because everything was working fine, then I added the following code to the contact.php file:
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)) {
echo "You must enter a valid email address.";
exit;
}
After that, no matter the content of the form, if I clicked "submit" it redirected to a blank page. So I commented out all the code above. Then uncommented it a line at a time, retrying the "submit" button after each attempt. Whenever $mail = new PHPMailer();
was un-commented, the "submit" button didn't work.
I'm new to this, and I don't know how to display an error log...
Andrew Shook
31,709 Pointsyou can turn on php error reporting by putting this at the top of your contact.php script:
error_reporting(E_ALL | E_STRICT);
ini_set("display_errors", 1);
Justin Wiswell
9,472 PointsI think I ran into this same issue, work around for me (i think) was to include the following file before the phpmailer was initiated:
require_once 'inc/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
this wasn't covered in the video, but i think that was what i did to resolve the issue. (The example docs with the new phpmailer seemed to leverage this. )
Andrew Shook
31,709 PointsYou don't have to use to autoloader. The autoloader simply makes all of the classes, like POP3 and SMTP, available for use with phpmailer. It might be best if Jason posted the entire script and had an instructor like Randy Hoyt look at it.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsJason Pohly, how do you know it is that line specifically? Did you look at the error log? If so, can you share the error? It would be helpful.