Skip to main content
Bumped by Community user
added phpmailer code
Source Link
Vitale
  • 235
  • 2
  • 8

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.

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.

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.

deleted 212 characters in body
Source Link
Vitale
  • 235
  • 2
  • 8

EDIT: my question is not truly a duplicate. I have a code, whichbecause I am presenting in my question, and I will be gratefulasking for your help on finding my mistakes in my code. Also, please, notice thatBut reading about phpmailer() I am using phphave realized that mail() function, and not customized CS50 phpmailer functions; it is important for me to use php functions, not customized ones (although I, of course, use query and other functions created for this particular pset). Thank you for understanding simply doesn't work correctly in many instances.
I

I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

EDIT: my question is not a duplicate. I have a code, which I am presenting in my question, and I will be grateful for your help on finding my mistakes. Also, please, notice that I am using php mail() function, and not customized CS50 phpmailer functions; it is important for me to use php functions, not customized ones (although I, of course, use query and other functions created for this particular pset). Thank you for understanding.
I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

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.

I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

Post Undeleted by Vitale
Post Deleted by Vitale
not a duplicate
Source Link
Vitale
  • 235
  • 2
  • 8

EDIT: my question is not a duplicate. I have a code, which I am presenting in my question, and I will be grateful for your help on finding my mistakes. Also, please, notice that I am using php mail() function, and not customized CS50 phpmailer functions; it is important for me to use php functions, not customized ones (although I, of course, use query and other functions created for this particular pset). Thank you for understanding.
I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

http://php.net/manual/en/function.mail.php

<?php

    // configuration
    require("../includes/config.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"];
        
        $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-name.cs50.io/new_password.php?q=".$password;
        $email_subject = "Requested link from CS50 Finance web page";
        //email the url
        $email_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($email, $email_subject, $email_body))
        {               
           // render form
           render("email_link_form.php", ["email" => $email]);
        }
    }
    else
    {
        redirect("login.php");
    }

?>

Thank you very much!

I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

http://php.net/manual/en/function.mail.php

<?php

    // configuration
    require("../includes/config.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"];
        
        $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-name.cs50.io/new_password.php?q=".$password;
        $email_subject = "Requested link from CS50 Finance web page";
        //email the url
        $email_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($email, $email_subject, $email_body))
        {               
           // render form
           render("email_link_form.php", ["email" => $email]);
        }
    }
    else
    {
        redirect("login.php");
    }

?>

Thank you very much!

EDIT: my question is not a duplicate. I have a code, which I am presenting in my question, and I will be grateful for your help on finding my mistakes. Also, please, notice that I am using php mail() function, and not customized CS50 phpmailer functions; it is important for me to use php functions, not customized ones (although I, of course, use query and other functions created for this particular pset). Thank you for understanding.
I am struggling with pset7, but in a happy way because, although it's a hard task, I have learned a lot. Now I am doing reset password part. I am among less comfortable, but happy to learn this fascinating stuff. I will be grateful for help. Please, take a look at the code below. I don't see why mail() function doesn't send an email. I have checked all variables from query(), all are fine, and all display correct values, but email doesn't arrive.

http://php.net/manual/en/function.mail.php

<?php

    // configuration
    require("../includes/config.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"];
        
        $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-name.cs50.io/new_password.php?q=".$password;
        $email_subject = "Requested link from CS50 Finance web page";
        //email the url
        $email_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($email, $email_subject, $email_body))
        {               
           // render form
           render("email_link_form.php", ["email" => $email]);
        }
    }
    else
    {
        redirect("login.php");
    }

?>

Thank you very much!

added the check for mail() function
Source Link
Vitale
  • 235
  • 2
  • 8
Loading
edited body
Source Link
Vitale
  • 235
  • 2
  • 8
Loading
Source Link
Vitale
  • 235
  • 2
  • 8
Loading