PHPMailer doesn't work

Hey guys, This is the other issue that I have failed to fix which is very frustrating but I have have spent a long time trying too so need help as I have to deliver a working version before Monday which will be the end of my learning project. Any help would be very much appreciated.

Problem: I’ve created a password reset system using PHPMailer and Gmail SMTP. I get no errors but on submitting the form no email is received.

Here is the code:
forgotPass_form.php

<?php
ob_start();
require "dbconnection.php";
if (isset($_POST["forgot_password"])){
    if(!empty($_POST["member-email"])) {
       $email =trim($_POST["member-email"]);    
    } else {
        $error_message = "<li>Email is requred</li>";
    }
    if(empty($error_message)){
        $query = $con->prepare("SELECT email FROM profiles WHERE email =?");
        $query->execute(array($email));
        $member = $query->fetchAll(PDO::FETCH_ASSOC);      
    }
    if (!empty($member)){
    $msg='yes';
    echo "<script type='text/javascript'>alert('$msg');</script>";
    require_once ("forgotPass.php");    
    } else {
        $error_message = "No Email Found";
    }
}
var_dump($_POST)
?>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div id="wrapper">
        <div>
           <form  id="formforget" name="formforget" method="post">
           <h3>Please enter your email to reset your password</h3>
                <?php if(!empty($success_message)) { ?>
                    <div class="success_message"><?php echo $success_message ?>
                    <?php } ?>
                    <?php if(isset($error_message)) { ?> 
                    <div class="error_message"><?php echo $error_message; ?></div>
                    <?php } ?>
                    <input type="email" name="member-email" placeholder="Enter a valid email" required>
                    <input type="submit" value="submit" name="forget-password" id="forget-password">
                </form>
            </div>
         </div>
      </div>
    </body>
</html>

forgotPass.php

<?php
            require("class.phpmailer.php");
            require("PHPMailerAutoload.php");

            define("pusheen_blog", "http://localhost:8080/TEST");

            $mail= new PHPMailer; //enables
            //Server settings
            $mail->SMTPDebug = 0; // Enable
            $mail->isSMTP(); // Set mailer to use SMTP
            $mail->Host       = "smtp.gmail.com"; // Set host name
            $mail->SMTPAuth   = true; // Set this true if SMTP host requires authentification to send mail
            $mail->Username   = "example@example.com"; // Profile username
            $mail->Password   = "Updowninout22$"; // Profile email password
            $mail->SMTPSecure = "tls"; // Enable TLS encryption, `ssl` also accepted
            $mail->Port       = 587; // TCP port to connect to

            //Recipients
            $mail->setFrom('example@example.com', 'WomenWhoCan');
            $mail->FromName = "WomenWhoCan";
            $mail->addAddress('$_POST', 'email'); // Add a recipient
            $mail->addReplyTo('womenwhocan.org.gmail.com');
            //Attachments
            //$mail->addAttachment('/var/tmp/file.tar.gz');          // Add attachments
            //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');     // Optional name

            //Content
            $mail->isHTML(true); // Set email format to HTML
            $mail->Subject = "Forget Password Recovery";
            $mail->Body="<div>".$member[0]["name"]."<br><br><p>Click here to recover your password<br>
            <a href='".pusheen_blog."resetPassword.php?name=".$member[0]["name"]."'> ".pusheen_blog.
            "resetPassword.php?name=".$member[0]["name"]."</a><br><br></p>Regards<br> Admin.</div>";

            if (!$mail->send()) {
                $error_message = "Mailer Error : " . $mail->ErrorInfo;

            } else {
                $success_message = "Your request has been submitted successfully";
            }

            ?>

dbconnection.php

<?php
try {
    
  $con = new PDO ("mysql:host=localhost; dbname=pusheen_blog", "root", "");

//echo "connected";
  $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  }  
  catch(PDOException $e)
  
  {
      echo "error" .$e->getMessage();
  }

I don’t think this is correct.

1 Like

Hey thank you I will check it out now.

I changed $mail->addAddress('$_POST', 'email') to $mail->addAddress('$_POST', 'member-email') which is the name of the related input field on the form.

I now get this when I do the var_dump($_POST)

array(2) { [“member-email”]=> string(21) “sandra.jones@gmail.com” [“forget-password”]=> string(6) “submit” }

On submit i get no errors and still no email arrives. However if I run forgotPass.php I get the following errors:
Notice: Undefined variable: member in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 31

Notice: Undefined variable: member in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 32

Notice: Undefined variable: member in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 33

I figured maybe I needed to include the forgotPass_form.php since it contains the following variable
$member = $query->fetchAll(PDO::FETCH_ASSOC);

That should be $mail->addAddress($_POST['member-email']);

Thanks.

I realised another error
(isset($_POST["forgot_password"])){
should read
(isset($_POST["forget-password"])){

Now when I click submit I get the localhost success alert so I guess it has reached the point to validate the email address.

It fails at this point now

 if (!$mail->send()) {
 $error_message = "Mailer Error : " . $mail->ErrorInfo;

These are the errors:
Notice: Undefined index: name in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 34

Notice: Undefined index: name in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 35

Notice: Undefined index: name in C:\xampp\htdocs\pusheen_blog\phpmailer\forgotPass.php on line 36

here are those lines of code:

$mail->Body="<div>".$member[0]["name"]."<br><br><p>Click here to recover your password<br>
            <a href='".pusheen_blog."resetPassword.php?name=".$member[0]["name"]."'> ".pusheen_blog.
            "resetPassword.php?name=".$member[0]["name"]."</a><br><br></p>Regards<br> Admin.</div>";

New development. Some idiot hacked my test gmail account and deleted it. I guess that was my own fault because I posted the pw on her in the code. Was only a test account so I didn’t think it mattered. I’m just going to create another one now and see if it works.

A lot of these errors are telling you that you are trying to reference these things that don’t exist. They don’t exist because they aren’t assigned properly.

The next time you post your codes, you may want to omit any sensitive information from your code so that it doesn’t happen again.

Yes of course I realise that but it was a test account so you can create lots of them. I’ve sorted a new one our now thanks.

I realise that the errors imply that something has not been defined but declared. However I can’t figure out probably because I have been coding since 3am yesterday. Still I need to finish this project or I will fail and i really don’t want too.

I really appreciate everyones help who have replied so far. I wish I had found this site weeks ago.

No. It should be read like that either. PHP cannot read hyphens (-). You will have to either use underscores (_) or combine the word. Also, don’t use if(isset($_POST[...])) to check for form submission. Use if($_SERVER['REQUEST_METHOD'] == 'POST']) instead.

I haven’t thoroughly studied your code, but because $member[0]['name'] is undefined I’m wondering if that particular $member variable is from the query that is like SELECT email FROM profiles ... it would make sense because the query did not SELECT the name field.

1 Like

Not in regular variables, no, so $forget-password is not a valid variable name (although there are ways around that if you really want to), but using hypens in array keys is perfectly fine.

1 Like

hey thanks for your input Mittineague I am still failing to sort the issue and my deadline is 2 hours :(.

Just wondering, did you make your deadline @louisejean?

Hi, Yes i did for the comments form, thanks to you :slight_smile: However I have 2 more tasks that need to be finished by 6pm this evening. I’m almost there with the phpmailer issue for the password reset system but I am having an issue with fitting in the design on the signup form. I will raise another topic though. I have an assessment on Friday so hopefully if I get this sprint completed I can have a few days to prepare for it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.