邮件无法发送。错误:您必须至少提供一个收件人电子邮件地址

i am trying to send a message to a selected email from my user database. i am getting the value of user_mail in body content perfectly, when i am sending it to "some@gmail.com but i am getting error "Message could not be sent.Mailer Error: You must provide at least one recipient", when i am trying to send to "user_email. Any suggestions?

Codes:

<?php require_once("connection.php");?> 
<?require_once('libraries/PHPMailer.php');?>
 /*** select the users name from the database ***/
    $dbh = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username,         $mysql_password);
    /*** $message = a message saying we have connected ***/

    /*** set the error mode to excptions ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


    $stmt = $dbh->prepare("SELECT user_email FROM users 
    WHERE user_id = :user_id" );

    /*** bind the parameters ***/
    $stmt->bindParam('user_id', $_SESSION['user_id'], PDO::PARAM_INT);

    /*** execute the prepared statement ***/
    $stmt->execute();

    /*** check for a result ***/
    $user_email = $stmt->fetchColumn();
   }
catch (Exception $e)
{
    /*** if we are here, something is wrong in the database ***/
    $message = 'We are unable to process your request. Please try again later"';
}

$mail = new PHPMailer;

$mail->IsSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.mandrillapp.com';                 // Specify main and backup server
$mail->Port = 587;                                    // Set the SMTP port
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'someone@app.com';                // SMTP username
$mail->Password = 'key';                  // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also      accepted

$mail->From = 'someone@app.com';
$mail->FromName = 'App';
$mail->AddAddress ($user_mail);  // Add a recipient

$mail->IsHTML(true);                                  // Set email format to HTML

$mail->Subject = ' Subject';
$mail->Body    = ' working fine';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->Send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}

correct way of adding recipient address

$address = "whoto@otherdomain.com";
$mail->AddAddress($address, "John Doe");

And try hard coding the $address as mentioned, if it works, then you may have exception in your code which doesn't lets you to set $user_email

change your catch block to:

catch (Exception $e)
{
    /*** if we are here, something is wrong in the database ***/
    $message = 'We are unable to process your request. Please try again later"';
    echo $message;
    echo $e;

}

I don't know if your situation was the same as mine.

I try to put $mail->AddAddress ($user_mail); into a while loop, and it works.

For example:

$email_res = mysql_query($email);

while($email_row = mysql_fetch_assoc($email_res)) {

    $mail->AddAddress($email_row[email]);

}

I tried to put 2 email strings in the (), but it didn't work.

I think $mail->AddAddress() can work when only one email address in $mail->AddAddress(), so I try to put $mail->AddAddress() into a while loop.

Everytime the while loop runs, it will generate a email address and the email address is in $mail->AddAddress(), so the program works.

To add a bit more emphasis to Jean's answer, he is absolutely correct that "$mail->AddAddress" only works with one email address at a time. This class will not accept a comma separated list of email addresses.

Jean shows how to loop from a MySQL query.

If you already have the comma separated list of addresses in hand, you can, of course, just use PHP's explode function to loop through the email addresses. So:

$_to=explode(',',$to);

foreach ($_to as $sendto)          
{
   $mail->AddAddress($sendto);

   if(!$mail->Send()) 
   {
      echo '<p class="center">Mailer Error: <strong style="color:#f00">' . $mail->ErrorInfo .'</strong></p>     
   } 
   else 
   {
      echo '<p style="text-align:center">Message sent to: <strong>'.$sendto.'!</strong></p>';
   }
}