Home > other >  PHPMailer error: Undefined property: PHPMailer\PHPMailer\Exception::$getMessage
PHPMailer error: Undefined property: PHPMailer\PHPMailer\Exception::$getMessage

Time:01-21

Need some help with PHPMailer code. I made a contact form with SMTP, and when i submit the vaules, i get this error: Undefined property: PHPMailer\PHPMailer\Exception::$getMessage in C:\xampp\htdocs\contact\mail.php on line 32

What is the wrong?

Here is the php code:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/Exception.php';
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer (true);
$alert = '';
    if (isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    try{
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = '[email protected]';
        $mail ->Password = 'appPassword';
        $mail->SMTPSecure = "tls";
        $mail->Port = '587';
        $mail->setFrom( '[email protected]');
        $mail->addAddress('[email protected]');
        $mail->isHTML (true);
        $mail->Subject = 'Message Received from Contact: ' . $name;
        $mail->Body = "Name: $name <br>Email: $email<br>Subject: $subject<br>Message: $message";
        $mail->send();
        $alert= "<div class='alert-success'><span>Message Sent! Thanks for contact us.</span></div>";
    } catch (Exception $e) {
        $alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
        }
}
?>

Here's the form code so you have it:

<form name="form1" method="post">
                            <div >
                                <div >
                                    <div >
                                    <input type="text"  name="name" id="name" placeholder="Enter name" required="required" />
                                    </div>
                                </div>
                                <div >
                                    <div >
                                        <input type="email"  name="email" id="email" placeholder="Enter email" required="required" />
                                    </div>
                                </div>
                            </div>
                            <div >
                                <div >
                                    <div >
                                        <input type="text"  name="subject" id="subject" placeholder="Subject" required="required" />
                                    </div>
                                </div>
                            </div>
                            <div ><?php echo $alert; ?></div>
                            <div >
                                <div >
                                    <div >
                                        <textarea name="message" id="message"  rows="4" cols="25" required="required" placeholder="Message" style="height: 170px;"></textarea>
                                    </div>                      
                                    <button type="submit"  name="submit" id="submitcontact">Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

CodePudding user response:

You have a semantics error. You started a string with a single quote and you closed it with a double quote. Be sure to start and end each string with the same type of quote.

change this

$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";

to this

$alert = "<div class='alert-error'><span>" . $e->getMessage()."</span></div>";

CodePudding user response:

You got your quotes in your alert message wrong. Change this line:

 $alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";

to this:

$alert = '<div ><span>' . $e->getMessage(). '</span></div>';
  • Related