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

James Barrett
James Barrett
13,253 Points

Receiving a 500 error upon submitting a PHPMailer form with SMTP

Code:

http://sandbox.onlinephpfunctions.com/code/fb0b51fcf2b57f26d727fed13e997770a72de5c2

I am unsure what to do! I have been trying to figure this out for hours and no luck - I just keep on receiving the error. This technique of building the form was based off Alena's 'Build a Basic Website'

4 Answers

Hi there,

I'd try showing errors on your page to see what the fatal PHP error is - you can paste this code on top of your page:

ini_set('display_errors', 1);

This will give you something to work with and save your time combing each line.

I'd check to see if your include files are all where they're supposed to be.

Maybe your host should simply be 'smtp.gmail.com' as well instead of the 'ssl' at the beginning, but I'm not positive about that one.

Though I'd start with turning your $mail->setFrom($email, $forename . " " . $surname); into a single variable. I.E.

<?php
$full_name = $forename . " " . $surname;
$mail->setFrom($email, $full_name);
?>
James Barrett
James Barrett
13,253 Points

Thank you for you answer! I am able to identify the fatal error now which is 'Fatal error: Class 'SMTP' not found in C:\Users\James\Documents\GitHub\FootDriveWebsite\inc\phpmailer\class.phpmailer.php on line 1443'. I am not quite sure what this means? Furthermore I have stored the name variables into a $full_name variable now. Which is stored underneath the collection of user inputs :)

Thanks, James.

Are you mailing through your localhost --- and did you change your SMTP to just smtp.gmail.com?

James Barrett
James Barrett
13,253 Points

I am mailing through my localhost using MAMP, is that going to make a difference? If it does how can I test it? Yes I changed the SMTP to smtp.gmail.com. Error still persists.

Thanks.

Try adding this to your code:

$mail->isSMTP();                                      // Set mailer to use SMTP

      $mail->SMTPOptions = array(
      'ssl' => array(
          'verify_peer' => false,
          'verify_peer_name' => false,
          'allow_self_signed' => true
      )
  );
James Barrett
James Barrett
13,253 Points

Fatal error: Class 'SMTP' not found in C:\Users\James\Documents\GitHub\FootDriveWebsite\inc\phpmailer\class.phpmailer.php on line 1443 Still receiving this error. I feel like something is not being recognised and I am not quite sure what it is :S

Try removing the line $mail->Mailer = 'smtp'

And if that doesn't work, try chaining SMPTSecure to 'tis' and the port to 587

  $mail->isSMTP();
  $mail->Host = "smtp.gmail.com";
  $mail->SMTPDebug = 2;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'ssl';
  $mail->Username = "myemail";
  $mail->Password = "mypassword";
  $mail->Port = 465;
  $mail->setFrom($email, $forename . " " . $surname);
  $mail->addAddress("myemail", "James");
  $mail->Subject = 'Test Subject';
  $mail->Body = $email_body;
James Barrett
James Barrett
13,253 Points

Still providing the same error, I think there must be something wrong with the code elsewhere, but unable to distinguish what it is... Hmm