EDIT number 2: I have tried phpmailer instead of mail() function. Please, take a look at the code below. I have created a few email accounts to test the code, none worked: yahoo, gmail, yandex. I am posting one the code that contains one of those accounts including real password. Please, help to get through this. I have the whole pset ready and I have been doing my best to solve the email issue for a second day. I am among less comfortable. I have even tried it with the harvard's address - the one show in the walkthrough froshims-3. https://courses.edx.org/courses/course-v1:HarvardX+CS50+X/courseware/4112040f92094edeba0e5e49b7f1fffe/67c3d981b20f41a69692839397966f2a/
In some instances I get
SMTP Error: Could not connect to SMTP host. Message could not be sent.
Mailer Error: SMTP Error: Could not connect to SMTP host.
In others I get error indicating that authentication failed.
<?php
// configuration
require("../includes/config.php");
require("libphp-phpmailer/class.phpmailer.php");
if (isset($_SESSION["id"]))
{
$row = CS50::query("SELECT username, email FROM users WHERE id = ?", $_SESSION["id"]);
$email = $row[0]["email"];
$name = $row[0]["username"];
/***
// wanted to have a secure link, but for now commenting it out
$salt = "3453#2K:90k≈“π631%380‘“HJK0D*7WPJ987NU";
// Create the unique user password reset key
$password = hash('sha512', $salt.$email);
// url to reset password
$reset_url = "https://ide50-alla.cs50.io/new_password.php?q=".$password;
***/
$reset_url = "https://ide50-alla.cs50.io/new_password.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "[email protected]";
$mail->Password = "audrey777";
$mail->SetFrom("[email protected]");
$mail->AddAddress($email);
$mail->Subject = "Requested link from CS50 Finance web page";
$mail->Body = "Dear $name,\n\nYou have requested to reset your password for CS50 Finance web page.
\n\nTo reset your password, please click the link below.\n\n".$reset_url."\n\nThank you!";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
apologize("Couldn't send email");
}
else
{
// render form
render("email_link_form.php", ["email" => $email]);
}
}
else
{
redirect("login.php");
}
?>
EDIT: my question is not truly a duplicate, because I am asking for help on finding mistakes in my code. But reading about phpmailer() I have realized that mail() function simply doesn't work correctly in many instances.