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


Message could not be sent.Mailer Error: You must provide at least one recipient email address

我正试图从我的用户数据库发送消息到选定的电子邮件。我得到user_mail的值在正文内容完美,当我把它发送到"some@gmail.com但我得到错误"消息无法发送。邮件错误:你必须提供至少一个收件人",当我试图发送到"user_email"。有什么建议吗?

代码:

<?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;
}

正确添加收件人地址的方法

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

并尝试硬编码$地址,如果它工作,那么你可能在你的代码中有异常,不允许你设置$user_email

将catch块更改为:

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;
}

我不知道你的情况是否和我的一样。

我尝试把$mail->AddAddress ($user_mail);进入一个while循环,它成功了

例如:

$email_res = mysql_query($email);
while($email_row = mysql_fetch_assoc($email_res)) {
    $mail->AddAddress($email_row[email]);
}

我尝试在()中放入2个电子邮件字符串,但它不工作。

我认为$mail->AddAddress()可以在$mail->AddAddress()中只有一个电子邮件地址时工作,所以我尝试将$mail->AddAddress()放入while循环中。

每次while循环运行时,它都会生成一个电子邮件地址,该电子邮件地址位于$mail->AddAddress()中,因此程序可以运行。

为了强调Jean的回答,他绝对正确地认为"$mail->AddAddress"一次只适用于一个电子邮件地址。这个类不接受逗号分隔的电子邮件地址列表。

Jean展示了如何从MySQL查询进行循环。

如果你已经有了逗号分隔的地址列表,当然,你可以使用PHP的爆炸函数来循环遍历电子邮件地址。所以:

$_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>';
   }
}
相关文章: