1

I have a dynamic table where user names are entered. On submit, the names are queried from LDAP to get the E-Mail ID's and a mail is to be sent to them. My code is as below :

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

In the output, the LDAP query code echoes the E-Mail ID, but doesn't send a mail. Where could I have gone wrong? I used the links below as a reference :

  1. PHP mailer multiple address
  2. PHPMailer AddAddress()
  3. how to send emails to multiple addresses using phpmailer

Appreciate any suggestion / Assistance.

Community
  • 1
  • 1
SR1092
  • 555
  • 1
  • 7
  • 31
  • Don't rely on non-authoritative sources. Take a look at [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps). That shows getting the list from MySQL, but the sending part would be the same as you're using, but it's much mroe efficient. Generally, read the docs, check your input and return values, code cleanly - that combined array-add and echo is horrible! – Synchro May 13 '16 at 08:47
  • for performance reasons you should also consider opening, binding to and closing the ldap connection outside the foreach loop. – heiglandreas May 14 '16 at 10:55

1 Answers1

1

I suspect that your evil combined echo-array-add-concat operation is appending <br/> to the end of every address, which won't be visible in your browser, but will make every email address invalid. Try this:

for ($j = 0; $j < $info['count']; $j++) 
{
    $add = $info[$j]['mail'][0];
    $address[] = $add;
    echo "$add<br/>";
}
Synchro
  • 35,538
  • 15
  • 81
  • 104
  • Good! I'd still recommend basing your code on the mailing list example, it's much more efficient than what you're doing. – Synchro May 13 '16 at 09:07