I’m using the MVC format and just while i’m testing, i’m built a simple form where you submit your email and you get sent an email but for some reason when i submit it just keeps loading and then after a few minutes from timeout it returns no errors! Can someone tell me what i’m doing wrong?
Controller
public function reset()
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$data = [
'email' => $email,
'email_err' => '',
];
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$data['email_err'] .= 'Invalid email address';
}
if(!$this->user->getUserByEmail($email) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$data['email_err'] .= 'Email address does not exist';
}
if(!empty($data['email_err'])) {
$this->view('user/reset', $data);
} else {
try
{
$this->email->sendMail($data['email']);
$this->view('user/login');
}
catch(\Exception $ex) // Global exception again here
{
echo $ex->getMessage();
die();
// Later on, maybe pass the message into a "email not sent" view:
$this->view('common/error'); // Or whatever your view name is
}
}
} else {
$data = [
'email' => '',
'email_err' => ''
];
$this->view('user/reset', $data);
}
}
Model/Class
<?php if (!defined('BASE_PATH')) exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require HELPERS_PATH.'vendor/autoload.php';
class Emails extends Model
{
private $db;
public function __construct()
{
$this->db = new Model();
}
public function sendMail($email)
{
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email@gmail.com'; // SMTP username
$mail->Password = '*******'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('mail@example.com');
$mail->addAddress($email); // Add a recipient // Name is optional
// $mail->addReplyTo('mail@example.com');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Forgotten password';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
} catch (Exception $e) {
throw new \Exception($e->errorMessage()); // Wrap it into a global exception
} catch (\Exception $e) { // Global exception - see the leading slash
throw $e;
}
}
}
View:
<form action="<?php echo FULL_ROOT . '/user/reset'; ?>" method="POST">
<?php echo flash('restore_state'); ?>
<div class="form-group row">
<label for="EmailInput" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" name="email" class="form-control" id="EmailInput" placeholder="Type your email" value="<?php echo $data['email']; ?>" />
<a class='text text-danger'><?php echo $data['email_err']; ?></a>
</div>
</div>
<div class="form-group row">
<div style="margin-left: calc(16.6666667% + 15px);">
<button type="submit" class="btn">Send me a link</button>
</div>
</div>
</form>
One thing i know is that the PHPMailer is all up to date, my gmail email and password are fine and if i was to tamper with the file path i get an error saying not found so my code is finding the phpmailer just fine so i don’t know what is wrong?