skip to Main Content

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: PHPMailerPHPMailerException::$getMessage in C:xampphtdocscontactmail.php on line 32

What is the wrong?

Here is the php code:

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
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 class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                    <input type="text" class="form-control input-lg" name="name" id="name" placeholder="Enter name" required="required" />
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input type="email" class="form-control input-lg" name="email" id="email" placeholder="Enter email" required="required" />
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <input type="text" class="form-control input-lg" name="subject" id="subject" placeholder="Subject" required="required" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-12"><?php echo $alert; ?></div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <textarea name="message" id="message" class="form-control" rows="4" cols="25" required="required" placeholder="Message" style="height: 170px;"></textarea>
                                    </div>                      
                                    <button type="submit" class="btn btn-skin btn-block" name="submit" id="submitcontact">Submit</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

2

Answers


  1. 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 class="alert-error"><span>' . $e->getMessage(). '</span></div>';
    
    Login or Signup to reply.
  2. 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>";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search