将地址作为数组提供时发送邮件时出错

时间:2016-05-13 08:25:43

标签: php email ldap phpmailer

我有一个动态表,用于输入用户名。在提交时,将从LDAP查询名称以获取电子邮件ID,并将邮件发送给他们。我的代码如下:

<?PHP
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    $email_body = $email_body ."Test";
    require_once("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth   = true;                
    $mail->Host       = "***"; 
    $mail->SetFrom('***');
    $mail->Subject    = "Test";
    $mail->MsgHTML($email_body);
    foreach($_POST['Trainee_Name'] as $traineename) //Field Name in the dynamic table
    {   
        $username="***";
        $password="***";
        $lc = ldap_connect("***") or
        die("Couldn't connect to AD!");
        ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, 3);
        ldap_bind($lc,$username,$password);
        $base = "OU=**,DC=**,DC=**";
        $filt = "(&(&(&(objectCategory=person)(objectClass=user)(name=$traineename*))))";
        $sr = @ldap_search($lc, $base, $filt);
        $info = ldap_get_entries($lc, $sr);
        for ($j = 0; $j < $info["count"]; $j++) 
        {
            echo $address=$info[$j]["mail"][0]."<br/>"  ; //echo's the email ID 
            $mail->AddAddress($address);
        }
        if ($j == 0)
        {
            echo "No matches found!";
        }
        ldap_close($lc);
    }       

    if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent";
        }
}
?>

在输出中,LDAP查询代码回显电子邮件ID,但不发送邮件。哪里可能出错了?我使用下面的链接作为参考:

  1. PHP mailer multiple address
  2. PHPMailer AddAddress()
  3. how to send emails to multiple addresses using phpmailer
  4. 感谢任何建议/协助。

1 个答案:

答案 0 :(得分:1)

我怀疑你的邪恶组合echo-array-add-concat操作会将<br/>附加到每个地址的末尾,这些地址在浏览器中不可见,但会使每个电子邮件地址无效。试试这个:

for ($j = 0; $j < $info['count']; $j++) 
{
    $add = $info[$j]['mail'][0];
    $address[] = $add;
    echo "$add<br/>";
}