PHP Build a Basic PHP Website Enhancing a Form Setting an Error Message Variable

Loy Yee Ko
Loy Yee Ko
5,814 Points

Error page after sending the email

I don't really have any idea which part of the code is wrong, I was following it the video, even debug it, so if anyone got any idea please help thanks!

if($name == "" OR $email == "" OR $details = "" OR $category =="" OR $title =="") {

    $error_message =  "Please fill in the required fields: Name, Email, Details, Category and Title";

}
if ($_POST["address"] != "") {
    $error_message = "Bad Form Input";

}
if(!PHPMailer::validateAddress($email)) {
    $error_message = "Invaild Email Address";

}
if(´╝üisset($error_message)){
        $email_body ="";
        $email_body .= "Name: ".$name."\n";
        $email_body .= "Email: ".$email."\n";
        $email_body .= "\n\nSuggested Item\n\n";
        $email_body .= "Category: ".$category."\n";
        $email_body .= "Titel: ".$title."\n";
        $email_body .= "Format: ".$format."\n";
        $email_body .= "Genre: ".$genre."\n";
        $email_body .= "Year: ".$year."\n";
        $email_body .= "Details: ".$details."\n";

$mail = new PHPMailer; //Tell PHPMailer to use SMTP $mail->isSMTP(); //Enable SMTP debugging // 0 = off (for production use) // 1 = client messages // 2 = client and server messages $mail->SMTPDebug = 2; //Set the hostname of the mail server $mail->Host = 'smtp.gmail.com'; // use // $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6 //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission $mail->Port = 587; //Set the encryption system to use - ssl (deprecated) or tls $mail->SMTPSecure = 'tls'; //Whether to use SMTP authentication $mail->SMTPAuth = true; //Username to use for SMTP authentication - use full email address for gmail $mail->Username = "contact@ingredientadventure.com"; //Password to use for SMTP authentication $mail->Password = "uqujiixceqhpicho";

            //It's important not to use the submitter's address as the from address as it's forgery,
            //which will cause your messages to fail SPF checks.
            //Use an address in your own domain as the from address, put the submitter's address in a reply-to
            $mail->setFrom('contact@ingredientadventure.com', $name);
            $mail->addAddress('contact@ingredientadventure.com', 'Ko Loy Yee');
            $mail->addReplyTo($email, $name);
            $mail->Subject = 'Library Suggestion from '.$name;
            $mail->Body = $email_body;
            if ($mail->send()) {
                header("location:suggest.php?status=thanks");
                exit;
            }
                $error_message = "Mailer Error: " . $mail->ErrorInfo;
        }

}

In order to see errirs on page: 1) In php.ini file set display_errors = on 2) restart yor web server(if you use php like an extension) or php-fpm(in case nginx + php) 3) Put 3 lines of code at the top of yor file

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

In order to use mail you need to have configured SMTP server. Do you have it?) Which Operating System(OS) do you use?

1 Answer

Loy Yee Ko
Loy Yee Ko
5,814 Points

Hey thanks for the tips, I did try that and solved the problem! thanks!