邮件错误->您必须提供至少一个收件人电子邮件地址

时间:2018-11-09 12:01:07

标签: php phpmailer forgot-password

我正在使用php mailer向用户发送忘记密码链接,这是我的代码

<?php require "config.php";?>
<?php
    use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer\src\Exception.php';

/* The main PHPMailer class. */
require 'PHPMailer\src\PHPMailer.php';

/* SMTP class, needed if you want to use SMTP. */
require 'PHPMailer\src\SMTP.php';


?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Forgot Password System</title>
     <script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
   <form method="post" action="<?php $_SERVER['PHP_SELF']?>">
      <p>Enter Email Address To Send Password Link</p>
      <input type="text" name="email">
      <input type="submit" name="submit_email">
    </form>
 <?php

if(isset($_POST['submit_email']) && $_POST['email'])
{
 $email = $_POST['email'];
  $select=mysqli_query($con,"SELECT email,password from users where email='$email'");
  if(mysqli_num_rows($select)==1)
  {
    while($row=mysqli_fetch_array($select))
    {
      $email=md5($row['email']);
      $pass=md5($row['password']);
    }
    $link="<a href='www.samplewebsite.com/reset.php?key=".$email."&reset=".$pass."'>Click To Reset password</a>";


    $mail = new PHPMailer();
    $mail->CharSet =  "utf-8";
    $mail->IsSMTP();
    // enable SMTP authentication
    $mail->SMTPAuth = true;                  
    // GMAIL username
    $mail->Username = "your_email_id@gmail.com";
    // GMAIL password
    $mail->Password = "your_gmail_password";
    $mail->SMTPSecure = "ssl";  
    // sets GMAIL as the SMTP server
    $mail->Host = "smtp.gmail.com";
    // set the SMTP port for the GMAIL server
    $mail->Port = "465";
    $mail->From='your_gmail_id@gmail.com';
    $mail->FromName='your_name';
    $mail->AddAddress('reciever_email_id', 'reciever_name');
    $mail->Subject  =  'Reset Password';
    $mail->IsHTML(true);
    $mail->Body    = 'Click On This Link to Reset Password '.$pass.'';
    if($mail->Send())
    {
      echo "Check Your Email and Click on the link sent to your email";
    }
    else
    {
      echo "Mail Error - >".$mail->ErrorInfo;
    }
  } 
}
?>


</body>
</html>

当我单击“提交”按钮时出现此错误邮件错误->您必须提供至少一个收件人电子邮件地址。我不知道我在做什么错 实际上我为我的网站复制了这段代码,我不知道这段代码出了什么问题以及为什么它向我显示此错误

1 个答案:

答案 0 :(得分:0)

替换这些行

$mail->From='your_gmail_id@gmail.com';
$mail->AddAddress('reciever_email_id', 'reciever_name');

与此

$mail->setFrom('your_gmail_id@gmail.com', 'mailer_name'); 
$mail->addAddress('reciever_email_id@gmail.com');

check example email了解更多信息

针对以下错误(根据您的评论)

$mail->Host = "ssl://smtp.gmail.com"; //add this host

并检查open_ssl扩展名并将port 465更改为587

这是完整的代码

//Create a new PHPMailer instance
    $mail = new PHPMailer;   
    $mail->isSMTP();
// change this to 0 if the site is going live
    $mail->SMTPDebug = 2;
    $mail->Debugoutput = 'html';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';

 //use SMTP authentication
    $mail->SMTPAuth = true;
//Username to use for SMTP authentication
    $mail->Username = "xxxxxx@gmail.com";
    $mail->Password = "******";
    $mail->setFrom('xxx@ww.com', 'Somebody');
    $mail->addReplyTo('xxx@ww.com', 'Somebody');
    $mail->addAddress('xxx@ww.com', 'Somebody');
    $mail->Subject = 'New contact from somebody';
    // $message is gotten from the form
    $mail->msgHTML($message);
$mail->AltBody = $filteredmessage;
    if (!$mail->send()) {
        echo "We are extremely sorry to inform you that your message
could not be delivered,please try again.";
    } else {
        echo "Your message was successfully delivered,you would be contacted shortly.";
        }

Reference