如何在数组PHPMailer中发送电子邮件

如何在数组PHPMailer中发送电子邮件,php,phpmailer,Php,Phpmailer,我正在我的网站上设置电子邮件通知,通知用户批准索赔。电子邮件将发送给允许批准索赔的多个用户。问题是,我想在“批准”按钮中输入电子邮件地址,以了解谁批准索赔 我已经尝试放入数组,但电子邮件只能发送到第一封电子邮件 下面是我发送多个电子邮件地址的示例代码 do{ $to = $Rows_RecQuery1['email']; $mail->AddAddress($email); }while($Rows_RecQuery1 = mysqli_fetch_array($RecQuery1));

我正在我的网站上设置电子邮件通知,通知用户批准索赔。电子邮件将发送给允许批准索赔的多个用户。问题是,我想在“批准”按钮中输入电子邮件地址,以了解谁批准索赔

我已经尝试放入数组,但电子邮件只能发送到第一封电子邮件

下面是我发送多个电子邮件地址的示例代码

do{ 
$to = $Rows_RecQuery1['email'];
$mail->AddAddress($email);
}while($Rows_RecQuery1 = mysqli_fetch_array($RecQuery1));
下面是我想发送电子邮件的按钮

<a style="border-style:solid; border-color:#F00; background-color:#F00; color:#FFF; text-decoration:none"
                             href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&process=0&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified']));?>">Cancel</a>

<a style="border-style:solid; border-color:#0CC; background-color:#0CC; color:#FFF;text-decoration:none"     
                          href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&$emailprocess=3&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified'])); ?>">Reject</a>

<a style="border-style:solid; border-color:#00F; background-color:#00F; color:#FFF;text-decoration:none"  
                                 href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&$emailprocess=2&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified'])); ?>">Approve</a>

我认为问题出在
mysqli_fetch_array()
中……它将以数组的形式获取整个查询的结果,而不是在迭代时一次获取一行。所以我猜它只会重复一次,因为条件会在第二次失败

尝试使用
mysqli\u fetch\u row()
代替


您遇到了变量混淆的问题。在addAddress中,您已经放置了
$email
变量,在循环中,您正在获取
$Rows\u RecQuery1['email']
。只需将
$to
而不是
$email
,它就可以工作了。我猜您在某处声明了
$email
变量,它包含一个电子邮件地址,这就是为什么只发送一封电子邮件的原因

do{ 
  $to = $Rows_RecQuery1['email'];
  $mail->AddAddress($to);
}while($Rows_RecQuery1 = mysqli_fetch_array($RecQuery1));

您的代码几乎没有问题

  • 首先,您正在执行
    do..while()
    循环您应该执行的
    while()
    ,因为至少在第二个循环之前,您没有可用的数据
  • 然后,您将获取数组而不是关联数组,如果您访问所需索引(而不是名称)上的数据,这是很好的
  • 试试这个:

    while ($contact = mysqli_fetch_assoc($RecQuery1));
    {
       $mail->AddAddress($contact['email']);
    }
    

    另外,如果您向我们展示您的查询,我们可能能够更好地帮助您

    以下是我使用的测试脚本。Swiftmailer是使用软件包管理器安装的,因此我包括自动加载器,而不是直接包含库。另外,我让日志保持打开状态,以防一切都中断,您可以检查生成的日志

    <?php
    $hostname = '';
    $port     = 465; // or anyhting else that works with your server
    $username = '';
    $password = '';
    $sender = ['test@mail.com' => 'John Doe'];
    $receivers = ['test2@mail.com'];
    
    echo date(DATE_RFC2822)."<br/>";
    include ('vendor/autoload.php');
    
    // Create the Transport
    $transport = (new \Swift_SmtpTransport($hostname, $port))
        ->setUsername($username)
        ->setPassword($password);
    
    //$transport->setAuthMode('login'); 
    //$transport->start();
    
    // Create the Mailer using your created Transport
    $mailer = new \Swift_Mailer($transport);
    
    // To use the ArrayLogger
    //$logger = new Swift_Plugins_Loggers_ArrayLogger();
    //$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));
    
    // Or to use the Echo Logger
    $logger = new \Swift_Plugins_Loggers_EchoLogger();
    $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
    
    // Create a message
    $message = (new \Swift_Message('Wonderful Subject'))
        ->setFrom($sender)
        ->setTo($receivers)
        ->setBody('Here is the message itself');
    
    // Send the message
    $result = $mailer->send($message);
    

    PS:如果脚本不能正常工作,现在是早上,没有咖啡,懒得在一两个小时内回来:)。

    非常感谢你的时间和努力:)当然,如果它能帮助你,我很高兴感谢你的时间和努力:)非常感谢你的时间和努力:)你可以试试。
    $Rows_RecQuery1 = mysqli_fetch_array($RecQuery1);
    foreach ($Rows_RecQuery1 as $k=>$v) {
        $receivers[] = $v['email'];
    }
    ....rest of the script.....