Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team

I am using gmail smtp setting but they seem not to allow me to send email when user register and gets a confirmation. Its giving me this issue, "
error sending confirmation email: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting


What I have tried:

<?php

require_once(__DIR__ . '/sendEmails/vendor/autoload.php');

$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUser = 'root';
$dbPass = '';

try {
  $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
  exit;
}

$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];

// hash the password using bcrypt
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);

// insert the user data into the database
$stmt = $pdo->prepare("INSERT INTO users (name, email, password) VALUES (:name, :email, :password)");
$stmt->execute(['name' => $name, 'email' => $email, 'password' => $hashedPassword]);

if ($stmt->rowCount() > 0) {
  // send confirmation email to the user
  $mail = new PHPMailer\PHPMailer\PHPMailer();
  $mail->isSMTP();
  $mail->Host = 'smtp.gmail.com';  // SMTP server
  $mail->Port = 587;
  $mail->SMTPSecure = 'tls';
  $mail->SMTPAuth = true;
  $mail->Username = 'ggcobani@gmail.com'; // SMTP username
  $mail->Password = 'GENERATED_APP_PASSWORD';  // SMTP password

  $mail->setFrom('gcobani.mkontwana@agilelimitless.org.za', 'Our Site Team');
  $mail->addAddress($email, $name);     // Add a recipient
  $mail->Subject = 'Registration Confirmation';
  $mail->Body    = "Dear $name,\n\nThank you for registering at our site. Your account has been created, and you can now log in using your email address and password.\n\nBest regards,\nOur Site Team";

  if ($mail->send()) {
    echo 'success';
  } else {
    echo 'error sending confirmation email: ' . $mail->ErrorInfo;
  }
} else {
  echo 'error inserting user into database';
}
?>
Posted
Comments
Richard MacCutchan 3-May-23 12:14pm    
I have a feeling that gmail does not allow you to do this. You need to use your own email provider to send the messages.
Gcobani Mkontwana 4-May-23 0:16am    
@Richard MacCutchan yes i did not, but i end up using different account, then was able to be successful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900