PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

krystennant
krystennant
7,197 Points

Is video up to date? No class file as described by Alena in the .zip.

I downloaded the zip as described from Github and there is no class file as described by Alena in the video. Am I missing something or is this not how to install it anymore?

krystennant
krystennant
7,197 Points

Okay so looks like PHPMailer had a significant update since this video was made.

I was able to get it working (so far!) via the new updated code - I have it working thus far with it set up like this - but any suggestions / edits would be appreciated since am still new to this stuff. Right now this works on my localhost and actually sends the email to my gmail account (though it goes to Spam XD )

Important notes are you now need require a few more files - the class.phpmailer.php file is now under a different name, etc. And when building the object you need to use that PHPMailer\PHPMailer\PHPMailer syntax.

Hopefully this helps others who may be stumbling in here. There is a more full explanation and changes to syntax of the upgrades from the old version (from the video) to the newer one on the PHPMailer Github .

    require("inc/phpmailer/PHPMailer.php");
    require("inc/phpmailer/SMTP.php");
    require("inc/phpmailer/Exception.php");

    $mail = new PHPMailer\PHPMailer\PHPMailer;

    if (!$mail->ValidateAddress($email)) {
        echo "Invalid Email Address";
        exit;
    }
    // building up the email body
    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .= "Email " . $email . "\n";
    $email_body .= "Details " . $details . "\n";
    $mail->setFrom($email, $name);
    $mail->addAddress('my_sample_example_email@gmail.com', 'yourname'); 
    //Content
    $mail->isHTML(false);
    $mail->Subject = 'Personal Library Suggestion from ' . $name;
    $mail->Body    = $email_body;

    if (!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
        exit;
    } else {
    header("location:suggest.php?status=thanks");
}
}
Andrew Patella
Andrew Patella
2,270 Points

Super helpful THANK YOU!

Sam Gord
Sam Gord
14,059 Points

THANKS VERY MUCH

2 Answers

Jon Berkner
Jon Berkner
10,002 Points

Great!!!! One more thing, if you are using the treehouse workspace you need to send to the localhost. However, the video has

  $mail->addAddress('treehouse@localhost', 'your name');

which will return the error:

Message could not be sent.Mailer Error: You must provide at least one recipient email address.

You must update the address to include localdomain as:

  $mail->addAddress('treehouse@localhost.localdomain', 'your name');
Adrian Keedle
Adrian Keedle
Pro Student 15,318 Points

this has taken me FIVE Days of Pain! I gave up and decided to go through step by step and look at every teachers note.... if i had only scrolled a little further on this question. Jon Berkner amazing thanks! Alena Holligan please please can you update.

Cliff Jackson
Cliff Jackson
2,886 Points

Has this video still not been updated...i am about to do this section??